[tyndur-devel] [PATCH] neuer shell-Befehl: killall
Kevin Wolf ·
On Wed, Mar 27, 2013 at 10:46:57PM +0100, Stefan Linke wrote: > + Neuen Shell-Befehl killall implementiert, der alle Prozesse mit gegebenem Namen killt. > > Signed-off-by: Stefan Linke <particleflux at gmail.com> Ich habe das jetzt mal so gepusht, aber es gibt noch an ein paar Stellen Möglichkeiten, nachzuarbeiten. ;-) > +#ifdef CONFIG_SHELL_BUILTIN_ONLY > + int shell_command_killall(int argc, char* argv[], const char* args) > +#else > + int main(int argc, char* argv[]) > +#endif > +{ > + int ret=0,i=0,fail=0,matches=0; > + task_info_t *task_info = NULL; > + char *process; > + char copied_cmdline[255]; > + > + if (argc < 2) { > + killall_display_usage(); > + return -1; > + } > + > + task_info = enumerate_tasks(); > + > + if (task_info == NULL) { > + return EXIT_FAILURE; > + } > + > + > + for (i = 0; i < task_info->task_count; ++i) { > + strncpy(copied_cmdline, task_info->tasks[i].cmdline, 255); copied_cmdline ist nicht nullterminiert, wenn der String zu lang war. > + > + process = strtok((char *) task_info->tasks[i].cmdline, " "); > + process = process == NULL > + ? strrchr(task_info->tasks[i].cmdline, '/') > + : strrchr(process, '/'); > + ++process; Wenn kein / in der Kommandozeile vorkommt, hast du ein Problem. > + > + if (strcmp(argv[1], process) == 0) { > + ++matches; > + ret = kill(task_info->tasks[i].pid, SIGTERM); > + > + if (ret < 0) { > + printf(TMS(killall_cant_kill, > + "Konnte pid %d - %s nicht beenden: %s (%s)\n"), > + task_info->tasks[i].pid, errno, strerror(errno)); > + > + if (!fail) { > + fail = 1; > + } Das if ist eigentlich ziemlich überflüssig. Wenn du eine 1 mit noch einer 1 überschreibst, passiert auch nichts schlimmes. Kevin