This repository has been archived by the owner on Sep 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
cmd_pidof.c
68 lines (60 loc) · 1.66 KB
/
cmd_pidof.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in
* the LICENSE file in the root directory of this source tree. An
* additional grant of patent rights can be found in the PATENTS file
* in the same directory.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include "util.h"
#include "autocmd.h"
#include "fs.h"
#include "json.h"
#if FBADB_MAIN
FORWARD(pidof);
#elif !defined(__linux__)
int
pidof_main(const struct cmd_pidof_info* info)
{
die(EINVAL, "this command is supported only on Linux");
}
#else
int
pidof_main(const struct cmd_pidof_info* info)
{
const char* name = info->name;
DIR* procdir = xopendir("/proc");
struct dirent* ent;
unsigned long found_pid = 0;
bool found = false;
while ((ent = readdir(procdir)) != NULL) {
SCOPED_RESLIST(rl);
char* endptr = NULL;
errno = 0;
unsigned long pid = strtoul(ent->d_name, &endptr, 10);
if (pid == 0 || errno != 0 || *endptr != '\0')
continue;
int cmdline_fd = try_xopen(xaprintf("/proc/%lu/cmdline", pid), O_RDONLY, 0);
if (cmdline_fd == -1)
continue;
char* name = slurp_fd(cmdline_fd, NULL);
if (strcmp(name, info->name) == 0) {
if (found)
die(EINVAL, "more than one process has name `%s'", name);
found = true;
found_pid = pid;
}
}
if (!found)
die(ENOENT, "no process has name `%s'", name);
xprintf(xstdout, "%lu%s", found_pid, info->pidof.zero ? "" : "\n");
xflush(xstdout);
return 0;
}
#endif