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.
This commit is contained in:
Lukas Nykryn
2013-04-16 14:42:30 +02:00
parent 53fd7dd1ed
commit 7ab5d33c5c
4 changed files with 51 additions and 0 deletions

View File

@@ -71,6 +71,10 @@
#include "common.h"
#ifdef WITH_SYSTEMD
#include <systemd/sd-login.h>
#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 */