int main(int argc, char *argv[]) { int i, rv, pid_t[128]; if ( argc != 2 ) { fprintf(stdout,"Usage %s procname\n",argv[0]); return 0; } rv = find_pid_by_name( argv[1], pid_t); if(!rv) { for(i=0; pid_t[i] != 0; i++) printf("%d\n", pid_t[i]); } return 0; }
static void do_tracing_by_name_internal(const char *progname, const char *filename) { /* If process already running, start tracing */ uint32_t pid = find_pid_by_name(progname); uint32_t minus_one = (uint32_t)(-1); if (pid != minus_one) { do_tracing_internal(pid,filename); } else { /* Otherwise, start monitoring for process start */ procname_set(progname); strncpy(tracefile, filename, 256); monitor_printf (default_mon, "Waiting for process %s to start\n", progname); } /* Print configuration variables */ //print_conf_vars(); }
extern int pidof_main(int argc, char **argv) { int opt, n = 0; int single_flag = 0; int fail = 1; /* do normal option parsing */ while ((opt = getopt(argc, argv, "s")) > 0) { switch (opt) { case 's': single_flag = 1; break; default: bb_show_usage(); } } /* Looks like everything is set to go. */ while(optind < argc) { long *pidList; long *pl; pidList = find_pid_by_name(argv[optind]); for(pl = pidList; *pl > 0; pl++) { printf("%s%ld", (n++ ? " " : ""), *pl); fail = 0; if (single_flag) break; } free(pidList); optind++; } printf("\n"); return fail ? EXIT_FAILURE : EXIT_SUCCESS; }
extern int kill_main(int argc, char **argv) { int whichApp, sig = SIGTERM, quiet, errors; const char *name; #ifdef BB_KILLALL /* Figure out what we are trying to do here */ whichApp = (strcmp(applet_name, "killall") == 0)? KILLALL : KILL; #else whichApp = KILL; #endif errors=0; quiet=0; argc--; argv++; /* Parse any options */ if (argc < 1) show_usage(); while (argc > 0 && **argv == '-') { while (*++(*argv)) { switch (**argv) { #ifdef BB_KILLALL case 'q': quiet++; break; #endif case 'l': if(argc>1) { for(argv++; *argv; argv++) { name = u_signal_names(*argv, &sig, -1); if(name!=NULL) printf("%s\n", name); } } else { int col = 0; for(sig=1; sig < NSIG; sig++) { name = u_signal_names(0, &sig, 1); if(name==NULL) /* unnamed */ continue; col += printf("%2d) %-16s", sig, name); if (col > 60) { printf("\n"); col = 0; } } printf("\n"); } return EXIT_SUCCESS; case '-': show_usage(); default: name = u_signal_names(*argv, &sig, 0); if(name==NULL) error_msg_and_die( "bad signal name: %s", *argv); argc--; argv++; goto do_it_now; } argc--; argv++; } } do_it_now: if (whichApp == KILL) { /* Looks like they want to do a kill. Do that */ while (--argc >= 0) { int pid; if (!isdigit(**argv)) perror_msg_and_die( "Bad PID"); pid = strtol(*argv, NULL, 0); if (kill(pid, sig) != 0) { perror_msg( "Could not kill pid '%d'", pid); errors++; } argv++; } } #ifdef BB_KILLALL else { pid_t myPid=getpid(); /* Looks like they want to do a killall. Do that */ while (--argc >= 0) { long* pidList; pidList = find_pid_by_name(*argv); if (!pidList || *pidList<=0) { errors++; if (!quiet) error_msg( "%s: no process killed", *argv); } else { for(; *pidList!=0; pidList++) { if (*pidList==myPid) continue; if (kill(*pidList, sig) != 0) { errors++; if (!quiet) perror_msg( "Could not kill pid '%d'", *pidList); } } } /* Note that we don't bother to free the memory * allocated in find_pid_by_name(). It will be freed * upon exit, so we can save a byte or two */ argv++; } } #endif return errors; }
int main(int argc, char *argv[]) { int i; pid_t pid; char *package_name = NULL; char *service_name = NULL; char *daemon_file_dir = NULL; int interval = SLEEP_INTERVAL; LOGI(LOG_TAG, "Copyright (c) 2015, Vincent Cheung<*****@*****.**>"); if (argc < 7) { LOGE(LOG_TAG, "usage: %s -p package-name -s " "daemon-service-name -t interval-time", argv[0]); return; } for (i = 0; i < argc; i ++) { if (!strcmp("-p", argv[i])) { package_name = argv[i + 1]; LOGD(LOG_TAG, "package name: %s", package_name); } if (!strcmp("-s", argv[i])) { service_name = argv[i + 1]; LOGD(LOG_TAG, "service name: %s", service_name); } if (!strcmp("-t", argv[i])) { interval = atoi(argv[i + 1]); LOGD(LOG_TAG, "interval: %d", interval); } } /* package name and service name should not be null */ if (package_name == NULL || service_name == NULL) { LOGE(LOG_TAG, "package name or service name is null"); return; } if ((pid = fork()) < 0) { exit(EXIT_SUCCESS); } else if (pid == 0) { /* add signal */ signal(SIGTERM, sigterm_handler); /* become session leader */ setsid(); /* change work directory */ chdir("/"); for (i = 0; i < MAXFILE; i ++) { close(i); } /* find pid by name and kill them */ int pid_list[100]; int total_num = find_pid_by_name(argv[0], pid_list); LOGD(LOG_TAG, "total num %d", total_num); for (i = 0; i < total_num; i ++) { int retval = 0; int daemon_pid = pid_list[i]; if (daemon_pid > 1 && daemon_pid != getpid()) { retval = kill(daemon_pid, SIGTERM); if (!retval) { LOGD(LOG_TAG, "kill daemon process success: %d", daemon_pid); } else { LOGD(LOG_TAG, "kill daemon process %d fail: %s", daemon_pid, strerror(errno)); exit(EXIT_SUCCESS); } } } LOGD(LOG_TAG, "child process fork ok, daemon start: %d", getpid()); while(sig_running) { interval = interval < SLEEP_INTERVAL ? SLEEP_INTERVAL : interval; select_sleep(interval, 0); LOGD(LOG_TAG, "check the service once, interval: %d", interval); /* start service */ start_service(package_name, service_name); } exit(EXIT_SUCCESS); } else { /* parent process */ exit(EXIT_SUCCESS); } }
int halt_main(int argc UNUSED_PARAM, char **argv) { static const int magic[] = { RB_HALT_SYSTEM, RB_POWER_OFF, RB_AUTOBOOT }; static const smallint signals[] = { SIGUSR1, SIGUSR2, SIGTERM }; int delay = 0; int which, flags, rc; /* Figure out which applet we're running */ for (which = 0; "hpr"[which] != applet_name[0]; which++) continue; /* Parse and handle arguments */ opt_complementary = "d+"; /* -d N */ /* We support -w even if !ENABLE_FEATURE_WTMP, * in order to not break scripts. * -i (shut down network interfaces) is ignored. */ flags = getopt32(argv, "d:nfwi", &delay); sleep(delay); write_wtmp(); if (flags & 8) /* -w */ return EXIT_SUCCESS; if (!(flags & 2)) /* no -n */ sync(); /* Perform action. */ rc = 1; if (!(flags & 4)) { /* no -f */ //TODO: I tend to think that signalling linuxrc is wrong // pity original author didn't comment on it... if (ENABLE_FEATURE_INITRD) { /* talk to linuxrc */ /* bbox init/linuxrc assumed */ pid_t *pidlist = find_pid_by_name("linuxrc"); if (pidlist[0] > 0) rc = kill(pidlist[0], signals[which]); if (ENABLE_FEATURE_CLEAN_UP) free(pidlist); } if (rc) { /* talk to init */ if (!ENABLE_FEATURE_CALL_TELINIT) { /* bbox init assumed */ rc = kill(1, signals[which]); } else { /* SysV style init assumed */ /* runlevels: * 0 == shutdown * 6 == reboot */ rc = execlp(CONFIG_TELINIT_PATH, CONFIG_TELINIT_PATH, which == 2 ? "6" : "0", (char *)NULL ); } } } else { rc = reboot(magic[which]); } if (rc) bb_perror_nomsg_and_die(); return rc; }
static void display_verbose(void) { security_context_t con, _con; char *fc[50], *pc[50], *cterm; pid_t *pidList; int i; read_config(pc, ARRAY_SIZE(pc), fc, ARRAY_SIZE(fc)); /* process contexts */ puts("\nProcess contexts:"); /* current context */ if (getcon(&con) == 0) { printf(COL_FMT "%s\n", "Current context:", con); if (ENABLE_FEATURE_CLEAN_UP) freecon(con); } /* /sbin/init context */ if (getpidcon(1, &con) == 0) { printf(COL_FMT "%s\n", "Init context:", con); if (ENABLE_FEATURE_CLEAN_UP) freecon(con); } /* [process] context */ for (i = 0; pc[i] != NULL; i++) { pidList = find_pid_by_name(bb_basename(pc[i])); if (pidList[0] > 0 && getpidcon(pidList[0], &con) == 0) { printf(COL_FMT "%s\n", pc[i], con); if (ENABLE_FEATURE_CLEAN_UP) freecon(con); } if (ENABLE_FEATURE_CLEAN_UP) free(pidList); } /* files contexts */ puts("\nFile contexts:"); cterm = ttyname(0); puts(cterm); if (cterm && lgetfilecon(cterm, &con) >= 0) { printf(COL_FMT "%s\n", "Controlling term:", con); if (ENABLE_FEATURE_CLEAN_UP) freecon(con); } for (i=0; fc[i] != NULL; i++) { struct stat stbuf; if (lgetfilecon(fc[i], &con) < 0) continue; if (lstat(fc[i], &stbuf) == 0) { if (S_ISLNK(stbuf.st_mode)) { if (getfilecon(fc[i], &_con) >= 0) { printf(COL_FMT "%s -> %s\n", fc[i], _con, con); if (ENABLE_FEATURE_CLEAN_UP) freecon(_con); } } else { printf(COL_FMT "%s\n", fc[i], con); } } if (ENABLE_FEATURE_CLEAN_UP) freecon(con); } }
int pidof_main(int argc UNUSED_PARAM, char **argv) { unsigned first = 1; unsigned opt; #if ENABLE_FEATURE_PIDOF_OMIT llist_t *omits = NULL; /* list of pids to omit */ opt_complementary = "o::"; #endif /* do unconditional option parsing */ opt = getopt32(argv, "" IF_FEATURE_PIDOF_SINGLE ("s") IF_FEATURE_PIDOF_OMIT("o:", &omits)); #if ENABLE_FEATURE_PIDOF_OMIT /* fill omit list. */ { llist_t *omits_p = omits; while (1) { omits_p = llist_find_str(omits_p, "%PPID"); if (!omits_p) break; /* are we asked to exclude the parent's process ID? */ omits_p->data = utoa((unsigned)getppid()); } } #endif /* Looks like everything is set to go. */ argv += optind; while (*argv) { pid_t *pidList; pid_t *pl; /* reverse the pidlist like GNU pidof does. */ pidList = pidlist_reverse(find_pid_by_name(*argv)); for (pl = pidList; *pl; pl++) { #if ENABLE_FEATURE_PIDOF_OMIT if (opt & OPT_OMIT) { llist_t *omits_p = omits; while (omits_p) { if (xatoul(omits_p->data) == (unsigned long)(*pl)) { goto omitting; } omits_p = omits_p->link; } } #endif printf(" %u" + first, (unsigned)*pl); first = 0; if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE)) break; #if ENABLE_FEATURE_PIDOF_OMIT omitting: ; #endif } free(pidList); argv++; } if (!first) bb_putchar('\n'); #if ENABLE_FEATURE_PIDOF_OMIT if (ENABLE_FEATURE_CLEAN_UP) llist_free(omits, NULL); #endif return first; /* 1 (failure) - no processes found */ }
int main(int argc, char *argv[]) { int i; int should_open_browser = 0; char *package_name = NULL; char *url = NULL; char *url_file_path = NULL; for (i = 0; i < argc; i ++) { if (!strcmp("-p", argv[i])) { package_name = argv[i + 1]; LOGD(LOG_TAG, "package name: %s", package_name); } if (!strcmp("-u", argv[i])) { url = argv[i + 1]; LOGD(LOG_TAG, "url: %s", url); } if (!strcmp("-f", argv[i])) { url_file_path = argv[i + 1]; LOGD(LOG_TAG, "url file path: %s", url_file_path); } if (!strcmp("-b", argv[i])) { should_open_browser = atoi(argv[i + 1]); LOGD(LOG_TAG, "should open brwoser: %d", should_open_browser); } } /* get the directory for watcher */ char *app_dir = str_stitching("/data/data/", package_name); char *lib_dir = str_stitching(app_dir, "/lib"); char *watch_file_path = str_stitching(app_dir, "/uninstall.watch"); /* the file path should not be null */ if (watch_file_path == NULL) { LOGE(LOG_TAG, "watch file path is NULL"); exit(EXIT_FAILURE); } /* avoid zombie process */ signal(SIGCHLD, sig_child); /* find pid by name and kill them */ int pid_list[100]; int total_num = find_pid_by_name(argv[0], pid_list); for (i = 0; i < total_num; i ++) { int retval = 0; int watcher_pid = pid_list[i]; if (watcher_pid > 1 && watcher_pid != getpid()) { retval = kill(watcher_pid, SIGKILL); if (!retval) { LOGD(LOG_TAG, "kill watcher process success: %d", watcher_pid); } else { LOGD(LOG_TAG, "kill wathcer process %d fail: %s", watcher_pid, strerror(errno)); exit(EXIT_SUCCESS); } } } /* get child process */ pid_t pid = fork(); if (pid < 0) { LOGE(LOG_TAG, "fork failed"); } else if (pid == 0) { /* inotify init */ int fd = inotify_init(); if (fd < 0) { LOGE(LOG_TAG, "inotify_init init failed"); exit(EXIT_FAILURE); } int w_fd = open(watch_file_path, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO); if (w_fd < 0) { LOGE(LOG_TAG, "open watch file error"); exit(EXIT_FAILURE); } close(w_fd); /* add watch in inotify */ int watch_fd = inotify_add_watch(fd, watch_file_path, IN_DELETE); if (watch_fd < 0) { LOGE(LOG_TAG, "inotify_add_watch failed"); exit(EXIT_FAILURE); } void *p_buf = malloc(sizeof(struct inotify_event)); if (p_buf == NULL) { LOGD(LOG_TAG, "malloc inotify event failed"); exit(EXIT_FAILURE); } LOGD(LOG_TAG, "watcher process fork ok, start to watch"); while (1) { /* read will block process */ size_t read_bytes = read(fd, p_buf, sizeof(struct inotify_event)); /* delay 200ms */ usleep(200*1000); /* to check if the app has uninstalled, indeed */ FILE *lib_dir_file = fopen(lib_dir, "r"); FILE *app_dir_file = fopen(app_dir, "r"); if (lib_dir_file == NULL || app_dir_file == NULL) { break; } else { /* close app dir file */ fclose(lib_dir_file); fclose(app_dir_file); /* add notify watch again */ int w_fd = open(watch_file_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO); close(w_fd); int watch_fd = inotify_add_watch(fd, watch_file_path, IN_DELETE); if (watch_fd < 0) { LOGE(LOG_TAG, "inotify_add_watch failed"); free(p_buf); exit(EXIT_FAILURE); } } } free(p_buf); inotify_rm_watch(fd, IN_DELETE); LOGD(LOG_TAG, "the app has been uninstalled, call url"); /* if the url was saved in file, read out */ if (url_file_path != NULL) { int url_fd = open(url_file_path, O_RDONLY); if (url_fd < 0) { LOGE(LOG_TAG, "url file open error"); exit(EXIT_FAILURE); } char buf[300] = {0}; if (read(url_fd, buf, 300) > 0) { url = buf; LOGD(LOG_TAG, "url from file: %s", url); } close(url_fd); } /* call url */ chttp_get(url); /* open browser if needed */ if (should_open_browser) { open_browser(url); } exit(EXIT_SUCCESS); } else { /* parent process */ exit(EXIT_SUCCESS); } }
int halt_main(int argc ATTRIBUTE_UNUSED, char **argv) { static const int magic[] = { #ifdef RB_HALT_SYSTEM RB_HALT_SYSTEM, #elif defined RB_HALT RB_HALT, #endif #ifdef RB_POWER_OFF RB_POWER_OFF, #elif defined RB_POWERDOWN RB_POWERDOWN, #endif RB_AUTOBOOT }; static const smallint signals[] = { SIGUSR1, SIGUSR2, SIGTERM }; int delay = 0; int which, flags, rc = 1; #if ENABLE_FEATURE_WTMP struct utmp utmp; struct utsname uts; #endif /* Figure out which applet we're running */ for (which = 0; "hpr"[which] != *applet_name; which++) continue; /* Parse and handle arguments */ opt_complementary = "d+"; /* -d N */ flags = getopt32(argv, "d:nfw", &delay); sleep(delay); #if ENABLE_FEATURE_WTMP if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) { close(creat(bb_path_wtmp_file, 0664)); } memset(&utmp, 0, sizeof(utmp)); utmp.ut_tv.tv_sec = time(NULL); safe_strncpy(utmp.ut_user, "shutdown", UT_NAMESIZE); utmp.ut_type = RUN_LVL; safe_strncpy(utmp.ut_id, "~~", sizeof(utmp.ut_id)); safe_strncpy(utmp.ut_line, "~~", UT_LINESIZE); if (uname(&uts) == 0) safe_strncpy(utmp.ut_host, uts.release, sizeof(utmp.ut_host)); updwtmp(bb_path_wtmp_file, &utmp); #endif /* !ENABLE_FEATURE_WTMP */ if (flags & 8) /* -w */ return 0; if (!(flags & 2)) /* no -n */ sync(); /* Perform action. */ if (ENABLE_INIT && !(flags & 4)) { if (ENABLE_FEATURE_INITRD) { pid_t *pidlist = find_pid_by_name("linuxrc"); if (pidlist[0] > 0) rc = kill(pidlist[0], signals[which]); if (ENABLE_FEATURE_CLEAN_UP) free(pidlist); } if (rc) rc = kill(1, signals[which]); } else rc = reboot(magic[which]); if (rc) bb_error_msg("no"); return rc; }
extern int kill_main(int argc, char **argv) { int whichApp, signo = SIGTERM, quiet = 0; const char *name; int errors = 0; #ifdef CONFIG_KILLALL /* Figure out what we are trying to do here */ whichApp = (strcmp(bb_applet_name, "killall") == 0)? KILLALL : KILL; #else whichApp = KILL; #endif /* Parse any options */ if (argc < 2) bb_show_usage(); if(argv[1][0] != '-'){ argv++; argc--; goto do_it_now; } /* The -l option, which prints out signal names. */ if(argv[1][1]=='l' && argv[1][2]=='\0'){ if(argc==2) { /* Print the whole signal list */ int col = 0; for(signo=1; signo < NSIG; signo++) { name = u_signal_names(0, &signo, 1); if(name==NULL) /* unnamed */ continue; col += printf("%2d) %-16s", signo, name); if (col > 60) { printf("\n"); col = 0; } } printf("\n"); } else { for(argv++; *argv; argv++) { name = u_signal_names(*argv, &signo, -1); if(name!=NULL) printf("%s\n", name); } } /* If they specified -l, were all done */ return EXIT_SUCCESS; } /* The -q quiet option */ if(argv[1][1]=='q' && argv[1][2]=='\0'){ quiet++; argv++; argc--; if(argc<2 || argv[1][0] != '-'){ goto do_it_now; } } if(!u_signal_names(argv[1]+1, &signo, 0)) bb_error_msg_and_die( "bad signal name '%s'", argv[1]+1); argv+=2; argc-=2; do_it_now: if (whichApp == KILL) { /* Looks like they want to do a kill. Do that */ while (--argc >= 0) { int pid; if (!isdigit(**argv)) bb_error_msg_and_die( "Bad PID '%s'", *argv); pid = strtol(*argv, NULL, 0); if (kill(pid, signo) != 0) { bb_perror_msg( "Could not kill pid '%d'", pid); errors++; } argv++; } } #ifdef CONFIG_KILLALL else { pid_t myPid=getpid(); /* Looks like they want to do a killall. Do that */ while (--argc >= 0) { long* pidList; pidList = find_pid_by_name(*argv); if (!pidList || *pidList<=0) { errors++; if (quiet==0) bb_error_msg( "%s: no process killed", *argv); } else { long *pl; for(pl = pidList; *pl !=0 ; pl++) { if (*pl==myPid) continue; if (kill(*pl, signo) != 0) { errors++; if (quiet==0) bb_perror_msg( "Could not kill pid '%ld'", *pl); } } } free(pidList); argv++; } } #endif return errors; }
int kill_main(int argc, char **argv) { char *arg; pid_t pid; int signo = SIGTERM, errors = 0, quiet = 0; #if !ENABLE_KILLALL && !ENABLE_KILLALL5 #define killall 0 #define killall5 0 #else /* How to determine who we are? find 3rd char from the end: * kill, killall, killall5 * ^i ^a ^l - it's unique * (checking from the start is complicated by /bin/kill... case) */ const char char3 = argv[0][strlen(argv[0]) - 3]; #define killall (ENABLE_KILLALL && char3 == 'a') #define killall5 (ENABLE_KILLALL5 && char3 == 'l') #endif /* Parse any options */ argc--; arg = *++argv; if (argc < 1 || arg[0] != '-') { goto do_it_now; } /* The -l option, which prints out signal names. * Intended usage in shell: * echo "Died of SIG`kill -l $?`" * We try to mimic what kill from coreutils-6.8 does */ if (arg[1] == 'l' && arg[2] == '\0') { if (argc == 1) { /* Print the whole signal list */ print_signames(); return 0; } /* -l <sig list> */ while ((arg = *++argv)) { if (isdigit(arg[0])) { signo = bb_strtou(arg, NULL, 10); if (errno) { bb_error_msg("unknown signal '%s'", arg); return EXIT_FAILURE; } /* Exitcodes >= 0x80 are to be treated * as "killed by signal (exitcode & 0x7f)" */ puts(get_signame(signo & 0x7f)); /* TODO: 'bad' signal# - coreutils says: * kill: 127: invalid signal * we just print "127" instead */ } else { signo = get_signum(arg); if (signo < 0) { bb_error_msg("unknown signal '%s'", arg); return EXIT_FAILURE; } printf("%d\n", signo); } } /* If they specified -l, we are all done */ return EXIT_SUCCESS; } /* The -q quiet option */ if (killall && arg[1] == 'q' && arg[2] == '\0') { quiet = 1; arg = *++argv; argc--; if (argc < 1) bb_show_usage(); if (arg[0] != '-') goto do_it_now; } arg++; /* skip '-' */ /* -o PID? (if present, it always is at the end of command line) */ if (killall5 && arg[0] == 'o') goto do_it_now; if (argc > 1 && arg[0] == 's' && arg[1] == '\0') { /* -s SIG? */ argc--; arg = *++argv; } /* else it must be -SIG */ signo = get_signum(arg); if (signo < 0) { /* || signo > MAX_SIGNUM ? */ bb_error_msg("bad signal name '%s'", arg); return EXIT_FAILURE; } arg = *++argv; argc--; do_it_now: pid = getpid(); if (killall5) { pid_t sid; procps_status_t* p = NULL; int ret = 0; /* Find out our session id */ sid = getsid(pid); /* Stop all processes */ if (signo != SIGSTOP && signo != SIGCONT) kill(-1, SIGSTOP); /* Signal all processes except those in our session */ while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID)) != NULL) { int i; if (p->sid == (unsigned)sid || p->pid == (unsigned)pid || p->pid == 1 ) { continue; } /* All remaining args must be -o PID options. * Check p->pid against them. */ for (i = 0; i < argc; i++) { pid_t omit; arg = argv[i]; if (arg[0] != '-' || arg[1] != 'o') { bb_error_msg("bad option '%s'", arg); ret = 1; goto resume; } arg += 2; if (!arg[0] && argv[++i]) arg = argv[i]; omit = bb_strtoi(arg, NULL, 10); if (errno) { bb_error_msg("invalid number '%s'", arg); ret = 1; goto resume; } if (p->pid == omit) goto dont_kill; } kill(p->pid, signo); dont_kill: ; } resume: /* And let them continue */ if (signo != SIGSTOP && signo != SIGCONT) kill(-1, SIGCONT); return ret; } /* Pid or name is required for kill/killall */ if (argc < 1) { bb_error_msg("you need to specify whom to kill"); return EXIT_FAILURE; } if (killall) { /* Looks like they want to do a killall. Do that */ while (arg) { pid_t* pidList; pidList = find_pid_by_name(arg); if (*pidList == 0) { errors++; if (!quiet) bb_error_msg("%s: no process killed", arg); } else { pid_t *pl; for (pl = pidList; *pl; pl++) { if (*pl == pid) continue; if (kill(*pl, signo) == 0) continue; errors++; if (!quiet) bb_perror_msg("can't kill pid %d", (int)*pl); } } free(pidList); arg = *++argv; } return errors; } /* Looks like they want to do a kill. Do that */ while (arg) { #if ENABLE_ASH || ENABLE_HUSH /* * We need to support shell's "hack formats" of * " -PRGP_ID" (yes, with a leading space) * and " PID1 PID2 PID3" (with degenerate case "") */ while (*arg != '\0') { char *end; if (*arg == ' ') arg++; pid = bb_strtoi(arg, &end, 10); if (errno && (errno != EINVAL || *end != ' ')) { bb_error_msg("invalid number '%s'", arg); errors++; break; } if (kill(pid, signo) != 0) { bb_perror_msg("can't kill pid %d", (int)pid); errors++; } arg = end; /* can only point to ' ' or '\0' now */ } #else pid = bb_strtoi(arg, NULL, 10); if (errno) { bb_error_msg("invalid number '%s'", arg); errors++; } else if (kill(pid, signo) != 0) { bb_perror_msg("can't kill pid %d", (int)pid); errors++; } #endif arg = *++argv; } return errors; }
extern int reboot_main(int argc, char **argv) { int delay = 0; /* delay in seconds before rebooting */ int rc; while ((rc = getopt(argc, argv, "d:")) > 0) { switch (rc) { case 'd': delay = atoi(optarg); break; default: show_usage(); break; } } if(delay > 0) sleep(delay); #ifdef CONFIG_USER_INIT_INIT /* Don't kill ourself */ signal(SIGTERM,SIG_IGN); signal(SIGHUP,SIG_IGN); setpgrp(); /* Allow Ctrl-Alt-Del to reboot system. */ init_reboot(RB_ENABLE_CAD); message("\n\rThe system is going down NOW !!\n"); sync(); /* Send signals to every process _except_ pid 1 */ message("\rSending SIGTERM to all processes.\n"); kill(-1, SIGTERM); sleep(1); sync(); message("\rSending SIGKILL to all processes.\n"); kill(-1, SIGKILL); sleep(1); sync(); if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,2,11)) { /* bdflush, kupdate not needed for kernels >2.2.11 */ bdflush(1, 0); sync(); } init_reboot(RB_AUTOBOOT); exit(0); /* Shrug */ #else #ifdef BB_FEATURE_LINUXRC { /* don't assume init's pid == 1 */ long *pid = find_pid_by_name("init"); if (!pid || *pid<=0) pid = find_pid_by_name("linuxrc"); if (!pid || *pid<=0) error_msg_and_die("no process killed"); fflush(stdout); return(kill(*pid, SIGTERM)); } #else // yachen *(unsigned int volatile *)(0xfff8101c) = 0x82; return(kill(1, SIGTERM)); #endif #endif }
int kill_main(int argc, char **argv) { char *arg; pid_t pid; int signo = SIGTERM, errors = 0, quiet = 0; #if !ENABLE_KILLALL && !ENABLE_KILLALL5 #define killall 0 #define killall5 0 #else /* How to determine who we are? find 3rd char from the end: * kill, killall, killall5 * ^i ^a ^l - it's unique * (checking from the start is complicated by /bin/kill... case) */ const char char3 = argv[0][strlen(argv[0]) - 3]; #define killall (ENABLE_KILLALL && char3 == 'a') #define killall5 (ENABLE_KILLALL5 && char3 == 'l') #endif /* Parse any options */ argc--; arg = *++argv; if (argc < 1 || arg[0] != '-') { goto do_it_now; } /* The -l option, which prints out signal names. * Intended usage in shell: * echo "Died of SIG`kill -l $?`" * We try to mimic what kill from coreutils-6.8 does */ if (arg[1] == 'l' && arg[2] == '\0') { if (argc == 1) { /* Print the whole signal list */ print_signames(); return 0; } /* -l <sig list> */ while ((arg = *++argv)) { if (isdigit(arg[0])) { signo = bb_strtou(arg, NULL, 10); if (errno) { bb_error_msg("unknown signal '%s'", arg); return EXIT_FAILURE; } /* Exitcodes >= 0x80 are to be treated * as "killed by signal (exitcode & 0x7f)" */ puts(get_signame(signo & 0x7f)); /* TODO: 'bad' signal# - coreutils says: * kill: 127: invalid signal * we just print "127" instead */ } else { signo = get_signum(arg); if (signo < 0) { bb_error_msg("unknown signal '%s'", arg); return EXIT_FAILURE; } printf("%d\n", signo); } } /* If they specified -l, we are all done */ return EXIT_SUCCESS; } /* The -q quiet option */ if (killall && arg[1] == 'q' && arg[2] == '\0') { quiet = 1; arg = *++argv; argc--; if (argc < 1) bb_show_usage(); if (arg[0] != '-') goto do_it_now; } /* -SIG */ signo = get_signum(&arg[1]); if (signo < 0) { /* || signo > MAX_SIGNUM ? */ bb_error_msg("bad signal name '%s'", &arg[1]); return EXIT_FAILURE; } arg = *++argv; argc--; do_it_now: pid = getpid(); if (killall5) { pid_t sid; procps_status_t* p = NULL; /* Find out our own session id */ sid = getsid(pid); /* Now stop all processes */ kill(-1, SIGSTOP); /* Now kill all processes except our session */ while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID))) { if (p->sid != (unsigned)sid && p->pid != (unsigned)pid && p->pid != 1) kill(p->pid, signo); } /* And let them continue */ kill(-1, SIGCONT); return 0; } /* Pid or name is required for kill/killall */ if (argc < 1) { bb_error_msg("you need to specify whom to kill"); return EXIT_FAILURE; } if (killall) { /* Looks like they want to do a killall. Do that */ while (arg) { pid_t* pidList; pidList = find_pid_by_name(arg); if (*pidList == 0) { errors++; if (!quiet) bb_error_msg("%s: no process killed", arg); } else { pid_t *pl; for (pl = pidList; *pl; pl++) { if (*pl == pid) continue; if (kill(*pl, signo) == 0) continue; errors++; if (!quiet) bb_perror_msg("cannot kill pid %u", (unsigned)*pl); } } free(pidList); arg = *++argv; } return errors; } /* Looks like they want to do a kill. Do that */ while (arg) { /* Support shell 'space' trick */ if (arg[0] == ' ') arg++; pid = bb_strtoi(arg, NULL, 10); if (errno) { bb_error_msg("bad pid '%s'", arg); errors++; } else if (kill(pid, signo) != 0) { bb_perror_msg("cannot kill pid %d", (int)pid); errors++; } arg = *++argv; } return errors; }