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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
#include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> #include <fcntl.h> #include <pthread.h> #include <unistd.h> #include <signal.h> #include <stdbool.h> #include <stddef.h> #include <syslog.h> #include <wait.h> #include <sys/stat.h> #include <sys/ipc.h> #include <sys/sem.h>
#define DEBUG_LEVEL LOG_DEBUG
#ifndef DEBUG_LEVEL #define DEBUG_LEVEL LOG_INFO #endif #define STRING_MSG "MSG" FILE *logfile = NULL; bool syslog_enable = false; void __attribute__ ((constructor)) init() { syslog_enable = false; logfile = stderr; }
#define alloc_sprintf(__alloc_sprintf_str, __format...) do { \ int __alloc_sprintf_len = snprintf(NULL, 0, __format); \ (__alloc_sprintf_str) = malloc(__alloc_sprintf_len+1); \ if(__alloc_sprintf_str != NULL) \ snprintf(__alloc_sprintf_str, __alloc_sprintf_len+1, __format); \ } while(0)
#define logger(level, msg...) do { \ if(level <= DEBUG_LEVEL) { \ fprintf(logfile, "[%ld] ", (long) getpid()); \ fprintf(logfile, msg); \ fprintf(logfile, "\n"); \ } \ if(syslog_enable) { \ char *data; \ alloc_sprintf(data, msg); \ syslog(level, "%s", data); \ safe_free(data); \ } \ } while(0)
#define COND_RET(x, ret, msg...) \ do { \ if(!(x)) { \ if(errno != 0) logger(LOG_ERR, "Error(%d): %s", errno, strerror(errno)); \ logger(LOG_ERR, "%s:%d", __FILE__, __LINE__); \ logger(LOG_ERR, "unmet condition:\"%s\"", #x); \ logger(LOG_ERR, msg); \ ret; \ } \ errno = 0; \ } while(0)
#define CHECK(x, msg...) COND_RET(x, return -1;, msg) #define CHECK_EXIT(x, msg...) COND_RET(x, exit(EXIT_FAILURE);, msg) #define CHECK_LOG(x, msg...) COND_RET(x, ;, msg)
#define safe_free(__safe_free_ptr) do { if(__safe_free_ptr) { free(__safe_free_ptr); (__safe_free_ptr) = NULL;} } while(0)
#define BUFFER_SIZE 4096
union semun { int val; struct semid_ds *buf; unsigned short *array; #if defined(__linux__) struct seminfo *__buf; #endif };
typedef struct { int semid; }EventFlag_t;
int incrsem(int sem_id, int semnum, short incr, short flg) { struct sembuf sembuf; sembuf = (struct sembuf){ .sem_num=semnum, .sem_flg=flg, .sem_op=incr }; return semop(sem_id, &sembuf, 1); }
int P(int sem_id) { return incrsem(sem_id, 0, -1, 0); }
int V(int sem_id) { return incrsem(sem_id, 0, 1, 0); }
int waitFor(int sem_id) { return incrsem(sem_id, 1, 0, 0); }
int getsem(int sem_id, int semnum, short *n) { union semun arg; int ret = semctl(sem_id, semnum, GETVAL, arg); *n = ret; return ret; }
int setsem(int sem_id, int semnum, short n) { union semun arg; arg.val = n; return semctl(sem_id, semnum, SETVAL, arg); }
int notifyAll(int sem_id) { return setsem(sem_id, 1, 0); } EventFlag_t *newEventFlag(const char *file, char x) { EventFlag_t eventFlag; key_t key = ftok(file, x); eventFlag.semid = semget(key, 4, IPC_CREAT | IPC_EXCL | 0666); union semun arg; if(eventFlag.semid == -1) { if(errno == EEXIST) { eventFlag.semid = semget(key, 4, 0666); COND_RET(eventFlag.semid != -1, return NULL, STRING_MSG); struct semid_ds ds; arg.buf = &ds; COND_RET(semctl(eventFlag.semid, 0, IPC_STAT, arg) != -1, return NULL, STRING_MSG); while (ds.sem_otime == 0) { logger(LOG_INFO, "waiting for sem init"); sleep(1); COND_RET(semctl(eventFlag.semid, 0, IPC_STAT, arg) != -1, return NULL, STRING_MSG); } logger(LOG_INFO, "semget get old"); } else { CHECK_LOG(false, STRING_MSG); return NULL; } } else { arg.array = (unsigned short *)&(unsigned short[]){0, 0, 0, 0}; if(semctl(eventFlag.semid, 0, SETALL, arg) == -1) { CHECK_LOG(false, STRING_MSG); CHECK_LOG(semctl(eventFlag.semid, 0, IPC_RMID) != -1, STRING_MSG); return NULL; } if(incrsem(eventFlag.semid, 0,1,0) == -1) { CHECK_LOG(false, STRING_MSG); CHECK_LOG(semctl(eventFlag.semid, 0, IPC_RMID) != -1, STRING_MSG); return NULL; } logger(LOG_INFO, "semget create new"); } return memcpy(malloc(sizeof(EventFlag_t)), &eventFlag, sizeof(EventFlag_t)); }
int setEventFlag(EventFlag_t *eventFlag, int flag) { CHECK(P(eventFlag->semid) != -1, STRING_MSG); short currentFlag;
CHECK(getsem(eventFlag->semid, 2, ¤tFlag) != -1, STRING_MSG); CHECK(setsem(eventFlag->semid, 2, currentFlag | (flag & 0xffff)) != -1, STRING_MSG);
CHECK(getsem(eventFlag->semid, 3, ¤tFlag) != -1, STRING_MSG); CHECK(setsem(eventFlag->semid, 3, currentFlag | ((flag >> 16) & 0xffff)) != -1, STRING_MSG);
CHECK(notifyAll(eventFlag->semid) != -1, STRING_MSG); CHECK(V(eventFlag->semid) != -1, STRING_MSG); return 0; }
int clearEventFlag(EventFlag_t *eventFlag, int flag) { CHECK(P(eventFlag->semid) != -1, STRING_MSG); short currentFlag;
CHECK(getsem(eventFlag->semid, 2, ¤tFlag) != -1, STRING_MSG); CHECK(setsem(eventFlag->semid, 2, currentFlag & ~(flag & 0xffff)) != -1, STRING_MSG);
CHECK(getsem(eventFlag->semid, 3, ¤tFlag) != -1, STRING_MSG); CHECK(setsem(eventFlag->semid, 3, currentFlag & ~((flag >> 16) & 0xffff)) != -1, STRING_MSG);
CHECK(V(eventFlag->semid) != -1, STRING_MSG); return 0; }
int __getEventFlag(EventFlag_t *eventFlag, int *flag) { if(flag) { short currentFlag = 0; int ret = 0; CHECK(getsem(eventFlag->semid, 2, ¤tFlag) != -1, STRING_MSG); ret |= currentFlag; CHECK(getsem(eventFlag->semid, 3, ¤tFlag) != -1, STRING_MSG); ret |= currentFlag << 16; *flag = ret; return 0; } return -1; }
int getEventFlag(EventFlag_t *eventFlag, int *flag) { CHECK(P(eventFlag->semid) != -1, STRING_MSG); CHECK_LOG(__getEventFlag(eventFlag, flag) != -1, STRING_MSG); CHECK(V(eventFlag->semid) != -1, STRING_MSG); return 0; } #define waitForMethod(eventFlag, flag, method) do { \ int currentFlag;\ CHECK(P(eventFlag->semid) != -1, STRING_MSG);\ __getEventFlag(eventFlag, ¤tFlag); \ while (method) {\ CHECK(V(eventFlag->semid) != -1, STRING_MSG);\ CHECK(waitFor(eventFlag->semid) != -1, STRING_MSG);\ CHECK(P(eventFlag->semid) != -1, STRING_MSG);\ __getEventFlag(eventFlag, ¤tFlag); \ }\ CHECK(V(eventFlag->semid) != -1, STRING_MSG);\ } while(0)
int waitForAny(EventFlag_t *eventFlag, int flag) { waitForMethod(eventFlag, flag, (currentFlag & flag) == 0); return 0; } int waitForAll(EventFlag_t *eventFlag, int flag) { waitForMethod(eventFlag, flag, (currentFlag & flag) != flag); return 0; } void destroyEventFlag(EventFlag_t **eventFlag) { if(eventFlag) { if(*eventFlag) { semctl((*eventFlag)->semid, 0, IPC_RMID); } safe_free(*eventFlag); } } EventFlag_t *eventFlag = NULL;
int safe_atoi(const char *str) { if(!str) { CHECK_LOG(false, "safe atoi, str is null"); fflush(NULL); raise(SIGABRT); } const char *p = str; while(*p) { if(*p > '9' || *p < '0') { CHECK_LOG(false, "safe atoi, not valid char: %c", *p); fflush(NULL); raise(SIGABRT); } p++; } return atoi(str); }
void printFlag(int flag) { logger(LOG_INFO, "printFlag"); int mask = 1; if(mask & flag) logger(LOG_INFO, "%d", 0); for(int cnt = 1; cnt < 32; cnt++) { mask <<= 1; if(mask & flag) logger(LOG_INFO, "%d", cnt); } }
#define printINT(info, x) logger(LOG_INFO, "("#info")."#x": %d", (info).x) #define printUINT(info, x) logger(LOG_INFO, "("#info")."#x": %ld", (info).x) #define printTIME(ds, x) logger(LOG_INFO, "("#ds")."#x": %s", ctime(&(ds).x))
int printInfo(int id) { union semun arg; struct seminfo info; struct semid_ds ds; arg.__buf = &info; CHECK(semctl(id, 0, SEM_INFO, arg) != -1, STRING_MSG); printINT(info, semmap); printINT(info, semmni); printINT(info, semmns); printINT(info, semmnu); printINT(info, semmsl); printINT(info, semopm); printINT(info, semume); printINT(info, semusz); printINT(info, semvmx); printINT(info, semaem); arg.buf = &ds; CHECK(semctl(id, 0, IPC_STAT, arg) != -1, STRING_MSG); printUINT(ds, sem_nsems); printTIME(ds, sem_otime); printTIME(ds, sem_ctime); return 0; }
int wrapPrintFlag(int flag, int (*func)(EventFlag_t *, int)) { int currentFlag = 0; CHECK_LOG(getEventFlag(eventFlag, ¤tFlag) != -1, STRING_MSG); printFlag(currentFlag); CHECK_LOG((*func)(eventFlag, flag) != -1, STRING_MSG); CHECK_LOG(getEventFlag(eventFlag, ¤tFlag) != -1, STRING_MSG); printFlag(currentFlag); return 0; }
#define FLAG_RANGE(flg) ((16 <= (flg) && (flg) < 31) || (0 <= (flg) && (flg) < 15)) #define XXSTRING(s) XSTRING(s) #define XSTRING(s) STRING(s) #define STRING(s) #s int main(int argc, char **argv) { CHECK_EXIT(argc > 1, "Usage: %s destroy|waitAny|waitAll|add|clear|get [flag]", argv[0]); eventFlag = newEventFlag(argv[0], 'x'); CHECK_EXIT(eventFlag != NULL, STRING_MSG); int flag = 0; for(int i = 2; i < argc; i++) { int flg = safe_atoi(argv[i]); if(!FLAG_RANGE(flg)){ CHECK_LOG(false, "Usage: %s", XXSTRING(FLAG_RANGE(flg))); continue; } flag |= (1 << flg); } printInfo(eventFlag->semid); logger(LOG_INFO, "start %s", argv[1]); if(!strcmp(argv[1], "waitAny")) { CHECK_LOG(wrapPrintFlag(flag, waitForAny) != -1, STRING_MSG); } else if(!strcmp(argv[1], "waitAll")) { CHECK_LOG(wrapPrintFlag(flag, waitForAll) != -1, STRING_MSG); } else if(!strcmp(argv[1], "add")) { CHECK_LOG(wrapPrintFlag(flag, setEventFlag) != -1, STRING_MSG); } else if(!strcmp(argv[1], "clear")) { CHECK_LOG(wrapPrintFlag(flag, clearEventFlag) != -1, STRING_MSG); } else if(!strcmp(argv[1], "get")) { CHECK_LOG(getEventFlag(eventFlag, &flag) != -1, STRING_MSG); printFlag(flag); } else if(!strcmp(argv[1], "destroy")) { destroyEventFlag(&eventFlag); } else { CHECK_EXIT(false, "Usage: %s waitAny|waitAll|add|clear|get [flag]", argv[0]); } logger(LOG_INFO, "end %s", argv[1]); }
|