void userdel_main(void) { struct passwd *pwd = NULL; pwd = xgetpwnam(*toys.optargs); update_password("/etc/passwd", pwd->pw_name, NULL); update_password("/etc/shadow", pwd->pw_name, NULL); // delete the group named USER, and remove user from group. // could update_password() be used for this? // not a good idea, as update_passwd() updates one entry at a time // in this case it will be modifying the files as many times the // USER appears in group database files. So the customized version // of update_passwd() is here. update_groupfiles("/etc/group", *toys.optargs); update_groupfiles("/etc/gshadow", *toys.optargs); if (toys.optflags & FLAG_r) { char *arg[] = {"rm", "-fr", pwd->pw_dir, NULL, NULL}; sprintf(toybuf, "/var/spool/mail/%s",pwd->pw_name); arg[3] = toybuf; xexec(arg); } }
void time_main(void) { pid_t pid; struct timeval tv, tv2; gettimeofday(&tv, NULL); if (!(pid = xfork())) xexec(toys.optargs); else { int stat; struct rusage ru; float r, u, s; wait4(pid, &stat, 0, &ru); gettimeofday(&tv2, NULL); if (tv.tv_usec > tv2.tv_usec) { tv2.tv_usec += 1000000; tv2.tv_sec--; } r = (tv2.tv_sec-tv.tv_sec)+((tv2.tv_usec-tv.tv_usec)/1000000.0); u = ru.ru_utime.tv_sec+(ru.ru_utime.tv_usec/1000000.0); s = ru.ru_stime.tv_sec+(ru.ru_stime.tv_usec/1000000.0); fprintf(stderr, "real %f\nuser %f\nsys %f\n", r, u, s); toys.exitval = WIFEXITED(stat) ? WEXITSTATUS(stat) : WTERMSIG(stat); } }
void reset_main(void) { char *args[] = {"stty", "sane", NULL}; /* \033c - reset the terminal with default setting * \033(B - set the G0 character set (B=US) * \033[2J - clear the whole screen * \033[0m - Reset all attributes */ if (isatty(1)) xprintf("\033c\033(B\033[0m\033[J\033[?25h"); fflush(stdout); // set the terminal to sane settings xexec(args); }
void nohup_main(void) { xsignal(SIGHUP, SIG_IGN); if (isatty(1)) { close(1); if (-1 == open("nohup.out", O_CREAT|O_APPEND|O_WRONLY, S_IRUSR|S_IWUSR )) { char *temp = getenv("HOME"); temp = xmprintf("%s/%s", temp ? temp : "", "nohup.out"); xcreate(temp, O_CREAT|O_APPEND|O_WRONLY, 0600); free(temp); } } if (isatty(0)) { close(0); xopen_stdio("/dev/null", O_RDONLY); } xexec(toys.optargs); }
void sys_exec_checkpoint_script (const char *script_name, const char *archive_name, int checkpoint_number) { pid_t pid; char uintbuf[UINTMAX_STRSIZE_BOUND]; pid = xfork (); if (pid != 0) { /* Master */ int status; while (waitpid (pid, &status, 0) == -1) if (errno != EINTR) { waitpid_error (script_name); break; } return; } /* Child */ setenv ("TAR_VERSION", PACKAGE_VERSION, 1); setenv ("TAR_ARCHIVE", archive_name, 1); setenv ("TAR_CHECKPOINT", STRINGIFY_BIGINT (checkpoint_number, uintbuf), 1); setenv ("TAR_BLOCKING_FACTOR", STRINGIFY_BIGINT (blocking_factor, uintbuf), 1); setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option), 1); setenv ("TAR_FORMAT", archive_format_string (current_format == DEFAULT_FORMAT ? archive_format : current_format), 1); priv_set_restore_linkdir (); xexec (script_name); }
cmain() { /* set up sector buffers */ bcbx[0].b_link = &bcbx[1]; bcbx[2].b_link = &bcbx[3]; bcbx[0].b_bufdrv = -1; bcbx[1].b_bufdrv = -1; bcbx[2].b_bufdrv = -1; bcbx[3].b_bufdrv = -1; bcbx[0].b_bufr = &secbuf[0][0]; bcbx[1].b_bufr = &secbuf[1][0]; bcbx[2].b_bufr = &secbuf[2][0]; bcbx[3].b_bufr = &secbuf[3][0]; bufl[0] = &bcbx[0]; /* fat buffers */ bufl[1] = &bcbx[2]; /* dir/data buffers */ osinit(); #if floppy xsetdrv(0); /* 0 if GEMDOSFI.SYS, 2 if GEMDOSHI.SYS */ #else xsetdrv(2); #endif xexec(0,"COMMAND.PRG","",env); }
int sys_exec_command (char *file_name, int typechar, struct tar_stat_info *st) { int p[2]; xpipe (p); pipe_handler = signal (SIGPIPE, SIG_IGN); global_pid = xfork (); if (global_pid != 0) { xclose (p[PREAD]); return p[PWRITE]; } /* Child */ xdup2 (p[PREAD], STDIN_FILENO); xclose (p[PWRITE]); stat_to_env (file_name, typechar, st); priv_set_restore_linkdir (); xexec (to_command_option); }
static void startdevproc(void *a) { Sarg *sa = a; Dev *d; Devtab *dt; int argc; char *args, *argse, **argv; char *fname; threadsetgrp(threadid()); d = sa->pp->dev; dt = sa->dt; args = sa->args; argse = sa->args + sizeof sa->args; argv = sa->argv; fname = sa->fname; sa->pp->devmaskp = &dt->devmask; sa->pp->devnb = getdevnb(&dt->devmask); if(sa->pp->devnb < 0){ sa->pp->devmaskp = nil; sa->pp->devnb = 0; }else args = seprint(args, argse, "-N %d", sa->pp->devnb); if(dt->args != nil) seprint(args, argse, " %s", dt->args); args = sa->args; dprint(2, "%s: start: %s %s\n", argv0, dt->name, args); argv[0] = dt->name; argc = 1; if(args[0] != 0) argc += tokenize(args, argv+1, nelem(sa->argv)-2); argv[argc] = nil; if(dt->init == nil){ if(d->dfd > 0 ){ close(d->dfd); d->dfd = -1; } rfork(RFCFDG); open("/dev/null", OREAD); open("/dev/cons", OWRITE); open("/dev/cons", OWRITE); xexec(sa->rc, argv[0], argv); snprint(fname, sizeof(sa->fname), "/bin/usb/%s", dt->name); xexec(sa->rc, fname, argv); snprint(fname, sizeof(sa->fname), "/boot/%s", dt->name); xexec(sa->rc, fname, argv); if(cputype == nil) cputype = getenv("cputype"); if(cputype != nil){ snprint(fname, sizeof(sa->fname), "/%s/bin/%s", cputype, dt->name); argv[0] = fname; xexec(sa->rc, fname, argv); } fprint(2, "%s: %s: not found. can't exec\n", argv0, dt->name); sendul(sa->rc, -1); threadexits("exec"); }else{ sa->pp->dev = opendev(d->dir); sendul(sa->rc, 0); if(dt->init(d, argc, argv) < 0) fprint(2, "%s: %s: %r\n", argv0, dt->name); closedev(d); free(sa); } threadexits(nil); }
int sys_exec_info_script (const char **archive_name, int volume_number) { pid_t pid; char uintbuf[UINTMAX_STRSIZE_BOUND]; int p[2]; static RETSIGTYPE (*saved_handler) (int sig); xpipe (p); saved_handler = signal (SIGPIPE, SIG_IGN); pid = xfork (); if (pid != 0) { /* Master */ int rc; int status; char *buf = NULL; size_t size = 0; FILE *fp; xclose (p[PWRITE]); fp = fdopen (p[PREAD], "r"); rc = getline (&buf, &size, fp); fclose (fp); if (rc > 0 && buf[rc-1] == '\n') buf[--rc] = 0; while (waitpid (pid, &status, 0) == -1) if (errno != EINTR) { signal (SIGPIPE, saved_handler); waitpid_error (info_script_option); return -1; } signal (SIGPIPE, saved_handler); if (WIFEXITED (status)) { if (WEXITSTATUS (status) == 0 && rc > 0) *archive_name = buf; else free (buf); return WEXITSTATUS (status); } free (buf); return -1; } /* Child */ setenv ("TAR_VERSION", PACKAGE_VERSION, 1); setenv ("TAR_ARCHIVE", *archive_name, 1); setenv ("TAR_VOLUME", STRINGIFY_BIGINT (volume_number, uintbuf), 1); setenv ("TAR_BLOCKING_FACTOR", STRINGIFY_BIGINT (blocking_factor, uintbuf), 1); setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option), 1); setenv ("TAR_FORMAT", archive_format_string (current_format == DEFAULT_FORMAT ? archive_format : current_format), 1); setenv ("TAR_FD", STRINGIFY_BIGINT (p[PWRITE], uintbuf), 1); xclose (p[PREAD]); priv_set_restore_linkdir (); xexec (info_script_option); }
/* Set ARCHIVE for writing, then compressing an archive. */ pid_t sys_child_open_for_compress (void) { int parent_pipe[2]; int child_pipe[2]; pid_t grandchild_pid; pid_t child_pid; xpipe (parent_pipe); child_pid = xfork (); if (child_pid > 0) { /* The parent tar is still here! Just clean up. */ archive = parent_pipe[PWRITE]; xclose (parent_pipe[PREAD]); return child_pid; } /* The new born child tar is here! */ set_program_name (_("tar (child)")); signal (SIGPIPE, SIG_DFL); xdup2 (parent_pipe[PREAD], STDIN_FILENO); xclose (parent_pipe[PWRITE]); /* Check if we need a grandchild tar. This happens only if either: a) the file is to be accessed by rmt: compressor doesn't know how; b) the file is not a plain file. */ if (!_remdev (archive_name_array[0]) && is_regular_file (archive_name_array[0])) { if (backup_option) maybe_backup_file (archive_name_array[0], 1); /* We don't need a grandchild tar. Open the archive and launch the compressor. */ if (strcmp (archive_name_array[0], "-")) { archive = creat (archive_name_array[0], MODE_RW); if (archive < 0) { int saved_errno = errno; if (backup_option) undo_last_backup (); errno = saved_errno; open_fatal (archive_name_array[0]); } xdup2 (archive, STDOUT_FILENO); } priv_set_restore_linkdir (); xexec (use_compress_program_option); } /* We do need a grandchild tar. */ xpipe (child_pipe); grandchild_pid = xfork (); if (grandchild_pid == 0) { /* The newborn grandchild tar is here! Launch the compressor. */ set_program_name (_("tar (grandchild)")); xdup2 (child_pipe[PWRITE], STDOUT_FILENO); xclose (child_pipe[PREAD]); priv_set_restore_linkdir (); xexec (use_compress_program_option); } /* The child tar is still here! */ /* Prepare for reblocking the data from the compressor into the archive. */ xdup2 (child_pipe[PREAD], STDIN_FILENO); xclose (child_pipe[PWRITE]); if (strcmp (archive_name_array[0], "-") == 0) archive = STDOUT_FILENO; else { archive = rmtcreat (archive_name_array[0], MODE_RW, rsh_command_option); if (archive < 0) open_fatal (archive_name_array[0]); } /* Let's read out of the stdin pipe and write an archive. */ while (1) { size_t status = 0; char *cursor; size_t length; /* Assemble a record. */ for (length = 0, cursor = record_start->buffer; length < record_size; length += status, cursor += status) { size_t size = record_size - length; status = safe_read (STDIN_FILENO, cursor, size); if (status == SAFE_READ_ERROR) read_fatal (use_compress_program_option); if (status == 0) break; } /* Copy the record. */ if (status == 0) { /* We hit the end of the file. Write last record at full length, as the only role of the grandchild is doing proper reblocking. */ if (length > 0) { memset (record_start->buffer + length, 0, record_size - length); status = sys_write_archive_buffer (); if (status != record_size) archive_write_error (status); } /* There is nothing else to read, break out. */ break; } status = sys_write_archive_buffer (); if (status != record_size) archive_write_error (status); } wait_for_grandchild (grandchild_pid); }