int create_sem(int *sem) { key_t ipckey; union semun sem_union; int s; ipckey = IPC_PRIVATE; s = semget(ipckey, 1, 0666|IPC_CREAT); if(s == -1){ #ifdef DEBUG write_log("semget error\n"); #endif return -1; } /* init semapho=1 */ sem_union.val = 1; if(semctl(s, 0, SETVAL, sem_union) == -1){ /* error */ remove_sem(s); return -1; } /* save semapho id */ *sem = s; return 0; }
int main(int ac, char **arv) { int error; #ifdef DEBUG /* logfile=/var/log/log.txt */ create_log(); fprintf(log_path, "ac = %d\n", ac); fflush(log_path); #endif /* setup signal handlers */ init_signals(); /* printer DEVICE ID and command line option check */ if((error = get_printer_devid()) < 0 || check_arg(ac,arv) < 0){ /* Not Canon printer or "--gui" option not found */ #ifdef DEBUG write_log("Now normal printing ......\n"); #endif print_normal(); exit(0); } /* create semapho and other setup */ if((error = lm_init(PRNT_PATH))){ exit(0); } /* monitor_process/print_process/status_process start */ lm_main_fork(); /* delete semapho */ remove_sem(sem_id); /* free memory (status monitor argv string) */ free_arg(); #ifdef DEBUG write_log("LM end \n"); #endif exit(0); }