autofs-5.1.5 - optionally log mount requestor process info From: Lars R. Damerow This information can be helpful to determine who or what is making particular mount requests, especially when used in conjunction with the use_mount_request_log_id option. Signed-off-by: Lars R. Damerow Signed-off-by: Ian Kent --- CHANGELOG | 1 + daemon/direct.c | 6 ++++++ daemon/indirect.c | 6 ++++++ include/log.h | 2 ++ lib/log.c | 39 +++++++++++++++++++++++++++++++++++++++ man/autofs.conf.5.in | 3 ++- redhat/autofs.conf.default.in | 4 +++- samples/autofs.conf.default.in | 4 +++- 8 files changed, 62 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index c2a0c7f1..ee7deda4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,6 +4,7 @@ xx/xx/2019 autofs-5.1.6 - add NULL check for get_addr_string() return. - use malloc(3) in spawn.c. - add mount_verbose configuration option. +- optionally log mount requestor process info. 30/10/2018 autofs-5.1.5 - fix flag file permission. diff --git a/daemon/direct.c b/daemon/direct.c index 9c61c4b4..4f468563 100644 --- a/daemon/direct.c +++ b/daemon/direct.c @@ -1242,6 +1242,12 @@ static void *do_mount_direct(void *arg) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &state); + if (defaults_get_mount_verbose()) { + pid_t ppid = log_pidinfo(ap, mt.pid, "requestor"); + if (ppid > 0) + log_pidinfo(ap, ppid, "parent"); + } + status = fstat(mt.ioctlfd, &st); if (status == -1) { error(ap->logopt, diff --git a/daemon/indirect.c b/daemon/indirect.c index d0724293..9ccbc038 100644 --- a/daemon/indirect.c +++ b/daemon/indirect.c @@ -758,6 +758,12 @@ static void *do_mount_indirect(void *arg) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &state); + if (defaults_get_mount_verbose()) { + pid_t ppid = log_pidinfo(ap, mt.pid, "requestor"); + if (ppid > 0) + log_pidinfo(ap, ppid, "parent"); + } + len = ncat_path(buf, sizeof(buf), ap->path, mt.name, mt.len); if (!len) { crit(ap->logopt, "path to be mounted is to long"); diff --git a/include/log.h b/include/log.h index c9b17b3c..69eed96b 100644 --- a/include/log.h +++ b/include/log.h @@ -46,6 +46,8 @@ extern void log_crit(unsigned, const char* msg, ...); extern void log_debug(unsigned int, const char* msg, ...); extern void logmsg(const char* msg, ...); +extern pid_t log_pidinfo(struct autofs_point *ap, pid_t pid, char *label); + #define debug(opt, msg, args...) \ do { log_debug(opt, "%s: " msg, __FUNCTION__, ##args); } while (0) diff --git a/lib/log.c b/lib/log.c index ca771d72..0cb47d7e 100644 --- a/lib/log.c +++ b/lib/log.c @@ -325,3 +325,42 @@ void log_to_stderr(void) return; } + +pid_t log_pidinfo(struct autofs_point *ap, pid_t pid, char *label) { + char buf[PATH_MAX + 1] = ""; + FILE *statfile; + + pid_t tgid, ppid; + int uid, euid, gid, egid; + char comm[64] = ""; + + sprintf(buf, "/proc/%d/status", pid); + statfile = fopen(buf, "r"); + if (statfile == NULL) { + info(ap->logopt, "pidinfo %s: failed to open %s", label, buf); + return -1; + } + + while (fgets(buf, sizeof(buf), statfile) != NULL) { + if (strncmp(buf, "Name:", 5) == 0) { + sscanf(buf, "Name:\t%s", (char *) &comm); + } else if (strncmp(buf, "Tgid:", 5) == 0) { + sscanf(buf, "Tgid:\t%d", (int *) &tgid); + } else if (strncmp(buf, "PPid:", 5) == 0) { + sscanf(buf, "PPid:\t%d", (int *) &ppid); + } else if (strncmp(buf, "Uid:", 4) == 0) { + sscanf(buf, + "Uid:\t%d\t%d", (int *) &uid, (int *) &euid); + } else if (strncmp(buf, "Gid:", 4) == 0) { + sscanf(buf, + "Gid:\t%d\t%d", (int *) &gid, (int *) &egid); + } + } + fclose(statfile); + + info(ap->logopt, + "pidinfo %s: pid:%d comm:%s tgid:%d uid:%d euid:%d gid:%d egid:%d", + label, pid, comm, tgid, uid, euid, gid, egid); + + return ppid; +} diff --git a/man/autofs.conf.5.in b/man/autofs.conf.5.in index 31136e2e..95ff7dd0 100644 --- a/man/autofs.conf.5.in +++ b/man/autofs.conf.5.in @@ -43,7 +43,8 @@ setting. .TP .B mount_verbose .br -Use the verbose flag when spawning mount(8) (program default "no"). +Use the verbose flag when spawning mount(8), and log some process info +about the requestor and its parent (program default "no"). .TP .B mount_wait .br diff --git a/redhat/autofs.conf.default.in b/redhat/autofs.conf.default.in index 7949f51a..4b89a5f7 100644 --- a/redhat/autofs.conf.default.in +++ b/redhat/autofs.conf.default.in @@ -26,7 +26,9 @@ timeout = 300 # #negative_timeout = 60 # -# mount_verbose - use the -v flag when calling mount(8). +# mount_verbose - use the -v flag when calling mount(8) and log some +# process information about the requestor and its +# parent. # #mount_verbose = no # diff --git a/samples/autofs.conf.default.in b/samples/autofs.conf.default.in index d33625f1..2f155111 100644 --- a/samples/autofs.conf.default.in +++ b/samples/autofs.conf.default.in @@ -26,7 +26,9 @@ timeout = 300 # #negative_timeout = 60 # -# mount_verbose - use the -v flag when calling mount(8). +# mount_verbose - use the -v flag when calling mount(8) and log some +# process information about the requestor and its +# parent. # #mount_verbose = no #