From 7ab5d33c5c764d0cdc443298aee5f88d450c6e47 Mon Sep 17 00:00:00 2001 From: Lukas Nykryn Date: Tue, 16 Apr 2013 14:42:30 +0200 Subject: [PATCH 1/6] ps: possibility to display systemd unit for a process Library systemd-login offers possibility to display name of a systemd unit file for specific pid. Note that not all processes are part of a system unit/service (e.g. user processes, or kernel threads). This patch adds output option "sd_unit" which will show name of systemd unit or "-", when process does not belong to any unit. To maintain compatibility with non-systemd systems, procps must be configured with --with-systemd option to enable this option. --- configure.ac | 18 ++++++++++++++++++ ps/Makefile.am | 4 ++++ ps/output.c | 25 +++++++++++++++++++++++++ ps/ps.1 | 4 ++++ 4 files changed, 51 insertions(+) diff --git a/configure.ac b/configure.ac index 0a74197f..ea75f01d 100644 --- a/configure.ac +++ b/configure.ac @@ -264,6 +264,24 @@ then fi AC_SUBST(DEJAGNU) +AC_ARG_WITH([systemd], + [AS_HELP_STRING([--with-systemd], [enable systemd support])], + [], [with_systemd=no]) + +if test "x$with_systemd" != xno; then + PKG_CHECK_MODULES([SYSTEMD], [libsystemd-login], [], [ + AC_CHECK_LIB(systemd-login, sd_pid_get_unit, [have_systemd=yes], [have_systemd=no]) + if test "x$have_systemd" = xno; then + AC_MSG_ERROR([systemd support missing/incomplete]) + fi + SYSTEMD_LIBS="-lsystemd-login" + ]) + AM_CONDITIONAL(WITH_SYSTEMD, true) + AC_DEFINE(WITH_SYSTEMD, 1, [enable systemd support]) +else + AM_CONDITIONAL(WITH_SYSTEMD, false) +fi + AC_CONFIG_FILES([ Makefile include/Makefile diff --git a/ps/Makefile.am b/ps/Makefile.am index e46f496f..7f381144 100644 --- a/ps/Makefile.am +++ b/ps/Makefile.am @@ -5,6 +5,10 @@ AM_CPPFLAGS = \ AM_LDFLAGS = ../proc/libprocps.la +if WITH_SYSTEMD +AM_LDFLAGS += @SYSTEMD_LIBS@ +endif + dist_man_MANS = ps.1 # Use `ginstall' in the definition of PROGRAMS and in dependencies to avoid diff --git a/ps/output.c b/ps/output.c index 3bc17ba9..39b77637 100644 --- a/ps/output.c +++ b/ps/output.c @@ -71,6 +71,10 @@ #include "common.h" +#ifdef WITH_SYSTEMD +#include +#endif + /* TODO: * Stop assuming system time is local time. */ @@ -1169,7 +1173,25 @@ static int pr_sgi_p(char *restrict const outbuf, const proc_t *restrict const pp return snprintf(outbuf, COLWID, "*"); } +#ifdef WITH_SYSTEMD +/************************* Systemd stuff ********************************/ +static int pr_sd_unit(char *restrict const outbuf, const proc_t *restrict const pp){ + int r; + size_t len; + char *unit; + r = sd_pid_get_unit(pp->tgid, &unit); + if(r<0) goto fail; + len = snprintf(outbuf, COLWID, "%s", unit); + free(unit); + return len; + +fail: + outbuf[0] = '-'; + outbuf[1] = '\0'; + return 1; +} +#endif /****************** FLASK & seLinux security stuff **********************/ // move the bulk of this to libproc sometime @@ -1496,6 +1518,9 @@ static const format_struct format_array[] = { {"sched", "SCH", pr_sched, sr_sched, 3, 0, AIX, TO|RIGHT}, {"scnt", "SCNT", pr_nop, sr_nop, 4, 0, DEC, AN|RIGHT}, /* man page misspelling of scount? */ {"scount", "SC", pr_nop, sr_nop, 4, 0, AIX, AN|RIGHT}, /* scnt==scount, DEC claims both */ +#ifdef WITH_SYSTEMD +{"sd_unit", "UNIT", pr_sd_unit, sr_nop, 31, 0, LNX, ET|LEFT}, +#endif {"sess", "SESS", pr_sess, sr_session, 5, 0, XXX, PO|PIDMAX|RIGHT}, {"session", "SESS", pr_sess, sr_session, 5, 0, LNX, PO|PIDMAX|RIGHT}, {"sgi_p", "P", pr_sgi_p, sr_nop, 1, 0, LNX, TO|RIGHT}, /* "cpu" number */ diff --git a/ps/ps.1 b/ps/ps.1 index 9d3d1ce8..0d66cf95 100644 --- a/ps/ps.1 +++ b/ps/ps.1 @@ -1489,6 +1489,10 @@ SCHED_FIFO, SCHED_RR, SCHED_BATCH, SCHED_ISO, and SCHED_IDLE are respectively displayed as 0, 1, 2, 3, 4, and 5. T} +sd_unit UNIT T{ +displays systemd unit which a process belongs to. +T} + sess SESS T{ session ID or, equivalently, the process ID of the session leader. (alias .BR session , \ sid ). From 716d96b8eb00f0f43a01d1707cbc1d611c10454b Mon Sep 17 00:00:00 2001 From: Lukas Nykryn Date: Tue, 16 Apr 2013 15:58:32 +0200 Subject: [PATCH 2/6] ps: possibility to display login session for a process Library systemd-login offers possibility to display name of login session for specific pid. Note that not all processes are part of a login session (e.g. system service processes, user processes that are shared between multiple sessions of the same user, or kernel threads). This patch adds output option "sd_session" which will show name of session or "-", when process does not belong to any session. To maintain compatibility with non-systemd systems, procps must be configured with --with-systemd option to enable this option. --- ps/output.c | 20 ++++++++++++++++++++ ps/ps.1 | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/ps/output.c b/ps/output.c index 39b77637..f1cf6bc0 100644 --- a/ps/output.c +++ b/ps/output.c @@ -1191,6 +1191,25 @@ fail: outbuf[1] = '\0'; return 1; } + +static int pr_sd_session(char *restrict const outbuf, const proc_t *restrict const pp){ + int r; + size_t len; + char *session; + + r = sd_pid_get_session(pp->tgid, &session); + if(r<0) goto fail; + len = snprintf(outbuf, COLWID, "%s", session); + free(session); + return len; + +fail: + outbuf[0] = '-'; + outbuf[1] = '\0'; + return 1; +} + + #endif /****************** FLASK & seLinux security stuff **********************/ // move the bulk of this to libproc sometime @@ -1519,6 +1538,7 @@ static const format_struct format_array[] = { {"scnt", "SCNT", pr_nop, sr_nop, 4, 0, DEC, AN|RIGHT}, /* man page misspelling of scount? */ {"scount", "SC", pr_nop, sr_nop, 4, 0, AIX, AN|RIGHT}, /* scnt==scount, DEC claims both */ #ifdef WITH_SYSTEMD +{"sd_session","SESSION", pr_sd_session, sr_nop, 11, 0, LNX, ET|LEFT}, {"sd_unit", "UNIT", pr_sd_unit, sr_nop, 31, 0, LNX, ET|LEFT}, #endif {"sess", "SESS", pr_sess, sr_session, 5, 0, XXX, PO|PIDMAX|RIGHT}, diff --git a/ps/ps.1 b/ps/ps.1 index 0d66cf95..f953e23a 100644 --- a/ps/ps.1 +++ b/ps/ps.1 @@ -1489,6 +1489,10 @@ SCHED_FIFO, SCHED_RR, SCHED_BATCH, SCHED_ISO, and SCHED_IDLE are respectively displayed as 0, 1, 2, 3, 4, and 5. T} +sd_session SESSION T{ +displays login session identifier of a process. +T} + sd_unit UNIT T{ displays systemd unit which a process belongs to. T} From 785776c10d38b3c6773b15ac7017327c4fa3a01b Mon Sep 17 00:00:00 2001 From: Lukas Nykryn Date: Wed, 17 Apr 2013 10:32:50 +0200 Subject: [PATCH 3/6] ps: possibility to display uid of session owner for a process Library systemd-login offers possibility to display the Unix user identifier of the owner of the session of a process. This information will also be displayed for user processes which are shared between multiple login sessions of the same user, where sd_session will be blank. This patch adds output option "sd_ouid" which will show user UID or "-", when there is no owner for a process. To maintain compatibility with non-systemd systems, procps must be configured with --with-systemd option to enable this option. --- ps/output.c | 16 ++++++++++++++++ ps/ps.1 | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/ps/output.c b/ps/output.c index f1cf6bc0..012f5b69 100644 --- a/ps/output.c +++ b/ps/output.c @@ -1209,6 +1209,21 @@ fail: return 1; } +static int pr_sd_ouid(char *restrict const outbuf, const proc_t *restrict const pp){ + int r; + size_t len; + uid_t ouid; + + r = sd_pid_get_owner_uid(pp->tgid, &ouid); + if(r<0) goto fail; + return snprintf(outbuf, COLWID, "%d", ouid); + +fail: + outbuf[0] = '-'; + outbuf[1] = '\0'; + return 1; +} + #endif /****************** FLASK & seLinux security stuff **********************/ @@ -1538,6 +1553,7 @@ static const format_struct format_array[] = { {"scnt", "SCNT", pr_nop, sr_nop, 4, 0, DEC, AN|RIGHT}, /* man page misspelling of scount? */ {"scount", "SC", pr_nop, sr_nop, 4, 0, AIX, AN|RIGHT}, /* scnt==scount, DEC claims both */ #ifdef WITH_SYSTEMD +{"sd_ouid", "OWNER", pr_sd_ouid, sr_nop, 5, 0, LNX, ET|LEFT}, {"sd_session","SESSION", pr_sd_session, sr_nop, 11, 0, LNX, ET|LEFT}, {"sd_unit", "UNIT", pr_sd_unit, sr_nop, 31, 0, LNX, ET|LEFT}, #endif diff --git a/ps/ps.1 b/ps/ps.1 index f953e23a..7d2ce73f 100644 --- a/ps/ps.1 +++ b/ps/ps.1 @@ -1489,6 +1489,10 @@ SCHED_FIFO, SCHED_RR, SCHED_BATCH, SCHED_ISO, and SCHED_IDLE are respectively displayed as 0, 1, 2, 3, 4, and 5. T} +sd_ouid OWNER T{ +displays the Unix user identifier of the owner of the session of a process. +T} + sd_session SESSION T{ displays login session identifier of a process. T} From 4c1536d5f1a1bdab72b2b751a466c6e3868ed00b Mon Sep 17 00:00:00 2001 From: Lukas Nykryn Date: Mon, 22 Apr 2013 12:33:31 +0200 Subject: [PATCH 4/6] ps: possibility to display machine name for a process Library systemd-login offers possibility to display the name of the VM or container which process belongs to. This patch adds output option "sd_machine" which will show machine name or "-" when the name can not be determined. To maintain compatibility with non-systemd systems, procps must be configured with --with-systemd option to enable this option. --- configure.ac | 4 ++-- ps/output.c | 16 ++++++++++++++++ ps/ps.1 | 4 ++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index ea75f01d..17e35d1b 100644 --- a/configure.ac +++ b/configure.ac @@ -269,8 +269,8 @@ AC_ARG_WITH([systemd], [], [with_systemd=no]) if test "x$with_systemd" != xno; then - PKG_CHECK_MODULES([SYSTEMD], [libsystemd-login], [], [ - AC_CHECK_LIB(systemd-login, sd_pid_get_unit, [have_systemd=yes], [have_systemd=no]) + PKG_CHECK_MODULES([SYSTEMD], [libsystemd-login >= 202], [], [ + AC_CHECK_LIB(systemd-login, sd_pid_get_machine_name, [have_systemd=yes], [have_systemd=no]) if test "x$have_systemd" = xno; then AC_MSG_ERROR([systemd support missing/incomplete]) fi diff --git a/ps/output.c b/ps/output.c index 012f5b69..771d6ffc 100644 --- a/ps/output.c +++ b/ps/output.c @@ -1224,7 +1224,22 @@ fail: return 1; } +static int pr_sd_machine(char *restrict const outbuf, const proc_t *restrict const pp){ + int r; + size_t len; + char *machine; + r = sd_pid_get_machine_name(pp->tgid, &machine); + if(r<0) goto fail; + len = snprintf(outbuf, COLWID, "%s", machine); + free(machine); + return len; + +fail: + outbuf[0] = '-'; + outbuf[1] = '\0'; + return 1; +} #endif /****************** FLASK & seLinux security stuff **********************/ // move the bulk of this to libproc sometime @@ -1553,6 +1568,7 @@ static const format_struct format_array[] = { {"scnt", "SCNT", pr_nop, sr_nop, 4, 0, DEC, AN|RIGHT}, /* man page misspelling of scount? */ {"scount", "SC", pr_nop, sr_nop, 4, 0, AIX, AN|RIGHT}, /* scnt==scount, DEC claims both */ #ifdef WITH_SYSTEMD +{"sd_machine","MACHINE", pr_sd_machine, sr_nop, 31, 0, LNX, ET|LEFT}, {"sd_ouid", "OWNER", pr_sd_ouid, sr_nop, 5, 0, LNX, ET|LEFT}, {"sd_session","SESSION", pr_sd_session, sr_nop, 11, 0, LNX, ET|LEFT}, {"sd_unit", "UNIT", pr_sd_unit, sr_nop, 31, 0, LNX, ET|LEFT}, diff --git a/ps/ps.1 b/ps/ps.1 index 7d2ce73f..30718a15 100644 --- a/ps/ps.1 +++ b/ps/ps.1 @@ -1489,6 +1489,10 @@ SCHED_FIFO, SCHED_RR, SCHED_BATCH, SCHED_ISO, and SCHED_IDLE are respectively displayed as 0, 1, 2, 3, 4, and 5. T} +sd_machine MACHINE T{ +displays machine name for processes assigned to VM or container. +T} + sd_ouid OWNER T{ displays the Unix user identifier of the owner of the session of a process. T} From 38e8087d5580d870d5960f75670ab5c4f482d366 Mon Sep 17 00:00:00 2001 From: Lukas Nykryn Date: Mon, 22 Apr 2013 12:56:02 +0200 Subject: [PATCH 5/6] ps: possibility to display systemd user unit for a process Library systemd-login offers possibility to display name of systemd user unit for specific pid. Note that not all processes are part of a user unit. This patch adds output option "sd_uunit" which will show name of user unit or "-", when process does not belong to any user unit. This is similar to "sd_unit" but applies to user units instead of system units. To maintain compatibility with non-systemd systems, procps must be configured with --with-systemd option to enable this option. --- ps/output.c | 19 +++++++++++++++++++ ps/ps.1 | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/ps/output.c b/ps/output.c index 771d6ffc..4ccc6c3b 100644 --- a/ps/output.c +++ b/ps/output.c @@ -1240,6 +1240,24 @@ fail: outbuf[1] = '\0'; return 1; } + +static int pr_sd_uunit(char *restrict const outbuf, const proc_t *restrict const pp){ + int r; + size_t len; + char *unit; + + r = sd_pid_get_user_unit(pp->tgid, &unit); + if(r<0) goto fail; + len = snprintf(outbuf, COLWID, "%s", unit); + free(unit); + return len; + +fail: + outbuf[0] = '-'; + outbuf[1] = '\0'; + return 1; +} + #endif /****************** FLASK & seLinux security stuff **********************/ // move the bulk of this to libproc sometime @@ -1572,6 +1590,7 @@ static const format_struct format_array[] = { {"sd_ouid", "OWNER", pr_sd_ouid, sr_nop, 5, 0, LNX, ET|LEFT}, {"sd_session","SESSION", pr_sd_session, sr_nop, 11, 0, LNX, ET|LEFT}, {"sd_unit", "UNIT", pr_sd_unit, sr_nop, 31, 0, LNX, ET|LEFT}, +{"sd_uunit", "UUNIT", pr_sd_uunit, sr_nop, 31, 0, LNX, ET|LEFT}, #endif {"sess", "SESS", pr_sess, sr_session, 5, 0, XXX, PO|PIDMAX|RIGHT}, {"session", "SESS", pr_sess, sr_session, 5, 0, LNX, PO|PIDMAX|RIGHT}, diff --git a/ps/ps.1 b/ps/ps.1 index 30718a15..df2400fa 100644 --- a/ps/ps.1 +++ b/ps/ps.1 @@ -1505,6 +1505,10 @@ sd_unit UNIT T{ displays systemd unit which a process belongs to. T} +sd_uunit UUNIT T{ +displays systemd user unit which a process belongs to. +T} + sess SESS T{ session ID or, equivalently, the process ID of the session leader. (alias .BR session , \ sid ). From 7b50c2e9184a64f4b65a3197db24d271b3052b55 Mon Sep 17 00:00:00 2001 From: Lukas Nykryn Date: Mon, 22 Apr 2013 13:16:33 +0200 Subject: [PATCH 6/6] ps: possibility to display seat for a process Library systemd-login offers possibility to display name of seat for a session on multi-seat systems. This patch adds output option "sd_seat" which will show name of seat or "-", when name of seat can not be determined, but "seat0" should always exist. To maintain compatibility with non-systemd systems, procps must be configured with --with-systemd option to enable this option. --- ps/output.c | 21 +++++++++++++++++++++ ps/ps.1 | 4 ++++ 2 files changed, 25 insertions(+) diff --git a/ps/output.c b/ps/output.c index 4ccc6c3b..6b5f19c0 100644 --- a/ps/output.c +++ b/ps/output.c @@ -1258,6 +1258,26 @@ fail: return 1; } +static int pr_sd_seat(char *restrict const outbuf, const proc_t *restrict const pp){ + int r; + size_t len; + char *session; + char *seat; + r = sd_pid_get_session(pp->tgid, &session); + if(r<0) goto fail; + r = sd_session_get_seat(session, &seat); + free(session); + if(r<0) goto fail; + len = snprintf(outbuf, COLWID, "%s", seat); + free(seat); + return len; + +fail: + outbuf[0] = '-'; + outbuf[1] = '\0'; + return 1; +} + #endif /****************** FLASK & seLinux security stuff **********************/ // move the bulk of this to libproc sometime @@ -1588,6 +1608,7 @@ static const format_struct format_array[] = { #ifdef WITH_SYSTEMD {"sd_machine","MACHINE", pr_sd_machine, sr_nop, 31, 0, LNX, ET|LEFT}, {"sd_ouid", "OWNER", pr_sd_ouid, sr_nop, 5, 0, LNX, ET|LEFT}, +{"sd_seat", "SEAT", pr_sd_seat, sr_nop, 11, 0, LNX, ET|LEFT}, {"sd_session","SESSION", pr_sd_session, sr_nop, 11, 0, LNX, ET|LEFT}, {"sd_unit", "UNIT", pr_sd_unit, sr_nop, 31, 0, LNX, ET|LEFT}, {"sd_uunit", "UUNIT", pr_sd_uunit, sr_nop, 31, 0, LNX, ET|LEFT}, diff --git a/ps/ps.1 b/ps/ps.1 index df2400fa..af249c2c 100644 --- a/ps/ps.1 +++ b/ps/ps.1 @@ -1497,6 +1497,10 @@ sd_ouid OWNER T{ displays the Unix user identifier of the owner of the session of a process. T} +sd_seat SEAT T{ +displays login session identifier of a process. +T} + sd_session SESSION T{ displays login session identifier of a process. T}