2020-10-07 13:42:13 +00:00
|
|
|
/*
|
|
|
|
htop - SystemdMeter.c
|
2020-12-04 12:42:00 +00:00
|
|
|
(C) 2020 htop dev team
|
2020-10-07 13:42:13 +00:00
|
|
|
Released under the GNU GPLv2, see the COPYING file
|
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SystemdMeter.h"
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <fcntl.h>
|
2020-11-18 13:26:30 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2020-10-07 13:42:13 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include "CRT.h"
|
2020-11-18 13:26:30 +00:00
|
|
|
#include "Macros.h"
|
|
|
|
#include "Object.h"
|
|
|
|
#include "RichString.h"
|
2020-10-07 13:42:13 +00:00
|
|
|
#include "XUtils.h"
|
|
|
|
|
2021-01-22 18:14:59 +00:00
|
|
|
#if defined(BUILD_STATIC) && defined(HAVE_LIBSYSTEMD)
|
|
|
|
#include <systemd/sd-bus.h>
|
|
|
|
#endif
|
2020-10-07 13:42:13 +00:00
|
|
|
|
2021-01-22 18:14:59 +00:00
|
|
|
|
|
|
|
#ifdef BUILD_STATIC
|
|
|
|
|
|
|
|
#define sym_sd_bus_open_system sd_bus_open_system
|
|
|
|
#define sym_sd_bus_get_property_string sd_bus_get_property_string
|
|
|
|
#define sym_sd_bus_get_property_trivial sd_bus_get_property_trivial
|
|
|
|
#define sym_sd_bus_unref sd_bus_unref
|
|
|
|
|
|
|
|
#else
|
2020-10-07 13:42:13 +00:00
|
|
|
|
|
|
|
typedef void sd_bus;
|
|
|
|
typedef void sd_bus_error;
|
|
|
|
static int (*sym_sd_bus_open_system)(sd_bus**);
|
|
|
|
static int (*sym_sd_bus_get_property_string)(sd_bus*, const char*, const char*, const char*, const char*, sd_bus_error*, char**);
|
|
|
|
static int (*sym_sd_bus_get_property_trivial)(sd_bus*, const char*, const char*, const char*, const char*, sd_bus_error*, char, void*);
|
|
|
|
static sd_bus* (*sym_sd_bus_unref)(sd_bus*);
|
2021-01-22 18:14:59 +00:00
|
|
|
static void* dlopenHandle = NULL;
|
|
|
|
|
|
|
|
#endif /* BUILD_STATIC */
|
|
|
|
|
|
|
|
#if !defined(BUILD_STATIC) || defined(HAVE_LIBSYSTEMD)
|
|
|
|
static sd_bus* bus = NULL;
|
|
|
|
#endif /* !BUILD_STATIC || HAVE_LIBSYSTEMD */
|
|
|
|
|
|
|
|
|
|
|
|
#define INVALID_VALUE ((unsigned int)-1)
|
2020-10-07 13:42:13 +00:00
|
|
|
|
|
|
|
static char* systemState = NULL;
|
|
|
|
static unsigned int nFailedUnits = INVALID_VALUE;
|
|
|
|
static unsigned int nInstalledJobs = INVALID_VALUE;
|
|
|
|
static unsigned int nNames = INVALID_VALUE;
|
|
|
|
static unsigned int nJobs = INVALID_VALUE;
|
|
|
|
|
|
|
|
static void SystemdMeter_done(ATTR_UNUSED Meter* this) {
|
|
|
|
free(systemState);
|
|
|
|
systemState = NULL;
|
|
|
|
|
2021-01-22 18:14:59 +00:00
|
|
|
#ifdef BUILD_STATIC
|
|
|
|
# ifdef HAVE_LIBSYSTEMD
|
|
|
|
if (bus) {
|
|
|
|
sym_sd_bus_unref(bus);
|
|
|
|
}
|
|
|
|
bus = NULL;
|
|
|
|
# endif /* HAVE_LIBSYSTEMD */
|
|
|
|
#else /* BUILD_STATIC */
|
2020-10-07 13:42:13 +00:00
|
|
|
if (bus && dlopenHandle) {
|
|
|
|
sym_sd_bus_unref(bus);
|
|
|
|
}
|
|
|
|
bus = NULL;
|
|
|
|
|
|
|
|
if (dlopenHandle) {
|
|
|
|
dlclose(dlopenHandle);
|
|
|
|
dlopenHandle = NULL;
|
|
|
|
}
|
2021-01-22 18:14:59 +00:00
|
|
|
#endif /* BUILD_STATIC */
|
2020-10-07 13:42:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 18:14:59 +00:00
|
|
|
#if !defined(BUILD_STATIC) || defined(HAVE_LIBSYSTEMD)
|
2020-10-07 13:42:13 +00:00
|
|
|
static int updateViaLib(void) {
|
2021-01-22 18:14:59 +00:00
|
|
|
#ifndef BUILD_STATIC
|
2020-10-07 13:42:13 +00:00
|
|
|
if (!dlopenHandle) {
|
|
|
|
dlopenHandle = dlopen("libsystemd.so.0", RTLD_LAZY);
|
|
|
|
if (!dlopenHandle)
|
|
|
|
goto dlfailure;
|
|
|
|
|
|
|
|
/* Clear any errors */
|
|
|
|
dlerror();
|
|
|
|
|
|
|
|
#define resolve(symbolname) do { \
|
|
|
|
*(void **)(&sym_##symbolname) = dlsym(dlopenHandle, #symbolname); \
|
|
|
|
if (!sym_##symbolname || dlerror() != NULL) \
|
|
|
|
goto dlfailure; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
resolve(sd_bus_open_system);
|
|
|
|
resolve(sd_bus_get_property_string);
|
|
|
|
resolve(sd_bus_get_property_trivial);
|
|
|
|
resolve(sd_bus_unref);
|
|
|
|
|
|
|
|
#undef resolve
|
|
|
|
}
|
2021-01-22 18:14:59 +00:00
|
|
|
#endif /* !BUILD_STATIC */
|
2020-10-07 13:42:13 +00:00
|
|
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
/* Connect to the system bus */
|
|
|
|
if (!bus) {
|
|
|
|
r = sym_sd_bus_open_system(&bus);
|
|
|
|
if (r < 0)
|
|
|
|
goto busfailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* const busServiceName = "org.freedesktop.systemd1";
|
|
|
|
static const char* const busObjectPath = "/org/freedesktop/systemd1";
|
|
|
|
static const char* const busInterfaceName = "org.freedesktop.systemd1.Manager";
|
|
|
|
|
|
|
|
r = sym_sd_bus_get_property_string(bus,
|
|
|
|
busServiceName, /* service to contact */
|
|
|
|
busObjectPath, /* object path */
|
|
|
|
busInterfaceName, /* interface name */
|
|
|
|
"SystemState", /* property name */
|
|
|
|
NULL, /* object to return error in */
|
|
|
|
&systemState);
|
|
|
|
if (r < 0)
|
|
|
|
goto busfailure;
|
|
|
|
|
|
|
|
r = sym_sd_bus_get_property_trivial(bus,
|
|
|
|
busServiceName, /* service to contact */
|
|
|
|
busObjectPath, /* object path */
|
|
|
|
busInterfaceName, /* interface name */
|
|
|
|
"NFailedUnits", /* property name */
|
|
|
|
NULL, /* object to return error in */
|
|
|
|
'u', /* property type */
|
|
|
|
&nFailedUnits);
|
|
|
|
if (r < 0)
|
|
|
|
goto busfailure;
|
|
|
|
|
|
|
|
r = sym_sd_bus_get_property_trivial(bus,
|
|
|
|
busServiceName, /* service to contact */
|
|
|
|
busObjectPath, /* object path */
|
|
|
|
busInterfaceName, /* interface name */
|
|
|
|
"NInstalledJobs", /* property name */
|
|
|
|
NULL, /* object to return error in */
|
|
|
|
'u', /* property type */
|
|
|
|
&nInstalledJobs);
|
|
|
|
if (r < 0)
|
|
|
|
goto busfailure;
|
|
|
|
|
|
|
|
r = sym_sd_bus_get_property_trivial(bus,
|
|
|
|
busServiceName, /* service to contact */
|
|
|
|
busObjectPath, /* object path */
|
|
|
|
busInterfaceName, /* interface name */
|
|
|
|
"NNames", /* property name */
|
|
|
|
NULL, /* object to return error in */
|
|
|
|
'u', /* property type */
|
|
|
|
&nNames);
|
|
|
|
if (r < 0)
|
|
|
|
goto busfailure;
|
|
|
|
|
|
|
|
r = sym_sd_bus_get_property_trivial(bus,
|
|
|
|
busServiceName, /* service to contact */
|
|
|
|
busObjectPath, /* object path */
|
|
|
|
busInterfaceName, /* interface name */
|
|
|
|
"NJobs", /* property name */
|
|
|
|
NULL, /* object to return error in */
|
|
|
|
'u', /* property type */
|
|
|
|
&nJobs);
|
|
|
|
if (r < 0)
|
|
|
|
goto busfailure;
|
|
|
|
|
|
|
|
/* success */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
busfailure:
|
|
|
|
sym_sd_bus_unref(bus);
|
|
|
|
bus = NULL;
|
|
|
|
return -2;
|
|
|
|
|
2021-01-22 18:14:59 +00:00
|
|
|
#ifndef BUILD_STATIC
|
2020-10-07 13:42:13 +00:00
|
|
|
dlfailure:
|
|
|
|
if (dlopenHandle) {
|
|
|
|
dlclose(dlopenHandle);
|
|
|
|
dlopenHandle = NULL;
|
|
|
|
}
|
|
|
|
return -1;
|
2021-01-22 18:14:59 +00:00
|
|
|
#endif /* !BUILD_STATIC */
|
2020-10-07 13:42:13 +00:00
|
|
|
}
|
2021-01-22 18:14:59 +00:00
|
|
|
#endif /* !BUILD_STATIC || HAVE_LIBSYSTEMD */
|
2020-10-07 13:42:13 +00:00
|
|
|
|
|
|
|
static void updateViaExec(void) {
|
2021-01-21 19:27:37 +00:00
|
|
|
if (Settings_isReadonly())
|
|
|
|
return;
|
|
|
|
|
2020-10-07 13:42:13 +00:00
|
|
|
int fdpair[2];
|
|
|
|
if (pipe(fdpair) < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pid_t child = fork();
|
|
|
|
if (child < 0) {
|
|
|
|
close(fdpair[1]);
|
|
|
|
close(fdpair[0]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child == 0) {
|
|
|
|
close(fdpair[0]);
|
|
|
|
dup2(fdpair[1], STDOUT_FILENO);
|
|
|
|
close(fdpair[1]);
|
|
|
|
int fdnull = open("/dev/null", O_WRONLY);
|
|
|
|
if (fdnull < 0)
|
|
|
|
exit(1);
|
|
|
|
dup2(fdnull, STDERR_FILENO);
|
|
|
|
close(fdnull);
|
|
|
|
execl("/bin/systemctl",
|
|
|
|
"/bin/systemctl",
|
|
|
|
"show",
|
|
|
|
"--property=SystemState",
|
|
|
|
"--property=NFailedUnits",
|
|
|
|
"--property=NNames",
|
|
|
|
"--property=NJobs",
|
|
|
|
"--property=NInstalledJobs",
|
|
|
|
NULL);
|
|
|
|
exit(127);
|
|
|
|
}
|
|
|
|
close(fdpair[1]);
|
|
|
|
|
|
|
|
int wstatus;
|
|
|
|
if (waitpid(child, &wstatus, 0) < 0 || !WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0) {
|
|
|
|
close(fdpair[0]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* commandOutput = fdopen(fdpair[0], "r");
|
|
|
|
if (!commandOutput) {
|
|
|
|
close(fdpair[0]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char lineBuffer[128];
|
|
|
|
while (fgets(lineBuffer, sizeof(lineBuffer), commandOutput)) {
|
|
|
|
if (String_startsWith(lineBuffer, "SystemState=")) {
|
|
|
|
char* newline = strchr(lineBuffer + strlen("SystemState="), '\n');
|
2020-11-16 15:50:08 +00:00
|
|
|
if (newline) {
|
2020-10-07 13:42:13 +00:00
|
|
|
*newline = '\0';
|
2020-11-16 15:50:08 +00:00
|
|
|
}
|
2021-01-05 13:47:49 +00:00
|
|
|
free_and_xStrdup(&systemState, lineBuffer + strlen("SystemState="));
|
2020-10-07 13:42:13 +00:00
|
|
|
} else if (String_startsWith(lineBuffer, "NFailedUnits=")) {
|
|
|
|
nFailedUnits = strtoul(lineBuffer + strlen("NFailedUnits="), NULL, 10);
|
|
|
|
} else if (String_startsWith(lineBuffer, "NNames=")) {
|
|
|
|
nNames = strtoul(lineBuffer + strlen("NNames="), NULL, 10);
|
|
|
|
} else if (String_startsWith(lineBuffer, "NJobs=")) {
|
|
|
|
nJobs = strtoul(lineBuffer + strlen("NJobs="), NULL, 10);
|
|
|
|
} else if (String_startsWith(lineBuffer, "NInstalledJobs=")) {
|
|
|
|
nInstalledJobs = strtoul(lineBuffer + strlen("NInstalledJobs="), NULL, 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(commandOutput);
|
|
|
|
}
|
|
|
|
|
2020-10-06 11:13:16 +00:00
|
|
|
static void SystemdMeter_updateValues(Meter* this) {
|
2020-10-07 13:42:13 +00:00
|
|
|
free(systemState);
|
|
|
|
systemState = NULL;
|
|
|
|
nFailedUnits = nInstalledJobs = nNames = nJobs = INVALID_VALUE;
|
|
|
|
|
2021-01-22 18:14:59 +00:00
|
|
|
#if !defined(BUILD_STATIC) || defined(HAVE_LIBSYSTEMD)
|
2020-10-07 13:42:13 +00:00
|
|
|
if (updateViaLib() < 0)
|
|
|
|
updateViaExec();
|
2021-01-22 18:14:59 +00:00
|
|
|
#else
|
|
|
|
updateViaExec();
|
|
|
|
#endif /* !BUILD_STATIC || HAVE_LIBSYSTEMD */
|
2020-10-07 13:42:13 +00:00
|
|
|
|
2020-10-06 11:13:16 +00:00
|
|
|
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%s", systemState ? systemState : "???");
|
2020-10-07 13:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int zeroDigitColor(unsigned int value) {
|
|
|
|
switch (value) {
|
|
|
|
case 0:
|
|
|
|
return CRT_colors[METER_VALUE];
|
|
|
|
case INVALID_VALUE:
|
|
|
|
return CRT_colors[METER_VALUE_ERROR];
|
|
|
|
default:
|
|
|
|
return CRT_colors[METER_VALUE_NOTICE];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int valueDigitColor(unsigned int value) {
|
|
|
|
switch (value) {
|
|
|
|
case 0:
|
|
|
|
return CRT_colors[METER_VALUE_NOTICE];
|
|
|
|
case INVALID_VALUE:
|
|
|
|
return CRT_colors[METER_VALUE_ERROR];
|
|
|
|
default:
|
|
|
|
return CRT_colors[METER_VALUE];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void SystemdMeter_display(ATTR_UNUSED const Object* cast, RichString* out) {
|
|
|
|
char buffer[16];
|
2021-04-14 18:47:42 +00:00
|
|
|
int len;
|
2020-10-07 13:42:13 +00:00
|
|
|
|
2021-01-27 14:12:06 +00:00
|
|
|
int color = (systemState && String_eq(systemState, "running")) ? METER_VALUE_OK : METER_VALUE_ERROR;
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_writeAscii(out, CRT_colors[color], systemState ? systemState : "N/A");
|
2020-10-07 13:42:13 +00:00
|
|
|
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_appendAscii(out, CRT_colors[METER_TEXT], " (");
|
2020-10-07 13:42:13 +00:00
|
|
|
|
|
|
|
if (nFailedUnits == INVALID_VALUE) {
|
|
|
|
buffer[0] = '?';
|
|
|
|
buffer[1] = '\0';
|
2021-04-14 18:47:42 +00:00
|
|
|
len = 1;
|
2020-10-07 13:42:13 +00:00
|
|
|
} else {
|
2021-04-14 18:47:42 +00:00
|
|
|
len = xSnprintf(buffer, sizeof(buffer), "%u", nFailedUnits);
|
2020-10-07 13:42:13 +00:00
|
|
|
}
|
2021-04-14 18:47:42 +00:00
|
|
|
RichString_appendnAscii(out, zeroDigitColor(nFailedUnits), buffer, len);
|
2020-10-07 13:42:13 +00:00
|
|
|
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_appendAscii(out, CRT_colors[METER_TEXT], "/");
|
2020-10-07 13:42:13 +00:00
|
|
|
|
|
|
|
if (nNames == INVALID_VALUE) {
|
|
|
|
buffer[0] = '?';
|
|
|
|
buffer[1] = '\0';
|
2021-04-14 18:47:42 +00:00
|
|
|
len = 1;
|
2020-10-07 13:42:13 +00:00
|
|
|
} else {
|
2021-04-14 18:47:42 +00:00
|
|
|
len = xSnprintf(buffer, sizeof(buffer), "%u", nNames);
|
2020-10-07 13:42:13 +00:00
|
|
|
}
|
2021-04-14 18:47:42 +00:00
|
|
|
RichString_appendnAscii(out, valueDigitColor(nNames), buffer, len);
|
2020-10-07 13:42:13 +00:00
|
|
|
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_appendAscii(out, CRT_colors[METER_TEXT], " failed) (");
|
2020-10-07 13:42:13 +00:00
|
|
|
|
|
|
|
if (nJobs == INVALID_VALUE) {
|
|
|
|
buffer[0] = '?';
|
|
|
|
buffer[1] = '\0';
|
2021-04-14 18:47:42 +00:00
|
|
|
len = 1;
|
2020-10-07 13:42:13 +00:00
|
|
|
} else {
|
2021-04-14 18:47:42 +00:00
|
|
|
len = xSnprintf(buffer, sizeof(buffer), "%u", nJobs);
|
2020-10-07 13:42:13 +00:00
|
|
|
}
|
2021-04-14 18:47:42 +00:00
|
|
|
RichString_appendnAscii(out, zeroDigitColor(nJobs), buffer, len);
|
2020-10-07 13:42:13 +00:00
|
|
|
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_appendAscii(out, CRT_colors[METER_TEXT], "/");
|
2020-10-07 13:42:13 +00:00
|
|
|
|
|
|
|
if (nInstalledJobs == INVALID_VALUE) {
|
|
|
|
buffer[0] = '?';
|
|
|
|
buffer[1] = '\0';
|
2021-04-14 18:47:42 +00:00
|
|
|
len = 1;
|
2020-10-07 13:42:13 +00:00
|
|
|
} else {
|
2021-04-14 18:47:42 +00:00
|
|
|
len = xSnprintf(buffer, sizeof(buffer), "%u", nInstalledJobs);
|
2020-10-07 13:42:13 +00:00
|
|
|
}
|
2021-04-14 18:47:42 +00:00
|
|
|
RichString_appendnAscii(out, valueDigitColor(nInstalledJobs), buffer, len);
|
2020-10-07 13:42:13 +00:00
|
|
|
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_appendAscii(out, CRT_colors[METER_TEXT], " jobs)");
|
2020-10-07 13:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const int SystemdMeter_attributes[] = {
|
|
|
|
METER_VALUE
|
|
|
|
};
|
|
|
|
|
|
|
|
const MeterClass SystemdMeter_class = {
|
|
|
|
.super = {
|
|
|
|
.extends = Class(Meter),
|
|
|
|
.delete = Meter_delete,
|
|
|
|
.display = SystemdMeter_display
|
|
|
|
},
|
|
|
|
.updateValues = SystemdMeter_updateValues,
|
|
|
|
.done = SystemdMeter_done,
|
|
|
|
.defaultMode = TEXT_METERMODE,
|
|
|
|
.maxItems = 0,
|
|
|
|
.total = 100.0,
|
|
|
|
.attributes = SystemdMeter_attributes,
|
|
|
|
.name = "Systemd",
|
|
|
|
.uiName = "Systemd state",
|
|
|
|
.description = "Systemd system state and unit overview",
|
|
|
|
.caption = "Systemd: ",
|
|
|
|
};
|