Skip to content

Commit

Permalink
procs-need-restart: Ignore EACCESS if not running as root
Browse files Browse the repository at this point in the history
  • Loading branch information
jirutka committed May 7, 2018
1 parent b368e06 commit 7ec9519
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions man/procs-need-restart.1.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ It reports processes that use (maps into memory) files that have been deleted or

The command accepts one or more PID of processes to scan.
If no positional argument is given, all processes running on the system (except kernel processes) are scanned.
But if user`'s effective UID is not 0 (i.e. not running as root), processes the user has no permissions to examine are ignored.

This program is part of *apk-autoupdate* package.

Expand Down
13 changes: 12 additions & 1 deletion src/procs-need-restart.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#define RET_ERROR -1

#define FLAG_VERBOSE 0x0001
#define FLAG_IGNORE_EACCES 0x0002

// Length of highest pid_t (int) value encoded as a decimal number.
#define PID_STR_MAX 10
Expand All @@ -83,7 +84,8 @@ static const char *HELP_MSG =
"\n"
"Find processes that use (maps into memory) files which have been deleted\n"
"or replaced on disk (and the new files are not identical to the mapped ones).\n"
"If no PID is specified, scan all processes.\n"
"If no PID is specified, scan all processes. But if user's effective UID is\n"
"not 0 (i.e. not root), ignore processes we don't have permissions to examine.\n"
"\n"
"This program is part of apk-autoupdate.\n"
"\n"
Expand Down Expand Up @@ -274,6 +276,9 @@ static int proc_maps_replaced_files (pid_t pid, const char **file_patterns) {
if ((maps_fp = fopen(maps_path, "r")) == NULL) {
int fopen_err = errno;

if (fopen_err == EACCES && flags & FLAG_IGNORE_EACCES) {
return 1; // no
}
// If process does not exist anymore, then it's not an error.
if (proc_exists(pid) == 1) {
return 1; // no
Expand Down Expand Up @@ -348,6 +353,9 @@ static int proc_has_replaced_exe (pid_t pid, const char **file_patterns) {
if (resolve_link(exe_path, link_path, sizeof(link_path)) < 0) {
int link_err = errno;

if (link_err == EACCES && flags & FLAG_IGNORE_EACCES) {
return 1; // no
}
// If process does not exist anymore, then it's not an error.
if (proc_exists(pid) == 1) {
return 1; // no
Expand Down Expand Up @@ -489,6 +497,9 @@ int main (int argc, char **argv) {
return scan_procs(pids, file_patterns);

} else {
if (geteuid() != 0) {
flags |= FLAG_IGNORE_EACCES;
}
return scan_all_procs(file_patterns);
}
}

0 comments on commit 7ec9519

Please sign in to comment.