1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
#include <stdio.h> #include <dlfcn.h> #include <string.h> #include <errno.h> #include <stdlib.h> #include <stdarg.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <signal.h>
void error(const char *file, int line,const char *str, ...) { va_list fmt; va_start(fmt, str); fprintf(stderr, "error:%s %s:%d\n", strerror(errno), file, line); vfprintf(stderr, str, fmt); fprintf(stderr, "\n"); va_end(fmt); }
#define ERROR_EXIT(ret, msg...) do { error(__FILE__, __LINE__, msg); ret } while(0)
#define ERROR(msg...) ERROR_EXIT(exit(1);, msg) #define FAIL(ret, msg...) ERROR_EXIT(ret, msg) #define PRINT_ERROR_STR(msg...) FAIL(;, msg)
#define safe_free(ptr) do { if(ptr) { free(ptr); ptr = NULL;} } while(0)
char *alloc_sprintf(const char * __format, ...) { va_list fmt; va_start(fmt, __format); int len = vsnprintf(NULL, 0, __format, fmt); va_end(fmt); char *str = malloc(len+1); if(str == NULL) ERROR(""); va_start(fmt, __format); vsnprintf(str, len+1, __format, fmt); va_end(fmt); return str; }
char *__client_fifo = NULL; void __attribute__((destructor)) cleanup() { safe_free(__client_fifo); }
#define SERVER_FIFO "server-fifo" #define CLIENT_FIFO (__client_fifo == NULL ? (__client_fifo = alloc_sprintf("client-fifo-%d", getpid())) : __client_fifo) #define GET_CLIENT_FIFO(pid) alloc_sprintf("client-fifo-%d", pid) #define GLOBAL_NUMBER_FILE "global_number_file" struct client_info { pid_t pid; int incr; };
unsigned long long int number = 0; void sync_global(unsigned long long int *old, unsigned long long int *new) { unsigned long long int buf; int global_numberfd = open(GLOBAL_NUMBER_FILE, O_RDWR | O_SYNC | O_CREAT, 0666); if(global_numberfd == -1) FAIL(return;,"");
ssize_t readsize = read(global_numberfd, &buf, sizeof(unsigned long long int)); if(readsize < 0) PRINT_ERROR_STR(""); lseek(global_numberfd, 0, SEEK_SET); if(old) { if(write(global_numberfd, old, sizeof(unsigned long long int)) != sizeof(unsigned long long int)) PRINT_ERROR_STR(""); } if(readsize > 0 && new) *new = buf; close(global_numberfd); }
int server_fd; int dummy_fd; void on_server_exit() { close(server_fd); close(dummy_fd); unlink(SERVER_FIFO); sync_global(&number, NULL); } void handler(int sig) { printf("server, sig:%s\n", strsignal(sig)); on_server_exit(); signal(sig, SIG_DFL); raise(sig); }
void destroy(int sig) { printf("server, sig:%s\n", strsignal(sig)); on_server_exit(); unlink(GLOBAL_NUMBER_FILE); _exit(0); }
int server(int argc, char *argv[]) { atexit(on_server_exit); signal(SIGPIPE, handler); signal(SIGINT, destroy); signal(SIGTERM, destroy); signal(SIGHUP, handler); signal(19, handler); if(mkfifo(SERVER_FIFO, 0666) == -1) ERROR("");
struct client_info buf; ssize_t rd_size; sync_global(NULL, &number); printf("number = %llu\n", number); for(int i = 0 ;; i++) { if((server_fd = open(SERVER_FIFO, O_RDONLY)) == -1) ERROR(""); printf("i = %d\n", i); while ((rd_size = read(server_fd, &buf, sizeof(struct client_info))) > 0) { printf("server: handling request from %d,%d\n", buf.pid, buf.incr); char *client_fifo = GET_CLIENT_FIFO(buf.pid); if(access(client_fifo, F_OK | W_OK) != 0) ERROR_EXIT(goto cleanup_noaction;, "");
int client_fd; if ((client_fd = open(client_fifo, O_WRONLY | O_NONBLOCK)) == -1) ERROR_EXIT(goto cleanup_noaction;, ""); int flag; if((flag = fcntl(client_fd, F_GETFL)) == -1) ERROR_EXIT(goto cleanup;, ""); flag &= ~O_NONBLOCK; if(fcntl(client_fd, F_SETFL, flag) == -1) ERROR_EXIT(goto cleanup;, ""); sigset_t sigset, oldset; if(sigemptyset(&sigset) == -1) ERROR_EXIT(goto cleanup_nomask;, ""); if(sigemptyset(&oldset) == -1) ERROR_EXIT(goto cleanup_nomask;, ""); if(sigaddset(&sigset, SIGPIPE) == -1) ERROR_EXIT(goto cleanup_nomask;, ""); if(sigprocmask(SIG_SETMASK, &sigset, &oldset) == -1) ERROR_EXIT(goto cleanup_nomask;, ""); if (write(client_fd, &(unsigned long long int[1]) {number}, sizeof(unsigned long long int[1])) != sizeof(unsigned long long int[1])) ERROR_EXIT(goto cleanup;, ""); number += buf.incr; cleanup: if(sigprocmask(SIG_SETMASK, &oldset, NULL) == -1) ERROR_EXIT(goto cleanup;, ""); cleanup_nomask: sync_global(&number, NULL); close(client_fd); cleanup_noaction: safe_free(client_fifo); } sleep(1); close(server_fd); if(rd_size < 0) ERROR(""); } return 0; } void clientdestroy(int sig) { printf("client %d, sig:%s\n", getpid(), strsignal(sig)); unlink(CLIENT_FIFO); signal(sig, SIG_DFL); raise(sig); } int client(int argc, char *argv[]) { int client_fd; signal(SIGHUP, clientdestroy); setbuf(stdout, NULL); printf("client %d open\n", getpid()); if((server_fd = open(SERVER_FIFO, O_WRONLY)) == -1) ERROR(""); if(mkfifo(CLIENT_FIFO, 0666) == -1) ERROR(""); struct client_info info = { .incr=atoi(argv[1]), .pid=getpid() }; printf("client %d write\n", getpid()); if(write(server_fd, &info, sizeof(struct client_info)) != sizeof(struct client_info)) { ERROR(""); } unsigned long long int n; printf("client %d read\n", getpid()); printf("client %d open1\n", getpid()); if((client_fd = open(CLIENT_FIFO, O_RDONLY)) == -1) ERROR(""); if(read(client_fd, &n, sizeof(unsigned long long int)) != sizeof(unsigned long long int)) { ERROR(""); } printf("client %d get id: %llu\n", getpid(), n); close(server_fd); close(client_fd); unlink(CLIENT_FIFO); }
int main(int argc, char *argv[]) { argv++; argc--; if(!strcmp("server", argv[0])) { server(argc, argv); } else if(!strcmp("client", argv[0])) { client(argc, argv); } return 0; }
|