static void open_dbs(int op, const char *dir, const char *dir_wr, const char *dir_rd, const char *dir_rd2) { int expect, ret; /* * The parent has an open connection to all directories. * We expect opening the writeable homes to return an error. * It is a failure if the child successfully opens that. */ expect = EXPECT_ERR; if ((ret = run_child(dir, op, expect)) != 0) testutil_die(ret, "wiredtiger_open readonly allowed"); if ((ret = run_child(dir_wr, op, expect)) != 0) testutil_die(ret, "wiredtiger_open readonly allowed"); /* * The parent must have a read-only connection open to the * read-only databases. If the child is opening read-only * too, we expect success. Otherwise an error if the child * attempts to open read/write (permission error). */ if (op == OP_READ) expect = EXPECT_SUCCESS; if ((ret = run_child(dir_rd, op, expect)) != 0) testutil_die(ret, "run child 1"); if ((ret = run_child(dir_rd2, op, expect)) != 0) testutil_die(ret, "run child 2"); exit(EXIT_SUCCESS); }
/** * Start a background process. */ void background(char *args[ARGS_SIZE]) { char *command = args[0]; pid_t pid; pid_t group_id = getpid(); int return_value = 0; signal(SIGINT, SIG_IGN); pid = fork(); if (pid == 0) { return_value = setpgid(0, group_id); check_return_value(return_value, "FAILED DUDE"); run_child(args, command); return; } else if (pid > 0) { return_value = setpgid(pid, group_id); check_return_value(return_value, "FAILED DUDE2"); } else if (pid < 0) { /* System fork err */ printf("Fork failed"); exit(0xCC); } }
static void test_pty(void) { pid_t pid; Pty *pty; rcvsiz = 0; zero(rcvbuf); assert_se(sd_event_default(&event) >= 0); pid = pty_fork(&pty, event, pty_fn, NULL, 80, 25); assert_se(pid >= 0); if (pid == 0) { /* child */ run_child(pty); exit(0); } /* parent */ run_parent(pty); /* Make sure the PTY recycled the child; yeah, this is racy if the * PID was already reused; but that seems fine for a test. */ assert_se(waitpid(pid, NULL, WNOHANG) < 0 && errno == ECHILD); pty_unref(pty); sd_event_unref(event); }
int main(void) { pid_t child; int status; size_t page_size = sysconf(_SC_PAGESIZE); shmem = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0); test_assert(shmem != (void*)-1); semid = semget(IPC_PRIVATE, COUNT, 0666); test_assert(semid >= 0); if ((child = fork()) == 0) { return run_child(); } atomic_printf("child %d\n", child); test_assert(child == waitpid(child, &status, __WALL)); /* delete the sem before testing status, because we want to ensure the segment is deleted even if the test failed. */ test_assert(0 == semctl(semid, 0, IPC_RMID, NULL)); test_assert(status == 0); atomic_puts("EXIT-SUCCESS"); return 0; }
static int run_test(void) { int ret; int fd; struct rlimit nofile; /* Emulate what sandboxes trying to close all open file descriptors */ test_assert(0 == getrlimit(RLIMIT_NOFILE, &nofile)); for (fd = STDOUT_FILENO + 1; fd < nofile.rlim_cur; ++fd) { ret = close(fd); test_assert(ret == 0 || (ret == -1 && errno == EBADF)); } ret = unshare(CLONE_NEWUSER); if (ret == -1 && errno == EINVAL) { atomic_puts("EXIT-SUCCESS"); return 77; } test_assert(0 == ret); test_assert(0 == unshare(CLONE_NEWNS)); test_assert(0 == unshare(CLONE_NEWIPC)); test_assert(0 == unshare(CLONE_NEWNET)); ret = unshare(CLONE_NEWPID); if (ret == -1 && errno == EINVAL) { atomic_puts("EXIT-SUCCESS"); return 77; } test_assert(0 == ret); test_assert(0 == chroot(tmp_name)); run_child(); return 77; }
void perfmon_init(void) { size_t i; long nr; if (cpu_type == CPU_TIMER_INT) return; if (!no_xen) { xen_ctx = xmalloc(sizeof(struct child)); xen_ctx->pid = getpid(); xen_ctx->up_pipe[0] = -1; xen_ctx->up_pipe[1] = -1; xen_ctx->sigusr1 = 0; xen_ctx->sigusr2 = 0; xen_ctx->sigterm = 0; create_context(xen_ctx); write_pmu(xen_ctx); load_context(xen_ctx); return; } nr = sysconf(_SC_NPROCESSORS_ONLN); if (nr == -1) { fprintf(stderr, "Couldn't determine number of CPUs.\n"); exit(EXIT_FAILURE); } nr_cpus = nr; children = xmalloc(sizeof(struct child) * nr_cpus); bzero(children, sizeof(struct child) * nr_cpus); for (i = 0; i < nr_cpus; ++i) { int ret; if (pipe(children[i].up_pipe)) { perror("Couldn't create child pipe"); exit(EXIT_FAILURE); } ret = fork(); if (ret == -1) { perror("Couldn't fork perfmon child"); exit(EXIT_FAILURE); } else if (ret == 0) { close(children[i].up_pipe[0]); run_child(i); } else { children[i].pid = ret; close(children[i].up_pipe[1]); printf("Waiting on CPU%d\n", (int)i); wait_for_child(&children[i]); } } }
static int command_loop(const char *child) { char buffer[MAXCOMMAND]; while (1) { size_t i; if (!fgets(buffer, MAXCOMMAND - 1, stdin)) { if (ferror(stdin)) die("Comammand input error"); exit(0); } /* Strip end of line characters. */ i = strlen(buffer); while (i > 0 && isspace(buffer[i - 1])) buffer[--i] = 0; if (!strcmp(buffer, "capabilities")) { printf("*connect\n\n"); fflush(stdout); } else if (!strncmp(buffer, "connect ", 8)) { printf("\n"); fflush(stdout); return run_child(child, buffer + 8); } else { fprintf(stderr, "Bad command"); return 1; } } }
static unsigned long doit(void *shm) { pid_t child; unsigned long res; int status; child = fork(); if (child == 0) { run_child(shm); _exit(0); } else { res = run_parent(shm); if (waitpid(child, &status, 0) != child) err(1, "waitpid"); if (WIFSIGNALED(status)) errx(1, "child died with signal %d", WTERMSIG(status)); if (!WIFEXITED(status)) errx(1, "child abnromal exit code 0x%x", status); if (WEXITSTATUS(status) != 0) errx(1, "child reported error %d", WEXITSTATUS(status)); *(unsigned *)shm = 0; return res; } }
int subprocloc(int timeout, FILE *fd[2], subproc_callback callback, void *data, const char *cmd, const char *args[]) { struct log_buffer log; log_buffer_init(&log, LL_DBG); if (log.f) { fprintf(log.f, "Running subprocess: %s", cmd); for (const char **p = args; *p; p++) fprintf(log.f, " %s", *p); fclose(log.f); DBG("%s", log.char_buffer); free(log.char_buffer); } // Prepare pipes for stdout and stderr int p_err[2], p_out[2]; pipe2(p_err, O_NONBLOCK); pipe2(p_out, O_NONBLOCK); // Fork pid_t pid = fork(); if (pid == -1) DIE("Failed to fork command %s: %s", cmd, strerror(errno)); else if (pid == 0) run_child(cmd, args, callback, data, p_out, p_err); ASSERT(close(p_out[1]) != -1); ASSERT(close(p_err[1]) != -1); struct pollfd pfds[] = { { .fd = p_out[0], .events = POLLIN }, { .fd = p_err[0], .events = POLLIN }
static void check_uevent(struct uevent *event) { int i; int child_state; if ( !strcmp(event->subsystem,"usb") && event->action == action_add && !target_inserted) { for (i = 0; i < UEVENT_PARAMS_MAX; i++) { if (!event->param[i]) break; if (!strncmp(event->param[i], "PRODUCT=", strlen("PRODUCT="))) { char *a = event->param[i] + strlen("PRODUCT="); if (!strcmp(a, "12d1/1446/0")) { // got you! Huawei E161/E169u LOG_VOL("Find target:%s => ready to call system", event->param[i]); target_inserted = 1; pid = fork(); if (!pid) run_child(); else { LOGD("waiting for usb_modeswitch terminated"); wait(&child_state); pid = -1; LOGD("ok now usb_modeswitch terminated"); } break; } } LOG_VOL("%s", event->param[i]); } } else if ( !strcmp(event->subsystem,"usb") && event->action == action_remove && target_inserted ) { target_inserted = 0; } }
void find_and_parse_cub_admin_version (int &major_version, int &minor_version) { const char *argv[3]; char tmpfile[PATH_MAX], strbuf[BUFFER_MAX_LEN]; FILE *infile; char cmd_name[CUBRID_CMD_NAME_LEN]; cubrid_cmd_name (cmd_name); snprintf (tmpfile, PATH_MAX - 1, "%s/cub_admin_version", sco.dbmt_tmp_dir); argv[0] = cmd_name; argv[1] = "--version"; argv[2] = NULL; run_child (argv, 1, NULL, tmpfile, NULL, NULL); if ((infile = fopen (tmpfile, "r")) != NULL) { fgets (strbuf, sizeof (strbuf), infile); fgets (strbuf, sizeof (strbuf), infile); char version[10]; sscanf (strbuf, "%*s %s", version); char *p = strtok (version, "."); major_version = atoi (p); p = strtok (NULL, "."); minor_version = atoi (p); fclose (infile); unlink (tmpfile); } else { major_version = minor_version = -1; } }
void start(void) { volatile int checker = 0; /* This variable checks that you correctly gave the child process a new stack. */ pid_t p; int status; app_printf("About to start a new process...\n"); p = sys_fork(); if (p == 0) run_child(); else if (p > 0) { app_printf("Main process %d!\n", sys_getpid()); do { status = sys_wait(p); app_printf("W"); } while (status == WAIT_TRYAGAIN); app_printf("Child %d exited with status %d!\n", p, status); // Check whether the child process corrupted our stack. // (This check doesn't find all errors, but it helps.) if (checker != 0) { app_printf("Error: stack collision!\n"); sys_exit(1); } else sys_exit(0); } else { app_printf("Error!\n"); sys_exit(1); } }
int main (int argc, char *argv[]) { int fd = -1, ret = -1, status = 0; char *filename = NULL, *cmd = NULL; struct stat stbuf = {0, }; if (argc != 3) { fprintf (stderr, "Usage: %s <filename> " "<gluster-cmd-to-trigger-graph-switch>\n", argv[0]); goto out; } filename = argv[1]; cmd = argv[2]; fd = open (filename, O_RDWR | O_CREAT, 0); if (fd < 0) { fprintf (stderr, "open (%s) failed (%s)\n", filename, strerror (errno)); goto out; } ret = flock (fd, LOCK_EX); if (ret < 0) { fprintf (stderr, "flock failed (%s)\n", strerror (errno)); goto out; } system (cmd); /* wait till graph switch completes */ ret = fstat64 (fd, &stbuf); if (ret < 0) { fprintf (stderr, "fstat64 failure (%s)\n", strerror (errno)); goto out; } sleep (10); /* By now old-graph would be disconnected and locks should be cleaned * up if they are not migrated. Check that by trying to acquire a lock * on a new fd opened by another process on same file */ ret = fork (); if (ret == 0) { ret = run_child (filename); } else { wait (&status); if (WIFEXITED(status)) { ret = WEXITSTATUS(status); } else { ret = 0; } } out: return ret; }
void Debugger::Go(bool enablebps) { if(enablebps) ActivateBreakpoints(); run_child(); if(enablebps) DeactivateBreakpoints(); }
static const char *block_on_write(void) { char buf[PIPE_BUF+4]; for (int i = 0; i < PIPE_BUF+4; i++) buf[i] = "pipe"[i % 4]; run_child(bow_child); if (write(write_end, buf, PIPE_BUF+4) != PIPE_BUF+4) return "write"; return wait_for_child(); }
int main(void) { cleanupsems(); pid_t server_pid = run_child(server); pid_t client_pid = run_child(client); int server_s, client_s; waitpid(server_pid, &server_s, 0); waitpid(client_pid, &client_s, 0); printf("\n"); int ret = EXIT_FAILURE; if (server_s == 0 && client_s == 0) { ret = EXIT_SUCCESS; } return ret; }
static const char *block_on_read(void) { char buf[4]; if (run_child(bor_child)) return "run_child"; if (read(read_end, buf, 4) != 4) return "read"; if (memcmp(buf, "pipe", 4)) return "memcmp"; return wait_for_child(); }
void mpijob::run() { #ifndef __WINDOWS__ // no forking, call child serially directly _started = true; _alive = true; declare_started(); run_child(); //t.stop(); #else eblerror("not implemented"); #endif }
static result_t start_child(pid_t * const pid, const int master_fd, const int slave_fd, const options_t * const options) { if (!process_create(pid)) fail(); if (*pid == 0) { close(master_fd); run_child(slave_fd, options); } succeed(); }
static void daemon_accept (int fd, fd_set socketfds, fd_set sslfds, int maxfd) { struct sockaddr_in addr; socklen_t addrlen = sizeof (addr); CLIENT *initclient; int rc = -1; rc = accept (fd, (struct sockaddr *)&addr, &addrlen); if (rc < 0) return; initclient = access_check (rc, addr); // uint port = ntohs (addr.sin_port); if (! initclient) { close (rc); return; } initclient->useSSL = FD_ISSET(fd, &sslfds); initclient->ssl = NULL; #if defined(_SC_NPROCESSORS_ONLN) if ( cfg.CoreBind > 0 ) { /* Select CPU core to bind to */ master->currcore++; if ( master->currcore >= master->numcores ) master->currcore = 0; initclient->numcore = master->currcore; } #endif switch (fork ()) { case 0: syslog_open ("nntpswitchd", LOG_PID, LOG_NEWS); for (int i = 0; i <= maxfd; i++) { if (FD_ISSET (i, &socketfds)) close (i); } run_child (initclient); syslog_close (); _exit (0); case -1: info ("couldnt fork child %s", strerror (errno)); default: if ( cfg.JSONACL == 1 && initclient->acl != NULL ) { freehttpacl(initclient->acl); } close (rc); } }
int cmd_stop_server (char *dbname, char *err_buf, int err_buf_size) { char strbuf[1024]; int t, timeout = 30, interval = 3; /* sec */ char cmd_name[CUBRID_CMD_NAME_LEN]; const char *argv[5]; if (err_buf) { memset (err_buf, 0, err_buf_size); } cmd_name[0] = '\0'; #if !defined (DO_NOT_USE_CUBRIDENV) sprintf (cmd_name, "%s/%s%s", sco.szCubrid, CUBRID_DIR_BIN, UTIL_CUBRID); #else sprintf (cmd_name, "%s/%s", CUBRID_BINDIR, UTIL_CUBRID); #endif argv[0] = cmd_name; argv[1] = PRINT_CMD_SERVER; argv[2] = PRINT_CMD_STOP; argv[3] = dbname; argv[4] = NULL; if (run_child (argv, 1, NULL, NULL, NULL, NULL) < 0) { /* stop_server */ if (err_buf) { sprintf (strbuf, "Command returned error : %s %s %s %s", cmd_name, PRINT_CMD_SERVER, PRINT_CMD_STOP, dbname); strncpy (err_buf, strbuf, err_buf_size - 1); } return -1; } for (t = timeout; t > 0; t -= interval) { SLEEP_MILISEC (interval, 0); if (!uIsDatabaseActive (dbname)) { return 0; } } if (err_buf) { sprintf (strbuf, "%s server hasn't shut down after %d seconds", dbname, timeout); strncpy (err_buf, strbuf, err_buf_size - 1); } return -1; }
int main(int argc, char** argv) { int fd; /* fifo pipe fd */ int i; int num_reported; pid_t pids[NUM_CHILDREN]; /* array of child pids */ /* * open up our pipe for status messages from child processes */ if( (fd = open_pipe(FIFO_NAME)) < 0) { exit(-1); } /* * start up the children, and hang on to the pids */ for (i = 0; i < NUM_CHILDREN; ++i) { pid_t pid = fork(); pids[i] = pid; if(pid == -1) { perror("Error forking child process"); exit(-1); } else if(pids[i] == 0) { run_child(); } } /* * wait for the child processes to complete their task */ if(await_children(pids, NUM_CHILDREN) != NUM_CHILDREN) { fprintf(stderr, "Child count doesn't match number forked\n"); } /* * read the results from the pipe */ num_reported = read_and_print_results(fd, pids, NUM_CHILDREN); if(num_reported != NUM_CHILDREN) { fprintf(stderr, "Not all children reported results\n"); } /* done with the pipe. close and unlink it. */ close(fd); unlink(FIFO_NAME); return 0; }
GeneralSpacedbResult * cmd_spacedb (const char *dbname, T_CUBRID_MODE mode) { GeneralSpacedbResult *res = NULL; int minor_version, major_version; char out_file[PATH_MAX]; char cubrid_err_file[PATH_MAX]; char cmd_name[CUBRID_CMD_NAME_LEN]; char err_message[ERR_MSG_SIZE]; const char *argv[10]; int argc = 0; cubrid_err_file[0] = '\0'; find_and_parse_cub_admin_version (major_version, minor_version); if (major_version < 10 || (major_version == 10 && minor_version == 0)) { res = new SpaceDbResultOldFormat(); } else { res = new SpaceDbResultNewFormat(); } sprintf (out_file, "%s/DBMT_util_002.%d", sco.dbmt_tmp_dir, (int) getpid ()); cubrid_cmd_name (cmd_name); argv[argc++] = cmd_name; argv[argc++] = UTIL_OPTION_SPACEDB; argv[argc++] = get_cubrid_mode_opt (mode); argv[argc++] = "--" SPACE_SIZE_UNIT_L; argv[argc++] = "PAGE"; argv[argc++] = "--" SPACE_OUTPUT_FILE_L; argv[argc++] = out_file; argv[argc++] = dbname; argv[argc++] = "-p"; argv[argc++] = NULL; snprintf (cubrid_err_file, PATH_MAX, "%s/%s.%u.err.tmp", sco.dbmt_tmp_dir, "cmd_spacedb", getpid ()); run_child (argv, 1, NULL, NULL, cubrid_err_file, NULL); /* spacedb */ read_error_file (cubrid_err_file, err_message, ERR_MSG_SIZE); res->set_err_msg (err_message); read_spacedb_output (res, out_file); if (access (cubrid_err_file, F_OK) == 0) { unlink (cubrid_err_file); } unlink (out_file); return res; }
/* * Run a test in a particular credential context -- always call the setup and * cleanup routines; if setup succeeds, also run the test. Test cleanup must * handle cases where the setup has failed, so may need to maintain their own * state in order to know what needs cleaning up (such as whether temporary * files were created). */ static void run(struct test *test, int asroot, int injail) { pid_t childpid, pid; if (test->t_setup_func != NULL) { if ((test->t_setup_func)(asroot, injail, test) != 0) { warnx("run(%s, %d, %d) setup failed", test->t_name, asroot, injail); goto cleanup; } } fflush(stdout); fflush(stderr); childpid = fork(); if (childpid == -1) { warn("run(%s, %d, %d) fork failed", test->t_name, asroot, injail); goto cleanup; } if (childpid == 0) { run_child(test, asroot, injail); fflush(stdout); fflush(stderr); exit(something_failed ? EXIT_FAILURE : EXIT_SUCCESS); } else { while (1) { int status; pid = waitpid(childpid, &status, 0); if (pid == -1) { something_failed = 1; warn("test: waitpid %s", test->t_name); } if (pid == childpid) { if (WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS) { /* All good in the subprocess! */ } else { something_failed = 1; } break; } } } fflush(stdout); fflush(stderr); cleanup: if (test->t_cleanup_func != NULL) test->t_cleanup_func(asroot, injail, test); }
/** * Start a process in the foreground and wait for the process to finish. */ void foreground(char *args[ARGS_SIZE]) { char *command = args[0]; pid_t group_id = getpid(); pid_t pid; pid = fork(); if (pid == 0) { /* Child */ setpgid(0, group_id); run_child(args, command); } else if (pid > 0) { /* Parent */ int status = 0; struct rusage before; struct rusage after; setpgid(pid, group_id); if (getrusage(RUSAGE_CHILDREN, &before) == -1) { perror("Catastrophic failure"); exit(EXIT_FAILURE); } sighold(SIGCHLD); if (waitpid(pid, &status, 0) == -1) { printf("waitpid failed %d\n", errno); } else { getrusage(RUSAGE_CHILDREN, &after); print_time(&before, &after); if (WIFEXITED(status)) { printf("Exited with status %d\n", WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { printf("Exited through signal %d\n", WTERMSIG(status)); } else { putz("Exited abnormally"); } } sigrelse(SIGCHLD); } else { /* System fork err */ printf("Fork failed"); exit(0xCC); } }
static void run_other_prog() { char *argv[] = { "/bin/sh", "-c", "/usr/local/etc/rootapp2.sh", NULL }; int status; msg("rootapp2.sh>"); reset(); status = run_child(argv); g_bPowerdown = WEXITSTATUS(status) == 99; }
/* chain fork a large number of children. */ int main(int argc, char **argv) { (void) argc; (void) argv; int ret; ret = fork(); CHECK(ret >= 0); if (ret) run_parent(); else run_child(); return 0; }
static void run_dvd_player() { char *argv[] = { "/usr/local/bin/DvdPlayer", NULL }; for(;;) { int status; msg("dvdplayer>"); reset(); if (g_bPowerdown) { system("echo O | /usr/local/bin/DvdPlayer"); } else { status = run_child(argv); } if (WEXITSTATUS(status) == 99 || g_bPowerdown) { g_bPowerdown = false; system("rmmod ieee80211_crypt"); system("rmmod ieee80211_rtl"); system("rmmod r8187"); system("rmmod ohci_hcd"); system("rmmod ehci_hcd"); system("echo mem>/sys/power/state"); sleep(5); system("rm -rf /tmp/usbmounts/* /tmp/usbmounts/.* &"); system("/sbin/modprobe ehci-hcd && /sbin/modprobe ohci-hcd &"); continue; } else if (WEXITSTATUS(status) == 88) { break; } system("reboot"); sleep(20); } }
int main (int argc, char const *const *argv, char const *const *envp) { char const *s = env_get2(envp, VAR) ; unsigned int fd = 1 ; unsigned int timeout = 0 ; int df = 1, keep = 0 ; PROG = "sdnotify-wrapper" ; { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { register int opt = subgetopt_r(argc, argv, "d:ft:k", &l) ; if (opt == -1) break ; switch (opt) { case 'd' : if (!uint0_scan(l.arg, &fd)) dieusage() ; break ; case 'f' : df = 0 ; break ; case 't' : if (!uint0_scan(l.arg, &timeout)) dieusage() ; break ; case 'k' : keep = 1 ; break ; default : dieusage() ; } } argc -= l.ind ; argv += l.ind ; } if (!argc) dieusage() ; if (!s) xpathexec_run(argv[0], argv, envp) ; else { pid_t parent = getpid() ; pid_t child ; int p[2] ; if (pipe(p) < 0) strerr_diefu1sys(111, "pipe") ; child = df ? doublefork() : fork() ; if (child < 0) strerr_diefu1sys(111, df ? "doublefork" : "fork") ; else if (!child) { PROG = "sdnotify-wrapper (child)" ; close(p[1]) ; return run_child(p[0], timeout, parent, s) ; } close(p[0]) ; if (fd_move((int)fd, p[1]) < 0) strerr_diefu1sys(111, "move descriptor") ; if (keep) xpathexec_run(argv[0], argv, envp) ; else xpathexec_r(argv, envp, env_len(envp), VAR, sizeof(VAR)) ; } }
int main (void) { pid_t pid; struct sigaction sa; sigset_t sigSet; // setup signal handler sigemptyset (&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = signal_handler; if (sigaction (SIGUSR1, &sa, NULL) == -1) { perror ("sigaction()"); return 1; } // block SIGUSR1 to start if (sigemptyset (&sigSet) == -1) { perror ("sigemptyset()"); return 1; } if (sigaddset (&sigSet, SIGUSR1) == -1) { perror ("sigaddset()"); return 1; } if (sigprocmask (SIG_SETMASK, &sigSet, NULL) == -1) { perror ("sigprocmask()"); return 1; } pid = fork (); switch (pid) { case -1: // error perror ("fork()"); return 1; case 0: // child run_child (); break; default: // parent run_parent (pid); break; } return 0; }