void libtu_init(const char *argv0) { progname=argv0; #ifndef CF_NO_GETTEXT textdomain(simple_basename(argv0)); #endif }
static int create_child_process(char *command_string, char *blockname, ProgramState *state) { char *command = command_string; char *argv; int i = 0; int ret_status; PmStatus pm_status; int tmp_io[2]; int fd_arr[3]; int ifd[2], ofd[2]; char *handle; struct PmLoadModuleInfoReply *mod_info; /* Parse out cmd and argv from the command string */ while (1) { if (command[i] == ' ' || command[i] == '\0') { if (command[i] == '\0') argv = NULL; else { command[i] = '\0'; argv = command_string + i + 1; } break; } i++; } if (blockname) handle = blockname; else handle = simple_basename(command); if (ose_pm_load_module_info(handle,&mod_info) == PM_SUCCESS) { /* Already installed */ free_buf((union SIGNAL**)&mod_info); } else if ((pm_status = ose_pm_install_load_module(0,"ELF",command,handle,0,0,NULL)) != PM_SUCCESS) { ERROR1(LOG_ERR,"ose_pm_install_load_module failed - pmstatus: 0x%08x\n", pm_status); return 0; } state->domain = PM_NEW_DOMAIN; pm_status = ose_pm_create_program(&state->domain, handle, 0, 0 , NULL, &state->progpid, &state->mainbid); if (pm_status != PM_SUCCESS) { if (pm_status == PM_EINSTALL_HANDLE_IN_USE) ERROR1(LOG_ERR,"ose_pm_create_program failed - " "install handle \"%s\" is in use. You can specify another " "install handle by using the -block option to run_erl.\n",handle); else ERROR1(LOG_ERR,"ose_pm_create_program failed - pmstatus: 0x%08x\n", pm_status); return 0; } pm_status = ose_pm_program_info(state->progpid, &state->info); /* FIXME don't forget to free this ((union SIGNAL **)&info) */ if (pm_status != PM_SUCCESS) { ERROR1(LOG_ERR,"ose_pm_program_info failed - pmstatus: 0x%08x\n", pm_status); return 0; } /* We only clone stdin+stdout, what about stderr? */ /* create pipes */ if (pipe(ifd) < 0) { if (errno == ENOENT) ERRNO_ERR0(LOG_ERR,"The /pipe file system is not available\n"); else ERRNO_ERR0(LOG_ERR,"pipe ifd failed\n"); return 0; } if (pipe(ofd) < 0) { ERRNO_ERR0(LOG_ERR,"pipe ofd failed\n"); return 0; } /* FIXME Lock? */ /* backup our stdin stdout */ if ((tmp_io[0] = dup(0)) < 0) { ERRNO_ERR0(LOG_ERR,"dup 0 failed\n"); return 0; } if ((tmp_io[1] = dup(1)) < 0) { ERRNO_ERR0(LOG_ERR,"dup 1 failed\n"); return 0; } /* set new pipe to fd 0,1 */ if (dup2(ifd[1], 1) < 0) { ERRNO_ERR0(LOG_ERR,"dup2 1 failed\n"); return 0; } if (dup2(ofd[0], 0) < 0) { ERRNO_ERR0(LOG_ERR,"dup2 0 failed\n"); return 0; } /* clone array to newly created */ fd_arr[0] = 2; /* Number of fd's */ fd_arr[1] = 0; fd_arr[2] = 1; if ((ret_status = efs_clone_array(state->info->main_process, fd_arr)) != EFS_SUCCESS) { ERROR1(LOG_ERR,"efs_close_array filed, errcode: %d\n", ret_status); return 0; } if (dup2(tmp_io[1], 1) < 0) { ERRNO_ERR0(LOG_ERR,"restoring dup2 1 failed\n"); return 0; } if (dup2(tmp_io[0], 0) < 0) { ERRNO_ERR0(LOG_ERR,"restoring dup2 1 failed\n"); return 0; } /* close loose-ends */ sf_close(tmp_io[0]); sf_close(tmp_io[1]); sf_close(ifd[1]); sf_close(ofd[0]); state->ifd = ifd[0]; state->ofd = ofd[1]; if (argv && set_env(state->progpid, "ARGV", argv)) { ERRNO_ERR0(LOG_ERR,"something went wrong with set_env\n"); } /* * Start the program. */ pm_status = ose_pm_start_program(state->progpid); if (pm_status != PM_SUCCESS) { ERROR1(LOG_ERR,"ose_pm_install_load_module failed - pmstatus: 0x%08x\n", pm_status); return 0; } return 1; }
void watch_file() { int r; int notify_fd; int dwatch; struct astr *sfilename; char *file; uint8_t inotify_buf[sizeof(struct inotify_event) + NAME_MAX + 1]; struct inotify_event *event; size_t i; ssize_t len; /* initialize the inotify system */ notify_fd = inotify_init(); if(notify_fd < 0) { perror(ERRORTEXT("Fatal: failed to initialize " "inotify system")); exit(EXIT_FAILURE); } setup_watch: /* set up the inotify watch and associated variables */ sfilename = (struct astr *) arcp_load(&livec_opts.filename); if(sfilename == NULL) { fprintf(stderr, ERRORTEXT("Fatal: no filename defined\n")); exit(EXIT_FAILURE); } file = simple_basename(astr_cstr(sfilename)); dwatch = setup_inotify_watch(notify_fd, sfilename); /* process the file */ process_file(sfilename); /* main watch loop */ for(;;) { if(sfilename != (struct astr *) arcp_load_phantom(&livec_opts.filename)) { /* the filename option has changed; remove the watch * and restart */ arcp_release(sfilename); r = inotify_rm_watch(notify_fd, dwatch); if(r != 0) { perror(ERRORTEXT("Failed to clean" " up old watch")); } goto setup_watch; } /* block until there's at least one event to be notified * about */ len = read(notify_fd, inotify_buf, sizeof(struct inotify_event) + NAME_MAX + 1); if(len <= 0) { perror(ERRORTEXT("read() of inotify event failed")); continue; } /* read through all events */ for(i = 0; i <= len - sizeof(struct inotify_event);) { event = (struct inotify_event *) &inotify_buf[i]; i += sizeof(struct inotify_event) + event->len; if(event->wd != dwatch) { /* FIXME: why wouldn't this be the same? */ continue; } if((event->mask & (IN_CLOSE_WRITE|IN_MOVED_TO)) && (strcmp(event->name, file) == 0)) { /* FIXME: do we need the event mask test * here? */ /* the (directory) event was about the file * we're interested in */ process_file(sfilename); break; } } } }
/* Here we go */ int main(int argc, char *argv[], char *envp[]) { char *progname; const struct futil_cmd_t *cmd; int i, errorcnt = 0; int vb_ver = VBOOT_VERSION_ALL; struct option long_opts[] = { {"vb1" , 0, &vb_ver, VBOOT_VERSION_1_0}, {"vb21", 0, &vb_ver, VBOOT_VERSION_2_1}, { 0, 0, 0, 0}, }; log_args(argc, argv); /* How were we invoked? */ progname = simple_basename(argv[0]); /* See if the program name is a command we recognize */ cmd = find_command(progname); if (cmd) /* Yep, just do that */ return run_command(cmd, argc, argv); /* Parse the global options, stopping at the first non-option. */ opterr = 0; /* quiet, you. */ while ((i = getopt_long(argc, argv, "+:", long_opts, NULL)) != -1) { switch (i) { case '?': if (optopt) fprintf(stderr, "Unrecognized option: -%c\n", optopt); else fprintf(stderr, "Unrecognized option: %s\n", argv[optind - 1]); errorcnt++; break; case ':': fprintf(stderr, "Missing argument to -%c\n", optopt); errorcnt++; break; case 0: /* handled option */ break; default: Debug("i=%d\n", i); DIE; } } vboot_version = vb_ver; /* Reset the getopt state so commands can parse their own options. */ argc -= optind; argv += optind; optind = 0; /* We require a command name. */ if (errorcnt || argc < 1) { do_help(0, 0); return 1; } /* For reasons I've forgotten, treat /blah/blah/CMD the same as CMD */ progname = simple_basename(argv[0]); /* Do we recognize the command? */ cmd = find_command(progname); if (cmd) return run_command(cmd, argc, argv); /* Nope. We've no clue what we're being asked to do. */ do_help(0, 0); return 1; }
int main(int argc, char *argv[], char *envp[]) { char *progname; const struct futil_cmd_t *cmd; int i, errorcnt = 0; int vb_ver = VBOOT_VERSION_ALL; int helpind = 0; struct option long_opts[] = { {"debug", 0, &debugging_enabled, 1}, {"vb1" , 0, &vb_ver, VBOOT_VERSION_1_0}, {"vb21", 0, &vb_ver, VBOOT_VERSION_2_1}, {"help", 0, 0, OPT_HELP}, { 0, 0, 0, 0}, }; log_args(argc, argv); /* How were we invoked? */ progname = simple_basename(argv[0]); /* See if the program name is a command we recognize */ cmd = find_command(progname); if (cmd) { /* Yep, just do that */ return !!run_command(cmd, argc, argv); } /* Parse the global options, stopping at the first non-option. */ opterr = 0; /* quiet, you. */ while ((i = getopt_long(argc, argv, "+:", long_opts, NULL)) != -1) { switch (i) { case OPT_HELP: /* Remember where we found this option */ /* Note: this might be GNU-specific */ helpind = optind - 1; break; case '?': if (optopt) fprintf(stderr, "Unrecognized option: -%c\n", optopt); else fprintf(stderr, "Unrecognized option: %s\n", argv[optind - 1]); errorcnt++; break; case ':': fprintf(stderr, "Missing argument to -%c\n", optopt); errorcnt++; break; case 0: /* handled option */ break; default: Debug("i=%d\n", i); DIE; } } vboot_version = vb_ver; /* * Translate "--help" in the args to "help" as the first parameter, * by rearranging argv[]. */ if (helpind) { int i; optind--; for (i = helpind; i < optind; i++) argv[i] = argv[i + 1]; argv[i] = "help"; } /* We require a command name. */ if (errorcnt || argc == optind) { do_help(1, argv); return 1; } /* For reasons I've forgotten, treat /blah/blah/CMD the same as CMD */ argv[optind] = simple_basename(argv[optind]); /* Do we recognize the command? */ cmd = find_command(argv[optind]); if (cmd) { /* Reset so commands can parse their own options */ argc -= optind; argv += optind; optind = 0; return !!run_command(cmd, argc, argv); } /* Nope. We've no clue what we're being asked to do. */ do_help(1, argv); return 1; }