library: Promote procps_users to external function

The user count is useful for not expanding the uptime_sprintf
functions. This way there is a standard method of getting the
uptime, user count and load average.

This will be used in the next commit for the uptime program
as a start.

Signed-off-by: Craig Small <csmall@dropbear.xyz>
This commit is contained in:
Craig Small 2024-01-31 17:17:09 +11:00
parent bf013807d6
commit 5773a51331
5 changed files with 43 additions and 8 deletions

1
NEWS
View File

@ -7,6 +7,7 @@ procps-ng-NEXT
external: schedule class added to pids api
external: disk sleep added to pids api, sleep revised issue #265
external: docker containers added to pids api
external: procps_users new exported function
* pgrep: select process by environment variable issue #167
* ps: Add environ field
* ps: Add htprv and htshr fields for HugeTables

View File

@ -58,6 +58,7 @@ int procps_loadavg (double *av1, double *av5, double *av15);
int procps_uptime (double *uptime_secs, double *idle_secs);
char *procps_uptime_sprint (void);
char *procps_uptime_sprint_short (void);
int procps_users (void);
// //////////////////////////////////////////////////////////////////

View File

@ -65,3 +65,7 @@ global:
local:
*;
};
LIBPROC_2.1 {
procps_users;
} LIBPROC_2;

View File

@ -48,20 +48,38 @@
static __thread char upbuf[256];
static __thread char shortbuf[256];
static int count_users(void)
/*
* users:
*
* Count the number of users on the system
* Strictly speaking not a proc FS function but used in many
* places.
*
* Returns: user count on success and <0 on failure
*/
PROCPS_EXPORT int procps_users(void)
{
int numuser = 0;
int numuser = -1;
#ifdef HAVE_UTMP_X
struct utmpx *ut;
#else
struct utmp *ut;
#endif
#if defined(WITH_SYSTEMD) || defined(WITH_ELOGIND)
if (sd_booted() > 0)
return sd_get_sessions(NULL);
#endif
#ifdef HAVE_UTMP_X
setutxent();
while ((ut = getutxent())) {
#else
setutent();
while ((ut = getutent())) {
if ((ut->ut_type == USER_PROCESS) && (ut->ut_name[0] != '\0'))
numuser++;
#endif
if ((ut->ut_type == USER_PROCESS) && (ut->ut_name[0] != '\0'))
numuser++;
}
endutent();
@ -131,6 +149,9 @@ PROCPS_EXPORT char *procps_uptime_sprint(void)
if (procps_uptime(&uptime_secs, &idle_secs) < 0)
return upbuf;
if ((users = procps_users()) < 0)
return upbuf;
updays = ((int) uptime_secs / (60*60*24));
uphours = ((int) uptime_secs / (60*60)) % 24;
upminutes = ((int) uptime_secs / (60)) % 60;
@ -146,7 +167,6 @@ PROCPS_EXPORT char *procps_uptime_sprint(void)
else
pos += sprintf(upbuf + pos, "%d min, ", upminutes);
users = count_users();
procps_loadavg(&av1, &av5, &av15);
if (users < 0)

View File

@ -1,6 +1,6 @@
.\"
.\" Copyright (c) 2020-2023 Jim Warner <james.warner@comcast.net>
.\" Copyright (c) 2020-2023 Craig Small <csmall@dropbear.xyz>
.\" Copyright (c) 2020-2024 Jim Warner <james.warner@comcast.net>
.\" Copyright (c) 2020-2024 Craig Small <csmall@dropbear.xyz>
.\"
.\" This manual is free software; you can redistribute it and/or
.\" modify it under the terms of the GNU Lesser General Public
@ -8,7 +8,7 @@
.\" version 2.1 of the License, or (at your option) any later version.
.\"
.\"
.TH PROCPS_MISC 3 "August 2022" "libproc2"
.TH PROCPS_MISC 3 "2024-01-31" "libproc2"
.\" Please adjust this date whenever revising the manpage.
.\"
.nh
@ -34,6 +34,7 @@ Runtime Particulars
.RI "int \fB procps_uptime\fR (double *" uptime_secs ", double *" idle_secs ");"
.RB "char *" procps_uptime_sprint " (void);"
.RB "char *" procps_uptime_sprint_short " (void);"
.RB "int " procps_users " (void);"
.RE
.PP
Namespace Particulars
@ -96,6 +97,12 @@ HH:MM:SS up HH:MM, # users, load average: 1, 5, 15 MM averages
up HH, MM
.RE
.P
.BR procps_users ()
returns the number of users on the system. This value comes from
.BR sd_get_sessions (3)
or enumerating through
.BR getutent (3).
.P
.BR procps_ns_get_id ()
returns the integer id (enum namespace_type) of the namespace for the given namespace \fIname\fR.
.P
@ -148,4 +155,6 @@ contains the set of namespaces for a particular
.SH SEE ALSO
.BR procps (3),
.BR procps_pids (3),
.BR getutent (3),
.BR sd_get_sessions (3),
.BR proc (5).