int main(int argc, char **argv) { int opt; int cmd = CmdCheck; while((opt = getopt(argc, argv, "+tch")) != -1) { switch(opt) { case 'c': cmd = CmdCheck; break; case 't': cmd = CmdTag; break; case 'h': usage(); return EXIT_SUCCESS; default: LERROR(ExitArgumentError, 0, "unknown option: %c", opt); } } if(optind >= argc) LERROR(ExitArgumentError, 0, "too few arguments. Use the -h flag to obtain usage information."); switch(cmd) { case CmdCheck: return command_check(argv[argc-1]); case CmdTag: return command_tag(argv[argc-1]); } return EXIT_SUCCESS; // not reached }
/* ************************************************************************* * * コマンド分岐関数 * ************************************************************************* */ void exec_command (char command, char *parameter, char *str) { switch (command) { case 'Q': command_quit(parameter); break; case 'C': command_check(parameter); break; case 'P': command_print(profile_data, atoi(parameter), str); break; case 'R': command_read(profile_data, parameter); break; case 'W': command_write(profile_data, parameter); break; case 'F': command_find(profile_data, parameter, str); break; case 'S': command_sort(profile_data, atoi(parameter), str); break; default: printf("Invalid command '%c' was found.\n", command); break; } }
int commandhandler(BIO *cbio, int cl) { BIO *bbody = NULL, *bbase64 = NULL, *bcrypt = NULL; int ret = -1; char buf[100 * 1024]; json_object *config = NULL; unsigned char iv[16]; BIO *bmem = NULL; char *bptr = NULL, *c = NULL; long blen = 0; char *command = NULL; logme(LOGMSG_DEBUG, "commandhandler (cl=%d)", cl); do { if(!(bmem = BIO_new(BIO_s_mem()))) break; if(!(bbody = BIO_new(BIO_s_mem()))) break; if(!(bbase64 = BIO_new(BIO_f_base64()))) break; BIO_set_flags(bbase64, BIO_FLAGS_BASE64_NO_NL); if(!(bcrypt = BIO_new(BIO_f_cipher()))) break; memset(iv, 0x00, sizeof(iv)); BIO_set_cipher(bcrypt, EVP_get_cipherbyname("aes-128-cbc"), (unsigned char *)conf.key, iv, 0); BIO_push(bbase64, bbody); BIO_push(bcrypt, bmem); while(blen < cl) { if((ret = BIO_read(cbio, buf, ((cl - blen) > sizeof(buf)) ? sizeof(buf) : (cl - blen))) <= 0) break; blen += ret; while((c = memchr(buf, '\n', ret)) || (c = memchr(buf, '\r', ret))) memmove(c, c + 1, --ret - (c - buf)); if(BIO_write(bbody, buf, ret) != ret) { logme(LOGMSG_DEBUG, "BIO_write error"); break; } } do { blen = BIO_read(bbase64, buf, sizeof(buf)); if(blen > 0) { BIO_write(bcrypt, buf, blen); } } while(blen > 0); (void)BIO_flush(bcrypt); blen = BIO_get_mem_data(bmem, &bptr); if(!(config = json_tokener_parse(bptr))) break; if(!(command = (char *)json_object_get_string(json_object_object_get(config, "command")))) break; logme(LOGMSG_DEBUG, "command: %s", command); if(!strcasecmp(command, "FORWARD")) { ret = command_forward(config, cbio); } else if(!strcasecmp(command, "CONFIG")) { ret = command_config(config, cbio); } else if(!strcasecmp(command, "UPGRADE")) { ret = command_upgrade(config, cbio); } else if(!strcasecmp(command, "CHECK")) { ret = command_check(config, cbio); } } while(0); if(bbody) BIO_free(bbody); if(bbase64) BIO_free(bbase64); if(bcrypt) BIO_free(bcrypt); if(bmem) BIO_free(bmem); if(config) json_object_put(config); return ret; }
void mainloop(void) { /* * This is the main program loop. Here's where we're doing all the * spawning and the checking. This is the heart of the program. */ int exitcode, status, command = CMD_OK, event = EV_AOK; while (1) { event = event_check(); command = command_check(); command_handle(command, pid); if (command == CMD_OK && event == EV_AOK) pid = fork(); else pid = -2; switch (pid) { case 0: /* we are the child */ vbprintf("(child) Executing %s in work directory %s\n" ,cmd, workdir); chdir(workdir); logging_rotate(); logging_init(); exitcode = execl(cmd, cmd, args, (char *)NULL); if (exitcode < 0) { perror("execl"); } exit(exitcode); break; case -1: /* error */ err(EX_OSERR, "Couldn't fork: "); break; case -2: /* stop */ vbprintf("Not reforking. Waiting %d seconds\n", delay); sleep(delay); break; default: /* we are the parent */ vbprintf("(parent) Forked process with pid %d\n", pid); while (waitpid(pid, &status, WNOHANG) == 0) { /* Do something while child is running */ dbprintf("Waiting for child (%d) to terminate\n", pid); running = TRUE; command = command_check(); command_handle(command, pid); sleep(delay); } /* if we get here, the child process must have died */ running = FALSE; command = command_check(); command_handle(command, pid); break; } if (command == CMD_STOP) { struct stat st; /* check if HALTFILE still exists */ chdir(workdir); if (stat(HALTFILE, &st) < 0) { vbprintf("Haltfile gone. Resuming...\n"); command = CMD_OK; } if (event == EV_AOK) { vbprintf("Event over. Resuming...\n"); command = CMD_OK; } } } return; }
void sequential (int * new_mode, int *ex, char **commands, char ** dir_list){ char **one_command; int i = 0; int special = 0; while (commands[i] != NULL){ one_command = tokenify(commands[i]," \n\t"); if (one_command[0] == NULL) { special = 1; } if (strcmp(one_command[0],"exit") == 0) { *ex=1; } else if ((strcmp(one_command[0], "mode")) == 0) { if (one_command[1] != NULL) { if ((strcmp(one_command[1], "sequential")) == 0 || (strcmp(one_command[1], "s")) == 0) { *new_mode = SEQUENTIAL; } else if ((strcmp(one_command[1], "parallel")) == 0 || (strcmp(one_command[1], "p")) == 0) { *new_mode = PARALLEL; } else { printf("Invalid mode, please try again.\n"); } } else { printf("Current mode: sequential\n"); } special = 1 } else if (strcmp(one_command[0],"jobs") == 0){ printf("No parallel jobs running in sequential mode.\n"); special = 1; } else if (strcmp(one_command[0],"pause")==0){ printf("Pause is not a valid command in sequential mode\n"); special = 1; } else if (strcmp(one_command[0],"resume")==0){ printf("Resume is not a valid command in sequential mode\n"); special = 1; } if (special) { special = 0; free_array(one_command); i++; continue; } struct stat statresult; int rv = stat(one_command[0], &statresult); if (rv < 0){ //stat failed, no such file //find another one and change it rv = command_check(dir_list, &one_command); if (rv < 0){ printf("Invalid command: %s\n", one_command[0]); i++; continue; } } int child_pid = fork(); int child_status; if (child_pid < 0){ fprintf(stderr,"fork failed, could not create child process\n"); } else if (child_pid == 0) { if (execv(one_command[0],one_command)<0){ fprintf(stderr,"Execution failed: %s\n",strerror(errno)); } } else { waitpid(child_pid, &child_status, 0); free_array(one_command); } i++; }