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 pwdx_main(void) { char **optargs; for (optargs = toys.optargs; *optargs; optargs++) { char *path; int num_bytes; path = xmprintf("/proc/%s/cwd", *optargs); num_bytes = readlink(path, toybuf, sizeof(toybuf)-1); free(path); if (num_bytes==-1) { path = strerror(errno); toys.exitval = 1; } else { path = toybuf; toybuf[num_bytes] = 0; } xprintf("%s: %s\n", *optargs, path); } }
static void update_groupfiles(char *filename, char* username) { char *filenamesfx = NULL, *sfx = NULL, *line = NULL; FILE *exfp, *newfp; int ulen = strlen(username); struct flock lock; filenamesfx = xmprintf("%s+", filename); sfx = strchr(filenamesfx, '+'); exfp = xfopen(filename, "r+"); *sfx = '-'; unlink(filenamesfx); if (link(filename, filenamesfx)) error_msg("Can't create backup file"); *sfx = '+'; lock.l_type = F_WRLCK; lock.l_whence = SEEK_SET; lock.l_start = lock.l_len = 0; if (fcntl(fileno(exfp), F_SETLK, &lock) < 0) perror_msg("Couldn't lock file %s",filename); lock.l_type = F_UNLCK; //unlocking at a later stage newfp = xfopen(filenamesfx, "w+"); while ((line = get_line(fileno(exfp))) != NULL){ sprintf(toybuf, "%s:",username); if (!strncmp(line, toybuf, ulen+1)) goto LOOP; else { char *n, *p = strrchr(line, ':'); if (p && *++p && (n = strstr(p, username))) { do { if (n[ulen] == ',') { *n = '\0'; n += ulen + 1; fprintf(newfp, "%s%s\n", line, n); break; } else if (!n[ulen]) { if (n[-1] == ',') n[-1] = *n = '\0'; if (n[-1] == ':') *n = '\0'; fprintf(newfp, "%s%s\n", line, n); break; } else n += ulen; } while (*n && (n=strstr(n, username))); if (!n) fprintf(newfp, "%s\n", line); } else fprintf(newfp, "%s\n", line); } LOOP: free(line); } fcntl(fileno(exfp), F_SETLK, &lock); fclose(exfp); errno = 0; fflush(newfp); fsync(fileno(newfp)); fclose(newfp); rename(filenamesfx, filename); if (errno){ perror_msg("File Writing/Saving failed: "); unlink(filenamesfx); } free(filenamesfx); }
static void inittab_parsing(void) { int i, fd, line_number = 0, token_count = 0; char *p, *q, *extracted_token, *tty_name = NULL, *command = NULL, *tmp; uint8_t action = 0; char *act_name = "sysinit\0wait\0once\0respawn\0askfirst\0ctrlaltdel\0" "shutdown\0restart\0"; fd = open("/etc/inittab", O_RDONLY); if (fd < 0) { error_msg("Unable to open /etc/inittab. Using Default inittab"); add_new_action(SYSINIT, "/etc/init.d/rcS", ""); add_new_action(RESPAWN, "/sbin/getty -n -l /bin/sh -L 115200 tty1 vt100", ""); } else { while((q = p = get_line(fd))) { //read single line from /etc/inittab char *x; if ((x = strchr(p, '#'))) *x = '\0'; line_number++; token_count = 0; action = 0; while ((extracted_token = strsep(&p,":"))) { token_count++; switch (token_count) { case 1: if (*extracted_token) { if (!strncmp(extracted_token, "/dev/", 5)) tty_name = xmprintf("%s",extracted_token); else tty_name = xmprintf("/dev/%s",extracted_token); } else tty_name = xstrdup(""); break; case 2: break; case 3: for (tmp = act_name, i = 0; *tmp; i++, tmp += strlen(tmp) +1) { if (!strcmp(tmp, extracted_token)) { action = 1 << i; break; } } if (!*tmp) error_msg("Invalid action at line number %d ---- ignoring",line_number); break; case 4: command = xstrdup(extracted_token); break; default: error_msg("Bad inittab entry at line %d", line_number); break; } } //while token if (q) free(q); if (token_count != 4) continue; if (action) add_new_action(action, command, tty_name); free(tty_name); free(command); } //while line close(fd); } }