summaryrefslogtreecommitdiffstats
path: root/libmisc/user_busy.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-26 16:18:36 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-26 16:18:36 +0000
commit6c3ea4f47ea280811a7fe53a22f7832e4533c9ec (patch)
tree3d7ed5da23b5dbf6f9e450dfb61642832249c31e /libmisc/user_busy.c
parentAdding upstream version 1:4.13+dfsg1. (diff)
downloadshadow-6c3ea4f47ea280811a7fe53a22f7832e4533c9ec.tar.xz
shadow-6c3ea4f47ea280811a7fe53a22f7832e4533c9ec.zip
Adding upstream version 1:4.15.2.upstream/1%4.15.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--lib/user_busy.c (renamed from libmisc/user_busy.c)68
1 files changed, 32 insertions, 36 deletions
diff --git a/libmisc/user_busy.c b/lib/user_busy.c
index c03feb2..a622376 100644
--- a/libmisc/user_busy.c
+++ b/lib/user_busy.c
@@ -23,6 +23,8 @@
#include "subordinateio.h"
#endif /* ENABLE_SUBIDS */
#include "shadowlog.h"
+#include "string/sprintf.h"
+
#ifdef __linux__
static int check_status (const char *name, const char *sname, uid_t uid);
@@ -32,7 +34,7 @@ static int user_busy_utmp (const char *name);
#endif /* !__linux__ */
/*
- * user_busy - check if an user if currently running processes
+ * user_busy - check if a user is currently running processes
*/
int user_busy (const char *name, uid_t uid)
{
@@ -43,26 +45,21 @@ int user_busy (const char *name, uid_t uid)
/* On Linux, directly parse /proc */
return user_busy_processes (name, uid);
#else /* !__linux__ */
- /* If we cannot rely on /proc, check is there is a record in utmp
+ /* If we cannot rely on /proc, check if there is a record in utmp
* indicating that the user is still logged in */
return user_busy_utmp (name);
#endif /* !__linux__ */
}
+
#ifndef __linux__
-static int user_busy_utmp (const char *name)
+static int
+user_busy_utmp(const char *name)
{
-#ifdef USE_UTMPX
- struct utmpx *utent;
+ struct utmpx *utent;
- setutxent ();
- while ((utent = getutxent ()) != NULL)
-#else /* !USE_UTMPX */
- struct utmp *utent;
-
- setutent ();
- while ((utent = getutent ()) != NULL)
-#endif /* !USE_UTMPX */
+ setutxent();
+ while ((utent = getutxent()) != NULL)
{
if (utent->ut_type != USER_PROCESS) {
continue;
@@ -84,17 +81,18 @@ static int user_busy_utmp (const char *name)
}
#endif /* !__linux__ */
+
#ifdef __linux__
#ifdef ENABLE_SUBIDS
#define in_parentuid_range(uid) ((uid) >= parentuid && (uid) < parentuid + range)
static int different_namespace (const char *sname)
{
/* 41: /proc/xxxxxxxxxx/task/xxxxxxxxxx/ns/user + \0 */
- char path[41];
- char buf[512], buf2[512];
- ssize_t llen1, llen2;
+ char path[41];
+ char buf[512], buf2[512];
+ ssize_t llen1, llen2;
- snprintf (path, 41, "/proc/%s/ns/user", sname);
+ SNPRINTF(path, "/proc/%s/ns/user", sname);
if ((llen1 = readlink (path, buf, sizeof(buf))) == -1)
return 0;
@@ -113,11 +111,11 @@ static int different_namespace (const char *sname)
static int check_status (const char *name, const char *sname, uid_t uid)
{
/* 40: /proc/xxxxxxxxxx/task/xxxxxxxxxx/status + \0 */
- char status[40];
- char line[1024];
- FILE *sfile;
+ char status[40];
+ char line[1024];
+ FILE *sfile;
- snprintf (status, 40, "/proc/%s/status", sname);
+ SNPRINTF(status, "/proc/%s/status", sname);
sfile = fopen (status, "r");
if (NULL == sfile) {
@@ -158,16 +156,16 @@ static int check_status (const char *name, const char *sname, uid_t uid)
static int user_busy_processes (const char *name, uid_t uid)
{
- DIR *proc;
- struct dirent *ent;
- char *tmp_d_name;
- pid_t pid;
- DIR *task_dir;
+ DIR *proc;
+ DIR *task_dir;
+ char *tmp_d_name;
/* 22: /proc/xxxxxxxxxx/task + \0 */
- char task_path[22];
- char root_path[22];
- struct stat sbroot;
- struct stat sbroot_process;
+ char task_path[22];
+ char root_path[22];
+ pid_t pid;
+ struct stat sbroot;
+ struct stat sbroot_process;
+ struct dirent *ent;
#ifdef ENABLE_SUBIDS
sub_uid_open (O_RDONLY);
@@ -207,13 +205,12 @@ static int user_busy_processes (const char *name, uid_t uid)
}
/* Check if this is a valid PID */
- if (get_pid (tmp_d_name, &pid) == 0) {
+ if (get_pid(tmp_d_name, &pid) == -1) {
continue;
}
/* Check if the process is in our chroot */
- snprintf (root_path, 22, "/proc/%lu/root", (unsigned long) pid);
- root_path[21] = '\0';
+ SNPRINTF(root_path, "/proc/%lu/root", (unsigned long) pid);
if (stat (root_path, &sbroot_process) != 0) {
continue;
}
@@ -233,13 +230,12 @@ static int user_busy_processes (const char *name, uid_t uid)
return 1;
}
- snprintf (task_path, 22, "/proc/%lu/task", (unsigned long) pid);
- task_path[21] = '\0';
+ SNPRINTF(task_path, "/proc/%lu/task", (unsigned long) pid);
task_dir = opendir (task_path);
if (task_dir != NULL) {
while ((ent = readdir (task_dir)) != NULL) {
pid_t tid;
- if (get_pid (ent->d_name, &tid) == 0) {
+ if (get_pid(ent->d_name, &tid) == -1) {
continue;
}
if (tid == pid) {