00001 /* 00002 FIFO-based server 00003 AUP2, Sec. 7.02.2 00004 00005 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00006 May be copied only for purposes and under conditions described 00007 on the Web page www.basepath.com/aup/copyright.htm. 00008 00009 The Example Files are provided "as is," without any warranty; 00010 without even the implied warranty of merchantability or fitness 00011 for a particular purpose. The author and his publisher are not 00012 responsible for any damages, direct or incidental, resulting 00013 from the use or non-use of these Example Files. 00014 00015 The Example Files may contain defects, and some contain deliberate 00016 coding mistakes that were included for educational reasons. 00017 You are responsible for determining if and how the Example Files 00018 are to be used. 00019 00020 */ 00021 #include "defs.h" 00022 #include "smsg.h" 00023 00024 #define WANT_BUGFIX 00025 00026 /* Server */ 00027 /* Assumes no partial reads */ 00028 /*[main]*/ 00029 int main(void) 00030 { 00031 int fd_server, fd_client, i; 00032 #ifdef WANT_BUGFIX 00033 int fd_server_w; 00034 #endif 00035 ssize_t nread; 00036 struct simple_message msg; 00037 char fifo_name[100]; 00038 00039 printf("server started\n"); 00040 if (mkfifo(SERVER_FIFO_NAME, PERM_FILE) == -1 && errno != EEXIST) 00041 EC_FAIL 00042 // will block waiting for writer 00043 ec_neg1( fd_server = open(SERVER_FIFO_NAME, O_RDONLY) ) 00044 #ifdef WANT_BUGFIX 00045 ec_neg1( fd_server_w = open(SERVER_FIFO_NAME, O_WRONLY) ) 00046 #endif 00047 while (true) { 00048 /* will return EOF if no writers -- a bug! */ 00049 ec_neg1( nread = read(fd_server, &msg, sizeof(msg)) ) 00050 //printf("back from read; nread = %d\n", nread); 00051 if (nread == 0) { 00052 errno = ENETDOWN; 00053 EC_FAIL 00054 } 00055 for (i = 0; msg.sm_data[i] != '\0'; i++) 00056 msg.sm_data[i] = toupper(msg.sm_data[i]); 00057 ec_false( make_fifo_name(msg.sm_clientpid, fifo_name, 00058 sizeof(fifo_name)) ) 00059 ec_neg1( fd_client = open(fifo_name, O_WRONLY) ) 00060 ec_neg1( write(fd_client, &msg, sizeof(msg)) ) 00061 ec_neg1( close(fd_client) ) 00062 } 00063 /* never actually get here */ 00064 ec_neg1( close(fd_server) ) 00065 #ifdef WANT_BUGFIX 00066 ec_neg1( close(fd_server_w) ) 00067 #endif 00068 exit(EXIT_SUCCESS); 00069 00070 EC_CLEANUP_BGN 00071 exit(EXIT_FAILURE); 00072 EC_CLEANUP_END 00073 }