bool process_iterator_t::next_process(wcstring *out_str, pid_t *out_pid) { wcstring cmd; pid_t pid = 0; while (cmd.empty()) { wcstring name; if (! dir || ! wreaddir(dir, name)) break; if (!iswnumeric(name.c_str())) continue; wcstring path = wcstring(L"/proc/") + name; struct stat buf; if (wstat(path, &buf)) continue; if (buf.st_uid != getuid()) continue; /* remember the pid */ pid = fish_wcstoi(name.c_str(), NULL, 10); /* the 'cmdline' file exists, it should contain the commandline */ FILE *cmdfile; if ((cmdfile=wfopen(path + L"/cmdline", "r"))) { wcstring full_command_line; signal_block(); fgetws2(&full_command_line, cmdfile); signal_unblock(); /* The command line needs to be escaped */ cmd = tok_first(full_command_line.c_str()); } #ifdef SunOS else if ((cmdfile=wfopen(path + L"/psinfo", "r"))) { psinfo_t info; if (fread(&info, sizeof(info), 1, cmdfile)) { /* The filename is unescaped */ cmd = str2wcstring(info.pr_fname); } } #endif if (cmdfile) fclose(cmdfile); } bool result = ! cmd.empty(); if (result) { *out_str = cmd; *out_pid = pid; } return result; }
static int find_process(const wchar_t *proc, expand_flags_t flags, std::vector<completion_t> &out) { int found = 0; if (!(flags & EXPAND_SKIP_JOBS)) { ASSERT_IS_MAIN_THREAD(); const job_t *j; if (iswnumeric(proc) || (wcslen(proc)==0)) { /* This is a numeric job string, like '%2' */ if (flags & ACCEPT_INCOMPLETE) { job_iterator_t jobs; while ((j = jobs.next())) { wchar_t jid[16]; if (j->command_is_empty()) continue; swprintf(jid, 16, L"%d", j->job_id); if (wcsncmp(proc, jid, wcslen(proc))==0) { wcstring desc_buff = format_string(COMPLETE_JOB_DESC_VAL, j->command_wcstr()); append_completion(out, jid+wcslen(proc), desc_buff, 0); } } } else { int jid; wchar_t *end; errno = 0; jid = fish_wcstoi(proc, &end, 10); if (jid > 0 && !errno && !*end) { j = job_get(jid); if ((j != 0) && (j->command_wcstr() != 0)) { { append_completion(out, to_string<long>(j->pgid)); found = 1; } } } } } if (found) return 1; job_iterator_t jobs; while ((j = jobs.next())) { if (j->command_is_empty()) continue; size_t offset; if (match_pid(j->command(), proc, flags, &offset)) { if (flags & ACCEPT_INCOMPLETE) { append_completion(out, j->command_wcstr() + offset + wcslen(proc), COMPLETE_JOB_DESC, 0); } else { append_completion(out, to_string<long>(j->pgid)); found = 1; } } } if (found) { return 1; } jobs.reset(); while ((j = jobs.next())) { process_t *p; if (j->command_is_empty()) continue; for (p=j->first_process; p; p=p->next) { if (p->actual_cmd.empty()) continue; size_t offset; if (match_pid(p->actual_cmd, proc, flags, &offset)) { if (flags & ACCEPT_INCOMPLETE) { append_completion(out, wcstring(p->actual_cmd, offset + wcslen(proc)), COMPLETE_CHILD_PROCESS_DESC, 0); } else { append_completion(out, to_string<long>(p->pid), L"", 0); found = 1; } } } } if (found) { return 1; } } /* Iterate over all processes */ wcstring process_name; pid_t process_pid; process_iterator_t iterator; while (iterator.next_process(&process_name, &process_pid)) { size_t offset; if (match_pid(process_name, proc, flags, &offset)) { if (flags & ACCEPT_INCOMPLETE) { append_completion(out, process_name.c_str() + offset + wcslen(proc), COMPLETE_PROCESS_DESC, 0); } else { append_completion(out, to_string<long>(process_pid)); } } } return 1; }
/// The following function is invoked on the main thread, because the job operation is not thread /// safe. It waits for child jobs, not for child processes individually. int builtin_wait(parser_t &parser, io_streams_t &streams, wchar_t **argv) { ASSERT_IS_MAIN_THREAD(); int retval = STATUS_CMD_OK; const wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); bool any_flag = false; // flag for -n option static const wchar_t *const short_options = L":n"; static const struct woption long_options[] = {{L"any", no_argument, NULL, 'n'}, {NULL, 0, NULL, 0}}; int opt; wgetopter_t w; while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) { switch (opt) { case 'n': any_flag = true; break; case ':': { builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]); return STATUS_INVALID_ARGS; } case '?': { builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]); return STATUS_INVALID_ARGS; } default: { DIE("unexpected retval from wgetopt_long"); break; } } } if (w.woptind == argc) { // no jobs specified retval = wait_for_backgrounds(parser, any_flag); } else { // jobs specified std::vector<job_id_t> waited_job_ids; for (int i = w.woptind; i < argc; i++) { if (iswnumeric(argv[i])) { // argument is pid pid_t pid = fish_wcstoi(argv[i]); if (errno || pid <= 0) { streams.err.append_format(_(L"%ls: '%ls' is not a valid process id\n"), cmd, argv[i]); continue; } if (job_id_t id = get_job_id_from_pid(pid, parser)) { waited_job_ids.push_back(id); } else { streams.err.append_format( _(L"%ls: Could not find a job with process id '%d'\n"), cmd, pid); } } else { // argument is process name if (!find_job_by_name(argv[i], waited_job_ids, parser)) { streams.err.append_format( _(L"%ls: Could not find child processes with the name '%ls'\n"), cmd, argv[i]); } } } if (waited_job_ids.empty()) return STATUS_INVALID_ARGS; retval = wait_for_backgrounds_specified(parser, waited_job_ids, any_flag); } return retval; }