/** * Checks whether a host can be used to start testbed service * * @param host the host to check * @param config the configuration handle to lookup the path of the testbed * helper * @param cb the callback to call to inform about habitability of the given host * @param cb_cls the closure for the callback * @return NULL upon any error or a handle which can be passed to * GNUNET_TESTBED_is_host_habitable_cancel() */ struct GNUNET_TESTBED_HostHabitableCheckHandle * GNUNET_TESTBED_is_host_habitable (const struct GNUNET_TESTBED_Host *host, const struct GNUNET_CONFIGURATION_Handle *config, GNUNET_TESTBED_HostHabitableCallback cb, void *cb_cls) { struct GNUNET_TESTBED_HostHabitableCheckHandle *h; char **rsh_args; char **rsh_suffix_args; char *stat_args[3]; const char *hostname; char *port; h = GNUNET_new (struct GNUNET_TESTBED_HostHabitableCheckHandle); h->cb = cb; h->cb_cls = cb_cls; h->host = host; hostname = (NULL == host->hostname) ? "127.0.0.1" : host->hostname; if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (config, "testbed", "HELPER_BINARY_PATH", &stat_args[1])) stat_args[1] = GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY); GNUNET_asprintf (&port, "%u", host->port); rsh_args = gen_rsh_args (port, hostname, host->username); GNUNET_free (port); port = NULL; stat_args[0] = "stat"; stat_args[2] = NULL; rsh_suffix_args = gen_rsh_suffix_args ((const char **) stat_args); GNUNET_free (stat_args[1]); h->helper_argv = join_argv ((const char **) rsh_args, (const char **) rsh_suffix_args); free_argv (rsh_suffix_args); free_argv (rsh_args); h->auxp = GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR, NULL, NULL, NULL, h->helper_argv[0], h->helper_argv); if (NULL == h->auxp) { GNUNET_break (0); /* Cannot exec SSH? */ GNUNET_free (h); return NULL; } h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time); h->habitability_check_task = GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h); return h; }
int main(int argc, char **argv) { char test1[] = " Hallo \n"; printf("Before: '%s'\n", test1); char *after = prepare(test1); printf("After: '%s'\n", after ? after : "<NULL>"); char test2[] = " \t\t \n"; printf("Before: '%s'\n", test2); after = prepare(test2); printf("After: '%s'\n", after ? after : "<NULL>"); struct cmd cmd = {0, 0}; char line[] = "echo \"test bla\" 'blub\\' diedup'"; split_cmd(line, &cmd); printf("Cmd: \n Argc: %d\n", cmd.argc); int i=0; for (; i < cmd.argc; ++i) { printf(" ->%s\n", cmd.argv[i]); } char *relined = build_cmd(&cmd); printf("Reassembled: '%s'\n", relined); free_argv(&cmd); free(relined); return 0; }
static int alloc_argv(struct fuse_args *args) { char **argv; int i; assert(!args->allocated); argv = calloc(args->argc, sizeof(*argv)); if (argv == NULL) return (-1); if (args->argv) { for (i = 0; i < args->argc; i++) { argv[i] = strdup(args->argv[i]); if (argv[i] == NULL) { free_argv(argv, i + 1); return (-1); } } } args->allocated = 1; args->argv = argv; return (0); }
static int join_hook(int argc, char *argv[]) { assert(argc == 2); /********************************************** * allow something like: * /join #channel1 #channel2 **********************************************/ if (strstr(argv[1], " ")) { int i; char **tmp_argv; int tmp_argc; char *orig = argv[1]; make_argv(&tmp_argc, &tmp_argv, argv[1]); for (i = 0; i < tmp_argc; i++) { argv[1] = tmp_argv[i]; join_hook(argc, argv); } free_argv(tmp_argc, tmp_argv); argv[1] = orig; return 0; } channel_t *tmp; tmp = find_channel(argv[1], irc_pick_server()); if (tmp == NULL) tmp = new_channel(argv[1], irc_pick_server()); tmp->set_window(tmp, active_window <= window_max && active_window > 0 ? active_window : 0); tmp->join(tmp); return 0; }
int main(int ac, char **av, char **env) { int i; char **argv; int return_val; ac = ac; av = av; env = env; print_prompt(); argv = get_argv(); printf("First argument is %s\n", argv[0]); for (i = 0; argv[i] != NULL; i++) { printf("'%s', ", argv[i]); } printf("\n"); return_val = free_argv(argv); printf("The return val is %i\n", return_val); /* execve(argv[0], argv, env); */ return 1; }
void aff_tty(char **tab, int len) { int len_total; int len_total_divided; int *tab_space; int *most_space_tab; int divid; t_ws col_len; len_total = calc_len(tab, len); tab_space = calc_tab_space(tab, len); divid = 1; most_space_tab = find_most_len(tab_space, divid, len); len_total_divided = sum_space(most_space_tab, len / divid); xioctl(0, TIOCGWINSZ, &col_len); while (len_total_divided >= col_len.ws_col) { free(most_space_tab); divid++; most_space_tab = find_most_len(tab_space, divid, len); len_total_divided = sum_space(most_space_tab, len / divid); } aff_divided(tab, divid, len, most_space_tab); free(most_space_tab); free(tab_space); free_argv(tab); }
void shell(void) { char *command = malloc(SHELL_BUF_MAX+1); int n = -1; int argc; char **argv; printf("%s", SHELL_PROMPT); while (1) { while ((command[++n] = getc()) != '\n' && command[n] != '\r' && n < (SHELL_BUF_MAX-1)) { if (command[n] == '\b' || command[n] == 0x7F) { if (n) { puts(BACKSPACE); n = n - 2; } else { n--; } } else { putc(command[n]); } } if (command[n] == '\n' || command[n] == '\r') { command[n] = '\0'; } else { command[n+1] = '\0'; } printf("\r\n"); if (n >= (SHELL_BUF_MAX-1)) { printf("Line too long, max length is %d.\r\n%s", SHELL_BUF_MAX, SHELL_PROMPT); n = -1; continue; } parse_command(command, &argc, &argv); if (argc < 0) { /* Something bad happened, argv has already been freed */ printf("%s: could not parse input\r\n", command); } else { run_command(command, argc, argv); free_argv(argc, &argv); } printf("%s", SHELL_PROMPT); n = -1; } free(command); }
int strtoargv(const char * const args_str, char **argv_new, int *argc_new, const fko_srv_options_t * const opts) { int current_arg_ctr = 0, i; char arg_tmp[MAX_LINE_LEN] = {0}; for (i=0; i < (int)strlen(args_str); i++) { if (!isspace(args_str[i])) { arg_tmp[current_arg_ctr] = args_str[i]; current_arg_ctr++; } else { if(current_arg_ctr > 0) { arg_tmp[current_arg_ctr] = '\0'; if (add_argv(argv_new, argc_new, arg_tmp, opts) != 1) { free_argv(argv_new, argc_new); return 0; } current_arg_ctr = 0; } } } /* pick up the last argument in the string */ if(current_arg_ctr > 0) { arg_tmp[current_arg_ctr] = '\0'; if (add_argv(argv_new, argc_new, arg_tmp, opts) != 1) { free_argv(argv_new, argc_new); return 0; } } return 1; }
void fuse_opt_free_args(struct fuse_args *args) { if (!args->allocated) return; free_argv(args->argv, args->argc); args->argv = 0; args->argc = 0; args->allocated = 0; }
void free_rtn_tuples(client *cl) { for (cl->ntuples--; cl->ntuples >= 0; cl->ntuples--) free_argv(cl->tuples[cl->ntuples].mr_argv, cl->tuples[cl->ntuples].mr_argc); free(cl->tuples); cl->tuples = xmalloc(sizeof(mr_params)); cl->tuplessize = 1; cl->ntuples = cl->nexttuple = 0; }
/** * Function to cancel a request started using GNUNET_TESTBED_is_host_habitable() * * @param handle the habitability check handle */ void GNUNET_TESTBED_is_host_habitable_cancel (struct GNUNET_TESTBED_HostHabitableCheckHandle *handle) { GNUNET_SCHEDULER_cancel (handle->habitability_check_task); (void) GNUNET_OS_process_kill (handle->auxp, GNUNET_TERM_SIG); (void) GNUNET_OS_process_wait (handle->auxp); GNUNET_OS_process_destroy (handle->auxp); free_argv (handle->helper_argv); GNUNET_free (handle); }
errno_t uu_proc_setargs( int pid, const char **av, const char **env ) { errno_t r = 0; hal_mutex_lock(&proc_lock); uuprocess_t * u = proc_by_pid(pid); if( u ) { if(u->argv) free_argv(u->argv); if(u->envp) free_argv(u->envp); u->argv = dup_argv( &u->argc, av, 0 ); u->envp = dup_argv( 0, env, 0 ); flatten_argv( u->cmd, MAX_UU_CMD, u->argv ); } else r = ESRCH; hal_mutex_unlock(&proc_lock); return r; }
static void monitoredProcessDied(ProcTrack* p, int status, int signo , int exitcode, int waslogged) { pid_t new_pid; char ** client_argv = (char **) p->privatedata; const char * pname = p->ops->proctype(p); if (shutting_down) { cl_respawn_quit(SIGTERM, NULL); p->privatedata = NULL; return; } if ( exitcode == MAGIC_EXIT_CODE) { cl_log(LOG_INFO, "Don't restart the monitored program" " %s [%d], since we got the magic exit code." , pname, p->pid); free_argv(client_argv); cl_respawn_quit(SIGTERM, NULL); /* Does NOT always exit */ return; } cl_log(LOG_INFO, "process %s[%d] exited, and its exit code is %d" , pname, p->pid, exitcode); if ( 0 < (new_pid = run_client_as_child(client_argv)) ) { cl_log(LOG_NOTICE, "restarted the monitored program, whose PID " " is %d", new_pid); } else { cl_log(LOG_ERR, "Failed to restart the monitored program %s ," "will exit.", pname ); free_argv(client_argv); cl_respawn_quit(SIGTERM, NULL); /* Does NOT always exit */ return; } p->privatedata = NULL; }
/** * Cleans-up the controller's helper process handle * * @param cproc the handle to the controller's helper process */ void GNUNET_TESTBED_controller_destroy_ (struct GNUNET_TESTBED_ControllerProc *cproc) { if (NULL != cproc->helper) { GNUNET_break (GNUNET_OK == GNUNET_HELPER_wait (cproc->helper)); GNUNET_HELPER_destroy (cproc->helper); } if (NULL != cproc->helper_argv) free_argv (cproc->helper_argv); cproc->host->controller_started = GNUNET_NO; cproc->host->locked = GNUNET_NO; GNUNET_free_non_null (cproc->msg); GNUNET_free (cproc); }
void daemon_stop(void) { if(restart && progname && saved_argv) { sleep(1); syslog(LOG_USER | LOG_CRIT, "%s to be restarted\n", _NS_); execv(progname, saved_argv); get_progname(); if(progname) execv(progname, saved_argv); } free_argv(); free(progname); progname = NULL; }
int main (void) { int argc; char **argv; char **envp; /* These variables are used by the WEL library */ ghInstance = GetModuleHandle(NULL); eif_hInstance = GetModuleHandle(NULL); eif_hPrevInstance = NULL; eif_lpCmdLine = GetCommandLine(); eif_nCmdShow = SW_SHOW; /* Initialization of the command line which is going to be passed to eiffel */ get_argcargv (&argc, &argv); /* We get ANSI version of environment variables */ envp = (char **) GetEnvironmentStringsA(); eif_alloc_init(); #ifdef EIF_THREADS eif_thr_init_root(); #endif { GTCX struct ex_vect *exvect; jmp_buf exenv; egc_init_plug(); initsig(); initstk(); exvect = exset((char *) 0, 0, (char *) 0); exvect->ex_jbuf = &exenv; if (setjmp(exenv)) failure(); eif_retrieve_root (&argc, argv); eif_rtinit(argc, argv, envp); eif_init_root(); egc_rcdt_init(); emain(argc, argv); free_argv (&argv); reclaim(); } FreeEnvironmentStringsA ((LPSTR) envp); exit(0); return 0; }
void daemon_stop(void) { if (!backgd_mode) printf ("Server stopping...\n"); if (restart && prog_name && saved_argv) { WARN_LOG ("%s", "Server restarting..."); chdir (current_dir); execv (prog_name, saved_argv); WARN_LOG ("%s", "Restart Failed..."); } free_argv (); free (prog_name); prog_name = NULL; }
void execnt_unit_test() { #if !defined(NDEBUG) /* vc6 preprocessor is broken, so assert with these strings gets * confused. Use a table instead. */ typedef struct test { char* command; int result; } test; test tests[] = { { "x", 0 }, { "x\n ", 0 }, { "x\ny", 1 }, { "x\n\n y", 1 }, { "echo x > foo.bar", 1 }, { "echo x < foo.bar", 1 }, { "echo x \">\" foo.bar", 0 }, { "echo x \"<\" foo.bar", 0 }, { "echo x \\\">\\\" foo.bar", 1 }, { "echo x \\\"<\\\" foo.bar", 1 } }; int i; for ( i = 0; i < sizeof(tests)/sizeof(*tests); ++i) { assert( !can_spawn( tests[i].command ) == tests[i].result ); } { char* long_command = malloc(MAXLINE + 10); assert( long_command != 0 ); memset( long_command, 'x', MAXLINE + 9 ); long_command[MAXLINE + 9] = 0; assert( can_spawn( long_command ) == MAXLINE + 9); free( long_command ); } { /* Work around vc6 bug; it doesn't like escaped string * literals inside assert */ char** argv = string_to_args("\"g++\" -c -I\"Foobar\""); char const expected[] = "-c -I\"Foobar\""; assert(!strcmp(argv[0], "g++")); assert(!strcmp(argv[1], expected)); free_argv(argv); } #endif }
/* Get the command line arguments from the previous invocation */ static int run_last_args(fko_cli_options_t *options, const char * const args_save_file) { FILE *args_file_ptr = NULL; int argc_new = 0, args_broken = 0; char args_str[MAX_LINE_LEN] = {0}; char *argv_new[MAX_CMDLINE_ARGS]; /* should be way more than enough */ memset(argv_new, 0x0, sizeof(argv_new)); if(verify_file_perms_ownership(args_save_file) != 1) return 0; if ((args_file_ptr = fopen(args_save_file, "r")) == NULL) { log_msg(LOG_VERBOSITY_ERROR, "Could not open args file: %s", args_save_file); return 0; } if ((fgets(args_str, MAX_LINE_LEN, args_file_ptr)) != NULL) { args_str[MAX_LINE_LEN-1] = '\0'; if (options->verbose) log_msg(LOG_VERBOSITY_NORMAL, "Executing: %s", args_str); if(strtoargv(args_str, argv_new, &argc_new, options) != 1) { args_broken = 1; } } fclose(args_file_ptr); if(args_broken) return 0; /* Reset the options index so we can run through them again. */ optind = 0; config_init(options, argc_new, argv_new); /* Since we passed in our own copies, free up malloc'd memory */ free_argv(argv_new, &argc_new); return 1; }
/* remote: partie 1 bis | write //usr/bin/netcat -ltp6666 -e///bin//sh in .data */ void x86_makecode(t_gadget *gadgets, size_t word_size) { int argv_start; int envp_start; t_rop_writer wr; char *second_reg; char reg_stack[32] = "pop %"; /* Whatever register we use to point to .data */ char **argv; size_t i; wr.mov = &gadgets[0]; wr.pop_data = &gadgets[1]; second_reg = get_reg(wr.mov->gadget->instruction, 0); strncat(reg_stack, second_reg, 3); free(second_reg); for (i = 1; i <= 4; i++) if (!strcmp(reg_stack, gadgets[i].inst)) { wr.pop_target = &gadgets[i]; break; } wr.zero_data = &gadgets[5]; if (word_size == 8 && wr.zero_data->gadget == NULL) wr.zero_data = &gadgets[6]; argv = get_argv(); sc_print_pre_init(); sc_print_comment("execve generated by " ROPGADGET_VERSION); sc_print_init(); sc_print_argv((const char * const *)argv, &wr, 0, TRUE, word_size, &argv_start, &envp_start); free_argv(argv); x86_makepartie2(gadgets, argv_start, envp_start, word_size); sc_print_end(); }
/** * Task for checking whether a host is habitable or not * * @param cls GNUNET_TESTBED_HostHabitableCheckHandle * @param tc the scheduler task context */ static void habitability_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { struct GNUNET_TESTBED_HostHabitableCheckHandle *h = cls; void *cb_cls; GNUNET_TESTBED_HostHabitableCallback cb; const struct GNUNET_TESTBED_Host *host; unsigned long code; enum GNUNET_OS_ProcessStatusType type; int ret; h->habitability_check_task = NULL; ret = GNUNET_OS_process_status (h->auxp, &type, &code); if (GNUNET_SYSERR == ret) { GNUNET_break (0); ret = GNUNET_NO; goto call_cb; } if (GNUNET_NO == ret) { h->wait_time = GNUNET_TIME_STD_BACKOFF (h->wait_time); h->habitability_check_task = GNUNET_SCHEDULER_add_delayed (h->wait_time, &habitability_check, h); return; } GNUNET_OS_process_destroy (h->auxp); h->auxp = NULL; ret = (0 != code) ? GNUNET_NO : GNUNET_YES; call_cb: if (NULL != h->auxp) GNUNET_OS_process_destroy (h->auxp); cb = h->cb; cb_cls = h->cb_cls; host = h->host; free_argv (h->helper_argv); GNUNET_free (h); if (NULL != cb) cb (cb_cls, host, ret); }
void free_fifo(t_fifo *fifo) { t_fifo_elem *tmp; t_fifo_elem *next; tmp = NULL; if (fifo != NULL) tmp = fifo->cur; while (tmp != NULL) { next = tmp->next; if (tmp->down != NULL) { fifo->cur = tmp->down; free_fifo(fifo); } free_argv(tmp->data.argv); free(tmp); tmp = next; } }
// ********************************************************************** // system kill task // int sysKillTask(int taskId) { Semaphore* sem = semaphoreList; Semaphore** semLink = &semaphoreList; // assert that you are not pulling the rug out from under yourself! assert("sysKillTask Error" && tcb[taskId].name && superMode); printf("\nKill Task %s", tcb[taskId].name); // signal task terminated semSignal(taskSems[taskId]); // look for any semaphores created by this task while(sem = *semLink) { DeQ(sem->q,taskId); if(sem->taskNum == taskId) { // semaphore found, delete from list, release memory deleteSemaphore(semLink); } else { // move to next semaphore semLink = (Semaphore**)&sem->semLink; } } // ?? delete task from system queues // printQ(rq); DeQ(rq,taskId); // printQ(rq); free_argv(tcb[taskId].argv,tcb[taskId].argc); free(tcb[taskId].name); free(tcb[taskId].stack); tcb[taskId].name = 0; // release tcb slot return 0; } // end sysKillTask
int main(int argc, char *argv[]) { ip6tc_handle_t handle; char buffer[10240]; unsigned int line = 0; int c; char curtable[IP6T_TABLE_MAXNAMELEN + 1]; FILE *in; const char *modprobe = 0; program_name = "ip6tables-restore"; program_version = IPTABLES_VERSION; #ifdef NO_SHARED_LIBS init_extensions(); #endif while ((c = getopt_long(argc, argv, "bcvhnM:", options, NULL)) != -1) { switch (c) { case 'b': binary = 1; break; case 'c': counters = 1; break; case 'v': verbose = 1; break; case 'h': print_usage("ip6tables-restore", IPTABLES_VERSION); break; case 'n': noflush = 1; break; case 'M': modprobe = optarg; break; } } if (optind == argc - 1) { in = fopen(argv[optind], "r"); if (!in) { fprintf(stderr, "Can't open %s: %s", argv[optind], strerror(errno)); exit(1); } } else if (optind < argc) { fprintf(stderr, "Unknown arguments found on commandline"); exit(1); } else in = stdin; /* Grab standard input. */ while (fgets(buffer, sizeof(buffer), in)) { int ret; line++; if (buffer[0] == '\n') continue; else if (buffer[0] == '#') { if (verbose) fputs(buffer, stdout); continue; } else if (strcmp(buffer, "COMMIT\n") == 0) { DEBUGP("Calling commit\n"); ret = ip6tc_commit(&handle); } else if (buffer[0] == '*') { /* New table */ char *table; table = strtok(buffer+1, " \t\n"); DEBUGP("line %u, table '%s'\n", line, table); if (!table) { exit_error(PARAMETER_PROBLEM, "%s: line %u table name invalid\n", program_name, line); exit(1); } strncpy(curtable, table, IP6T_TABLE_MAXNAMELEN); handle = create_handle(table, modprobe); if (noflush == 0) { DEBUGP("Cleaning all chains of table '%s'\n", table); for_each_chain(flush_entries, verbose, 1, &handle); DEBUGP("Deleting all user-defined chains " "of table '%s'\n", table); for_each_chain(delete_chain, verbose, 0, &handle) ; } ret = 1; } else if (buffer[0] == ':') { /* New chain. */ char *policy, *chain; chain = strtok(buffer+1, " \t\n"); DEBUGP("line %u, chain '%s'\n", line, chain); if (!chain) { exit_error(PARAMETER_PROBLEM, "%s: line %u chain name invalid\n", program_name, line); exit(1); } if (!ip6tc_builtin(chain, handle)) { DEBUGP("Creating new chain '%s'\n", chain); if (!ip6tc_create_chain(chain, &handle)) exit_error(PARAMETER_PROBLEM, "error creating chain " "'%s':%s\n", chain, strerror(errno)); } policy = strtok(NULL, " \t\n"); DEBUGP("line %u, policy '%s'\n", line, policy); if (!policy) { exit_error(PARAMETER_PROBLEM, "%s: line %u policy invalid\n", program_name, line); exit(1); } if (strcmp(policy, "-") != 0) { struct ip6t_counters count; if (counters) { char *ctrs; ctrs = strtok(NULL, " \t\n"); parse_counters(ctrs, &count); } else { memset(&count, 0, sizeof(struct ip6t_counters)); } DEBUGP("Setting policy of chain %s to %s\n", chain, policy); if (!ip6tc_set_policy(chain, policy, &count, &handle)) exit_error(OTHER_PROBLEM, "Can't set policy `%s'" " on `%s' line %u: %s\n", chain, policy, line, ip6tc_strerror(errno)); } ret = 1; } else { int a; char *ptr = buffer; char *pcnt = NULL; char *bcnt = NULL; char *parsestart; /* the parser */ char *param_start, *curchar; int quote_open; /* reset the newargv */ newargc = 0; if (buffer[0] == '[') { /* we have counters in our input */ ptr = strchr(buffer, ']'); if (!ptr) exit_error(PARAMETER_PROBLEM, "Bad line %u: need ]\n", line); pcnt = strtok(buffer+1, ":"); if (!pcnt) exit_error(PARAMETER_PROBLEM, "Bad line %u: need :\n", line); bcnt = strtok(NULL, "]"); if (!bcnt) exit_error(PARAMETER_PROBLEM, "Bad line %u: need ]\n", line); /* start command parsing after counter */ parsestart = ptr + 1; } else { /* start command parsing at start of line */ parsestart = buffer; } add_argv(argv[0]); add_argv("-t"); add_argv((char *) &curtable); if (counters && pcnt && bcnt) { add_argv("--set-counters"); add_argv((char *) pcnt); add_argv((char *) bcnt); } /* After fighting with strtok enough, here's now * a 'real' parser. According to Rusty I'm now no * longer a real hacker, but I can live with that */ quote_open = 0; param_start = parsestart; for (curchar = parsestart; *curchar; curchar++) { if (*curchar == '"') { if (quote_open) { quote_open = 0; *curchar = ' '; } else { quote_open = 1; param_start++; } } if (*curchar == ' ' || *curchar == '\t' || * curchar == '\n') { char param_buffer[1024]; int param_len = curchar-param_start; if (quote_open) continue; if (!param_len) { /* two spaces? */ param_start++; continue; } /* end of one parameter */ strncpy(param_buffer, param_start, param_len); *(param_buffer+param_len) = '\0'; /* check if table name specified */ if (!strncmp(param_buffer, "-t", 3) || !strncmp(param_buffer, "--table", 8)) { exit_error(PARAMETER_PROBLEM, "Line %u seems to have a " "-t table option.\n", line); exit(1); } add_argv(param_buffer); param_start += param_len + 1; } else { /* regular character, skip */ } } DEBUGP("calling do_command6(%u, argv, &%s, handle):\n", newargc, curtable); for (a = 0; a < newargc; a++) DEBUGP("argv[%u]: %s\n", a, newargv[a]); ret = do_command6(newargc, newargv, &newargv[2], &handle); free_argv(); } if (!ret) { fprintf(stderr, "%s: line %u failed\n", program_name, line); exit(1); } } return 0; }
int main(int argc, char *argv[]) #endif { struct ip6tc_handle *handle = NULL; char buffer[10240]; int c; char curtable[IP6T_TABLE_MAXNAMELEN + 1]; FILE *in; int in_table = 0, testing = 0; line = 0; ip6tables_globals.program_name = "ip6tables-restore"; c = xtables_init_all(&ip6tables_globals, NFPROTO_IPV6); if (c < 0) { fprintf(stderr, "%s/%s Failed to initialize xtables\n", ip6tables_globals.program_name, ip6tables_globals.program_version); exit(1); } #if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS) init_extensions(); init_extensions6(); #endif while ((c = getopt_long(argc, argv, "bcvthnM:", options, NULL)) != -1) { switch (c) { case 'b': binary = 1; break; case 'c': counters = 1; break; case 'v': verbose = 1; break; case 't': testing = 1; break; case 'h': print_usage("ip6tables-restore", IPTABLES_VERSION); break; case 'n': noflush = 1; break; case 'M': xtables_modprobe_program = optarg; break; } } if (optind == argc - 1) { in = fopen(argv[optind], "re"); if (!in) { fprintf(stderr, "Can't open %s: %s\n", argv[optind], strerror(errno)); exit(1); } } else if (optind < argc) { fprintf(stderr, "Unknown arguments found on commandline\n"); exit(1); } else in = stdin; /* Grab standard input. */ while (fgets(buffer, sizeof(buffer), in)) { int ret = 0; line++; if (buffer[0] == '\n') continue; else if (buffer[0] == '#') { if (verbose) fputs(buffer, stdout); continue; } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) { if (!testing) { DEBUGP("Calling commit\n"); ret = ip6tc_commit(handle); ip6tc_free(handle); handle = NULL; } else { DEBUGP("Not calling commit, testing\n"); ret = 1; } in_table = 0; } else if ((buffer[0] == '*') && (!in_table)) { /* New table */ char *table; table = strtok(buffer+1, " \t\n"); DEBUGP("line %u, table '%s'\n", line, table); if (!table) { xtables_error(PARAMETER_PROBLEM, "%s: line %u table name invalid\n", ip6tables_globals.program_name, line); exit(1); } strncpy(curtable, table, IP6T_TABLE_MAXNAMELEN); curtable[IP6T_TABLE_MAXNAMELEN] = '\0'; if (handle) ip6tc_free(handle); handle = create_handle(table); if (noflush == 0) { DEBUGP("Cleaning all chains of table '%s'\n", table); for_each_chain6(flush_entries6, verbose, 1, handle); DEBUGP("Deleting all user-defined chains " "of table '%s'\n", table); for_each_chain6(delete_chain6, verbose, 0, handle); } ret = 1; in_table = 1; } else if ((buffer[0] == ':') && (in_table)) { /* New chain. */ char *policy, *chain; chain = strtok(buffer+1, " \t\n"); DEBUGP("line %u, chain '%s'\n", line, chain); if (!chain) { xtables_error(PARAMETER_PROBLEM, "%s: line %u chain name invalid\n", ip6tables_globals.program_name, line); exit(1); } if (strlen(chain) >= XT_EXTENSION_MAXNAMELEN) xtables_error(PARAMETER_PROBLEM, "Invalid chain name `%s' " "(%u chars max)", chain, XT_EXTENSION_MAXNAMELEN - 1); if (ip6tc_builtin(chain, handle) <= 0) { if (noflush && ip6tc_is_chain(chain, handle)) { DEBUGP("Flushing existing user defined chain '%s'\n", chain); if (!ip6tc_flush_entries(chain, handle)) xtables_error(PARAMETER_PROBLEM, "error flushing chain " "'%s':%s\n", chain, strerror(errno)); } else { DEBUGP("Creating new chain '%s'\n", chain); if (!ip6tc_create_chain(chain, handle)) xtables_error(PARAMETER_PROBLEM, "error creating chain " "'%s':%s\n", chain, strerror(errno)); } } policy = strtok(NULL, " \t\n"); DEBUGP("line %u, policy '%s'\n", line, policy); if (!policy) { xtables_error(PARAMETER_PROBLEM, "%s: line %u policy invalid\n", ip6tables_globals.program_name, line); exit(1); } if (strcmp(policy, "-") != 0) { struct ip6t_counters count; if (counters) { char *ctrs; ctrs = strtok(NULL, " \t\n"); if (!ctrs || !parse_counters(ctrs, &count)) xtables_error(PARAMETER_PROBLEM, "invalid policy counters " "for chain '%s'\n", chain); } else { memset(&count, 0, sizeof(struct ip6t_counters)); } DEBUGP("Setting policy of chain %s to %s\n", chain, policy); if (!ip6tc_set_policy(chain, policy, &count, handle)) xtables_error(OTHER_PROBLEM, "Can't set policy `%s'" " on `%s' line %u: %s\n", policy, chain, line, ip6tc_strerror(errno)); } ret = 1; } else if (in_table) { int a; char *ptr = buffer; char *pcnt = NULL; char *bcnt = NULL; char *parsestart; /* the parser */ char *curchar; int quote_open, escaped; size_t param_len; /* reset the newargv */ newargc = 0; if (buffer[0] == '[') { /* we have counters in our input */ ptr = strchr(buffer, ']'); if (!ptr) xtables_error(PARAMETER_PROBLEM, "Bad line %u: need ]\n", line); pcnt = strtok(buffer+1, ":"); if (!pcnt) xtables_error(PARAMETER_PROBLEM, "Bad line %u: need :\n", line); bcnt = strtok(NULL, "]"); if (!bcnt) xtables_error(PARAMETER_PROBLEM, "Bad line %u: need ]\n", line); /* start command parsing after counter */ parsestart = ptr + 1; } else { /* start command parsing at start of line */ parsestart = buffer; } add_argv(argv[0]); add_argv("-t"); add_argv(curtable); if (counters && pcnt && bcnt) { add_argv("--set-counters"); add_argv((char *) pcnt); add_argv((char *) bcnt); } /* After fighting with strtok enough, here's now * a 'real' parser. According to Rusty I'm now no * longer a real hacker, but I can live with that */ quote_open = 0; escaped = 0; param_len = 0; for (curchar = parsestart; *curchar; curchar++) { char param_buffer[1024]; if (quote_open) { if (escaped) { param_buffer[param_len++] = *curchar; escaped = 0; continue; } else if (*curchar == '\\') { escaped = 1; continue; } else if (*curchar == '"') { quote_open = 0; *curchar = ' '; } else { param_buffer[param_len++] = *curchar; continue; } } else { if (*curchar == '"') { quote_open = 1; continue; } } if (*curchar == ' ' || *curchar == '\t' || * curchar == '\n') { if (!param_len) { /* two spaces? */ continue; } param_buffer[param_len] = '\0'; /* check if table name specified */ if (!strncmp(param_buffer, "-t", 2) || !strncmp(param_buffer, "--table", 8)) { xtables_error(PARAMETER_PROBLEM, "Line %u seems to have a " "-t table option.\n", line); exit(1); } add_argv(param_buffer); param_len = 0; } else { /* regular character, copy to buffer */ param_buffer[param_len++] = *curchar; if (param_len >= sizeof(param_buffer)) xtables_error(PARAMETER_PROBLEM, "Parameter too long!"); } } DEBUGP("calling do_command6(%u, argv, &%s, handle):\n", newargc, curtable); for (a = 0; a < newargc; a++) DEBUGP("argv[%u]: %s\n", a, newargv[a]); ret = do_command6(newargc, newargv, &newargv[2], &handle); free_argv(); fflush(stdout); } if (!ret) { fprintf(stderr, "%s: line %u failed\n", ip6tables_globals.program_name, line); exit(1); } } if (in_table) { fprintf(stderr, "%s: COMMIT expected at line %u\n", ip6tables_globals.program_name, line + 1); exit(1); } if (in != NULL) fclose(in); return 0; }
/** * Starts a controller process at the given host. The given host's configration * is used as a Template configuration to use for the remote controller; the * remote controller will be started with a slightly modified configuration * (port numbers, unix domain sockets and service home values are changed as per * TESTING library on the remote host). The modified configuration replaces the * host's existing configuration before signalling success through the * GNUNET_TESTBED_ControllerStatusCallback() * * @param trusted_ip the ip address of the controller which will be set as TRUSTED * HOST(all connections form this ip are permitted by the testbed) when * starting testbed controller at host. This can either be a single ip * address or a network address in CIDR notation. * @param host the host where the controller has to be started. CANNOT be NULL. * @param cb function called when the controller is successfully started or * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be * called if cb is called with GNUNET_SYSERR as status. Will never be * called in the same task as 'GNUNET_TESTBED_controller_start' * (synchronous errors will be signalled by returning NULL). This * parameter cannot be NULL. * @param cls closure for above callbacks * @return the controller process handle, NULL on errors */ struct GNUNET_TESTBED_ControllerProc * GNUNET_TESTBED_controller_start (const char *trusted_ip, struct GNUNET_TESTBED_Host *host, GNUNET_TESTBED_ControllerStatusCallback cb, void *cls) { struct GNUNET_TESTBED_ControllerProc *cp; struct GNUNET_TESTBED_HelperInit *msg; const struct GNUNET_CONFIGURATION_Handle *cfg; const char *hostname; static char *const binary_argv[] = { HELPER_TESTBED_BINARY, NULL }; GNUNET_assert (NULL != host); GNUNET_assert (NULL != (cfg = GNUNET_TESTBED_host_get_cfg_ (host))); hostname = NULL; API_VIOLATION (GNUNET_NO == host->locked, "Host is already locked by a previous call to GNUNET_TESTBED_controller_start()"); host->locked = GNUNET_YES; API_VIOLATION (GNUNET_NO == host->controller_started, "Attempting to start a controller on a host which is already started a controller"); cp = GNUNET_new (struct GNUNET_TESTBED_ControllerProc); if (0 == GNUNET_TESTBED_host_get_id_ (host)) { cp->helper = GNUNET_HELPER_start (GNUNET_YES, HELPER_TESTBED_BINARY, binary_argv, &helper_mst, &helper_exp_cb, cp); } else { char *helper_binary_path_args[2]; char **rsh_args; char **rsh_suffix_args; const char *username; char *port; char *argstr; char *aux; unsigned int cnt; username = host->username; hostname = host->hostname; GNUNET_asprintf (&port, "%u", host->port); LOG_DEBUG ("Starting remote connection to destination %s\n", hostname); if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "testbed", "HELPER_BINARY_PATH", &helper_binary_path_args[0])) helper_binary_path_args[0] = GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY); helper_binary_path_args[1] = NULL; rsh_args = gen_rsh_args (port, hostname, username); rsh_suffix_args = gen_rsh_suffix_args ((const char **) helper_binary_path_args); cp->helper_argv = join_argv ((const char **) rsh_args, (const char **) rsh_suffix_args); free_argv (rsh_args); free_argv (rsh_suffix_args); GNUNET_free (port); argstr = GNUNET_strdup (""); for (cnt = 0; NULL != cp->helper_argv[cnt]; cnt++) { aux = argstr; GNUNET_assert (0 < GNUNET_asprintf (&argstr, "%s %s", aux, cp->helper_argv[cnt])); GNUNET_free (aux); } LOG_DEBUG ("Helper cmd str: %s\n", argstr); GNUNET_free (argstr); cp->helper = GNUNET_HELPER_start (GNUNET_NO, cp->helper_argv[0], cp->helper_argv, &helper_mst, &helper_exp_cb, cp); GNUNET_free (helper_binary_path_args[0]); } if (NULL == cp->helper) { if (NULL != cp->helper_argv) free_argv (cp->helper_argv); GNUNET_free (cp); return NULL; } cp->host = host; cp->cb = cb; cp->cls = cls; msg = GNUNET_TESTBED_create_helper_init_msg_ (trusted_ip, hostname, cfg); cp->msg = &msg->header; cp->shandle = GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO, &clear_msg, cp); if (NULL == cp->shandle) { GNUNET_free (msg); GNUNET_TESTBED_controller_stop (cp); return NULL; } return cp; }
int iptables_restore_main(int argc, char *argv[]) { struct xtc_handle *handle = NULL; char buffer[10240]; int c; char curtable[XT_TABLE_MAXNAMELEN + 1]; FILE *in; int in_table = 0, testing = 0; const char *tablename = NULL; const struct xtc_ops *ops = &iptc_ops; line = 0; iptables_globals.program_name = "iptables-restore"; c = xtables_init_all(&iptables_globals, NFPROTO_IPV4); if (c < 0) { fprintf(stderr, "%s/%s Failed to initialize xtables\n", iptables_globals.program_name, iptables_globals.program_version); exit(1); } #if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS) init_extensions(); init_extensions4(); #endif while ((c = getopt_long(argc, argv, "bcvthnM:T:", options, NULL)) != -1) { switch (c) { case 'b': binary = 1; break; case 'c': counters = 1; break; case 'v': verbose = 1; break; case 't': testing = 1; break; case 'h': print_usage("iptables-restore", IPTABLES_VERSION); break; case 'n': noflush = 1; break; case 'M': xtables_modprobe_program = optarg; break; case 'T': tablename = optarg; break; } } if (optind == argc - 1) { in = fopen(argv[optind], "re"); if (!in) { fprintf(stderr, "Can't open %s: %s\n", argv[optind], strerror(errno)); exit(1); } } else if (optind < argc) { fprintf(stderr, "Unknown arguments found on commandline\n"); exit(1); } else in = stdin; /* Grab standard input. */ while (fgets(buffer, sizeof(buffer), in)) { int ret = 0; line++; if (buffer[0] == '\n') continue; else if (buffer[0] == '#') { if (verbose) fputs(buffer, stdout); continue; } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) { if (!testing) { DEBUGP("Calling commit\n"); ret = ops->commit(handle); ops->free(handle); handle = NULL; } else { DEBUGP("Not calling commit, testing\n"); ret = 1; } in_table = 0; } else if ((buffer[0] == '*') && (!in_table)) { /* New table */ char *table; table = strtok(buffer+1, " \t\n"); DEBUGP("line %u, table '%s'\n", line, table); if (!table) { xtables_error(PARAMETER_PROBLEM, "%s: line %u table name invalid\n", xt_params->program_name, line); exit(1); } strncpy(curtable, table, XT_TABLE_MAXNAMELEN); curtable[XT_TABLE_MAXNAMELEN] = '\0'; if (tablename && (strcmp(tablename, table) != 0)) continue; if (handle) ops->free(handle); handle = create_handle(table); if (noflush == 0) { DEBUGP("Cleaning all chains of table '%s'\n", table); for_each_chain4(flush_entries4, verbose, 1, handle); DEBUGP("Deleting all user-defined chains " "of table '%s'\n", table); for_each_chain4(delete_chain4, verbose, 0, handle); } ret = 1; in_table = 1; } else if ((buffer[0] == ':') && (in_table)) { /* New chain. */ char *policy, *chain; chain = strtok(buffer+1, " \t\n"); DEBUGP("line %u, chain '%s'\n", line, chain); if (!chain) { xtables_error(PARAMETER_PROBLEM, "%s: line %u chain name invalid\n", xt_params->program_name, line); exit(1); } if (strlen(chain) >= XT_EXTENSION_MAXNAMELEN) xtables_error(PARAMETER_PROBLEM, "Invalid chain name `%s' " "(%u chars max)", chain, XT_EXTENSION_MAXNAMELEN - 1); if (ops->builtin(chain, handle) <= 0) { if (noflush && ops->is_chain(chain, handle)) { DEBUGP("Flushing existing user defined chain '%s'\n", chain); if (!ops->flush_entries(chain, handle)) xtables_error(PARAMETER_PROBLEM, "error flushing chain " "'%s':%s\n", chain, strerror(errno)); } else { DEBUGP("Creating new chain '%s'\n", chain); if (!ops->create_chain(chain, handle)) xtables_error(PARAMETER_PROBLEM, "error creating chain " "'%s':%s\n", chain, strerror(errno)); } } policy = strtok(NULL, " \t\n"); DEBUGP("line %u, policy '%s'\n", line, policy); if (!policy) { xtables_error(PARAMETER_PROBLEM, "%s: line %u policy invalid\n", xt_params->program_name, line); exit(1); } if (strcmp(policy, "-") != 0) { struct xt_counters count; if (counters) { char *ctrs; ctrs = strtok(NULL, " \t\n"); if (!ctrs || !parse_counters(ctrs, &count)) xtables_error(PARAMETER_PROBLEM, "invalid policy counters " "for chain '%s'\n", chain); } else { memset(&count, 0, sizeof(count)); } DEBUGP("Setting policy of chain %s to %s\n", chain, policy); if (!ops->set_policy(chain, policy, &count, handle)) xtables_error(OTHER_PROBLEM, "Can't set policy `%s'" " on `%s' line %u: %s\n", policy, chain, line, ops->strerror(errno)); } ret = 1; } else if (in_table) { int a; char *ptr = buffer; char *pcnt = NULL; char *bcnt = NULL; char *parsestart; /* reset the newargv */ newargc = 0; if (buffer[0] == '[') { /* we have counters in our input */ ptr = strchr(buffer, ']'); if (!ptr) xtables_error(PARAMETER_PROBLEM, "Bad line %u: need ]\n", line); pcnt = strtok(buffer+1, ":"); if (!pcnt) xtables_error(PARAMETER_PROBLEM, "Bad line %u: need :\n", line); bcnt = strtok(NULL, "]"); if (!bcnt) xtables_error(PARAMETER_PROBLEM, "Bad line %u: need ]\n", line); /* start command parsing after counter */ parsestart = ptr + 1; } else { /* start command parsing at start of line */ parsestart = buffer; } add_argv(argv[0]); add_argv("-t"); add_argv(curtable); if (counters && pcnt && bcnt) { add_argv("--set-counters"); add_argv((char *) pcnt); add_argv((char *) bcnt); } add_param_to_argv(parsestart); DEBUGP("calling do_command4(%u, argv, &%s, handle):\n", newargc, curtable); for (a = 0; a < newargc; a++) DEBUGP("argv[%u]: %s\n", a, newargv[a]); ret = do_command4(newargc, newargv, &newargv[2], &handle); free_argv(); fflush(stdout); } if (tablename && (strcmp(tablename, curtable) != 0)) continue; if (!ret) { fprintf(stderr, "%s: line %u failed\n", xt_params->program_name, line); exit(1); } } if (in_table) { fprintf(stderr, "%s: COMMIT expected at line %u\n", xt_params->program_name, line + 1); exit(1); } fclose(in); return 0; }
/* Run an external command returning exit status, and optionally filling * provided buffer with STDOUT output up to the size provided. * * Note: XXX: We are not using the timeout parameter at present. We still need * to implement a reliable timeout mechanism. */ static int _run_extcmd(uid_t uid, gid_t gid, const char *cmd, char *so_buf, const size_t so_buf_sz, const int want_stderr, const int timeout, const char *substr_search, int *pid_status, const fko_srv_options_t * const opts) { char so_read_buf[IO_READ_BUF_LEN] = {0}; pid_t pid=0; FILE *output; int retval = EXTCMD_SUCCESS_ALL_OUTPUT; int line_ctr = 0, found_str = 0; char *argv_new[MAX_CMDLINE_ARGS]; /* for validation and/or execvpe() */ int argc_new=0; #if HAVE_EXECVPE int pipe_fd[2]; #endif #if AFL_FUZZING return 0; #endif *pid_status = 0; /* Even without execvpe() we examine the command for basic validity * in term of number of args */ memset(argv_new, 0x0, sizeof(argv_new)); if(strtoargv(cmd, argv_new, &argc_new, opts) != 1) { log_msg(LOG_ERR, "run_extcmd(): Error converting cmd str to argv via strtoargv()"); return EXTCMD_ARGV_ERROR; } #if !HAVE_EXECVPE /* if we are not using execvpe() then free up argv_new unconditionally * since was used only for validation */ free_argv(argv_new, &argc_new); #endif #if HAVE_EXECVPE if(opts->verbose > 1) log_msg(LOG_INFO, "run_extcmd() (with execvpe()): running CMD: %s", cmd); if(so_buf != NULL || substr_search != NULL) { if(pipe(pipe_fd) < 0) { log_msg(LOG_ERR, "run_extcmd(): pipe() failed: %s", strerror(errno)); free_argv(argv_new, &argc_new); return EXTCMD_PIPE_ERROR; } } pid = fork(); if (pid == 0) { if(chdir("/") != 0) exit(EXTCMD_CHDIR_ERROR); if(so_buf != NULL || substr_search != NULL) { close(pipe_fd[0]); dup2(pipe_fd[1], STDOUT_FILENO); if(want_stderr) dup2(pipe_fd[1], STDERR_FILENO); else close(STDERR_FILENO); } /* Take care of gid/uid settings before running the command. */ if(gid > 0) if(setgid(gid) < 0) exit(EXTCMD_SETGID_ERROR); if(uid > 0) if(setuid(uid) < 0) exit(EXTCMD_SETUID_ERROR); /* don't use env */ execvpe(argv_new[0], argv_new, (char * const *)NULL); } else if(pid == -1) { log_msg(LOG_ERR, "run_extcmd(): fork() failed: %s", strerror(errno)); free_argv(argv_new, &argc_new); return EXTCMD_FORK_ERROR; } /* Only the parent process makes it here */ if(so_buf != NULL || substr_search != NULL) { close(pipe_fd[1]); if ((output = fdopen(pipe_fd[0], "r")) != NULL) { if(so_buf != NULL) memset(so_buf, 0x0, so_buf_sz); while((fgets(so_read_buf, IO_READ_BUF_LEN, output)) != NULL) { line_ctr++; if(so_buf != NULL) { strlcat(so_buf, so_read_buf, so_buf_sz); if(strlen(so_buf) >= so_buf_sz-1) break; } else /* we are looking for a substring */ { /* Get past comments and empty lines (note: we only look at the * first character). */ if(IS_EMPTY_LINE(so_read_buf[0])) continue; if(strstr(so_read_buf, substr_search) != NULL) { found_str = 1; break; } } } fclose(output); } else { log_msg(LOG_ERR, "run_extcmd(): could not fdopen() pipe output file descriptor."); free_argv(argv_new, &argc_new); return EXTCMD_OPEN_ERROR; } } free_argv(argv_new, &argc_new); waitpid(pid, pid_status, 0); #else if(opts->verbose > 1) log_msg(LOG_INFO, "run_extcmd() (without execvpe()): running CMD: %s", cmd); if(so_buf == NULL && substr_search == NULL) { /* Since we do not have to capture output, we will fork here (which we * * would have to do anyway if we are running as another user as well). * */ pid = fork(); if(pid == -1) { log_msg(LOG_ERR, "run_extcmd: fork failed: %s", strerror(errno)); return(EXTCMD_FORK_ERROR); } else if (pid == 0) { /* We are the child */ if(chdir("/") != 0) exit(EXTCMD_CHDIR_ERROR); /* Take care of gid/uid settings before running the command. */ if(gid > 0) if(setgid(gid) < 0) exit(EXTCMD_SETGID_ERROR); if(uid > 0) if(setuid(uid) < 0) exit(EXTCMD_SETUID_ERROR); *pid_status = system(cmd); exit(*pid_status); } /* Retval is forced to 0 as we don't care about the exit status of * the child (for now) */ retval = EXTCMD_SUCCESS_ALL_OUTPUT; } else { /* Looking for output use popen and fill the buffer to its limit. */ output = popen(cmd, "r"); if(output == NULL) { log_msg(LOG_ERR, "Got popen error %i: %s", errno, strerror(errno)); retval = EXTCMD_OPEN_ERROR; } else { if(so_buf != NULL) memset(so_buf, 0x0, so_buf_sz); while((fgets(so_read_buf, IO_READ_BUF_LEN, output)) != NULL) { line_ctr++; if(so_buf != NULL) { strlcat(so_buf, so_read_buf, so_buf_sz); if(strlen(so_buf) >= so_buf_sz-1) break; } else /* we are looking for a substring */ { /* Get past comments and empty lines (note: we only look at the * first character). */ if(IS_EMPTY_LINE(so_read_buf[0])) continue; if(strstr(so_read_buf, substr_search) != NULL) { found_str = 1; break; } } } pclose(output); } } #endif if(substr_search != NULL) { /* The semantics of the return value changes in search mode to the line * number where the substring match was found, or zero if it wasn't found */ if(found_str) retval = line_ctr; else retval = 0; } else { if(WIFEXITED(*pid_status)) { /* Even if the child exited with an error condition, if we make it here * then the child exited normally as far as the OS is concerned (i.e. didn't * crash or get hit with a signal) */ retval = EXTCMD_SUCCESS_ALL_OUTPUT; } else retval = EXTCMD_EXECUTION_ERROR; } if(opts->verbose > 1) log_msg(LOG_INFO, "run_extcmd(): returning %d, pid_status: %d", retval, WIFEXITED(*pid_status) ? WEXITSTATUS(*pid_status) : *pid_status); return(retval); }
int parse_server_string ( const char *str ) { assert(str); if(bool_option("show_all_server_strings")) cio_out(str); current_server_string = str; size_t i; char **argv; int argc; char *buf = NULL; server_string_t *si; server_string_list_t *sl; /* make a buffer */ buf = copy_string(str); assert(buf != NULL); buf[strlen(buf) - 1] = 0; if(strlen(buf) == 0) { return -1; } make_argv(&argc, &argv, buf); assert(argc != 0); // we want a consistent format regardless of whether or not a prefix // was sent, so if one wasn't we fake it if(argv[0][0] != ':') { // no prefix was found char **tmp = malloc(sizeof(char *) * (argc + 1)); tmp[0] = malloc(strlen(irc_pick_server()) + 2); tmp[0][0] = ':'; strcpy(&tmp[0][1], irc_pick_server()); for(i = 0; i < argc; i++) { tmp[i + 1] = copy_string(argv[i]); } free_argv(argc, argv); argc++; argv = tmp; } // run hooks rcv_hook_t *rcvp = &hook_head; for(list_next(rcvp); rcvp != &hook_head; list_next(rcvp)) { rcvp->func(argc, argv); } // find a function to run sl = (server_string_list_t *)(head.head.next); find_next_string_handler: for( ; (list_t *)sl != (list_t *)&head; list_next(sl)) { si = sl->strings; while(si->keystring != NULL) { if(strcmp_nc(argv[1], si->keystring) == 0) { goto found_string_handler; } si++; } } // nothing was found if(bool_option("verbose") || bool_option("show_unhandled_msgs")) { cio_out("unhandled string sent from server: %s\n", str); } free_argv(argc, argv); free(buf); return -1; found_string_handler: free_argv(argc, argv); if(buf[0] == ':') { make_max_argv(&argc, &argv, buf, si->argc); } else { make_max_argv(&argc, &argv, buf, si->argc - 1); char **tmp = malloc(sizeof(char *) * (argc + 1)); //tmp[0] = copy_string(irc_pick_server()); tmp[0] = malloc(strlen(irc_pick_server()) + 2); tmp[0][0] = ':'; strcpy(&tmp[0][1], irc_pick_server()); //add_char_to_str(tmp[0], 0, ':'); for(i = 0; i < argc; i++) { tmp[i + 1] = copy_string(argv[i]); } free_argv(argc, argv); argc++; argv = tmp; } if(si->strip_colon && strlen(argv[argc - 1]) != 0 && argv[argc - 1][0] == ':') { remove_char_from_str(argv[argc - 1], 0); } /********************************************** * make sure argc is correct, so that the * callbacks don't get any suprises **********************************************/ assert(!(argc > si->argc)); if(argc < si->argc) { char **tmp = argv; argv = malloc(sizeof(char *) * si->argc); for(i = 0; i < argc; i++) { argv[i] = tmp[i]; } for(i = argc; i < si->argc; i++) { argv[i] = malloc(1); argv[i][0] = 0; } argc = si->argc; } int retval = si->hook(argc, argv); if(retval) { list_next(sl); goto find_next_string_handler; } free_argv(argc, argv); current_server_string = NULL; return retval; }
/* Ktap Main Entry */ static int ktap_main(struct file *file, struct ktap_parm *parm) { unsigned long *buff = NULL; ktap_state *ks; ktap_closure *cl; char **argv; int start_time, delta_time; int ret; if (atomic_inc_return(&kp_is_running) != 1) { atomic_dec(&kp_is_running); pr_info("only one ktap thread allow to run\n"); return -EBUSY; } start_time = gettimeofday_us(); ret = load_trunk(parm, &buff); if (ret) { pr_err("cannot load file\n"); goto out; } argv = copy_argv_from_user(parm); if (IS_ERR(argv)) { vfree(buff); ret = PTR_ERR(argv); goto out; } ks = kp_newstate(parm, kp_dir_dentry, argv); /* free argv memory after store into arg table */ free_argv(parm->argc, argv); if (unlikely(!ks)) { vfree(buff); ret = -ENOEXEC; goto out; } file->private_data = ks; cl = kp_load(ks, (unsigned char *)buff); vfree(buff); if (cl) { /* optimize bytecode before excuting */ kp_optimize_code(ks, 0, cl->l.p); delta_time = gettimeofday_us() - start_time; kp_verbose_printf(ks, "booting time: %d (us)\n", delta_time); kp_call(ks, ks->top - 1, 0); } kp_final_exit(ks); out: atomic_dec(&kp_is_running); return ret; }