summaryrefslogtreecommitdiffstats
path: root/linux/SystemdMeter.c
blob: 567cfc7c9697afb90fcea48960aff066acf63d0d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
htop - SystemdMeter.c
(C) 2020 htop dev team
Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/

#include "linux/SystemdMeter.h"

#include <dlfcn.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>

#include "CRT.h"
#include "Macros.h"
#include "Object.h"
#include "RichString.h"
#include "Settings.h"
#include "XUtils.h"

#if defined(BUILD_STATIC) && defined(HAVE_LIBSYSTEMD)
#include <systemd/sd-bus.h>
#endif


#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

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*);
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)

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;

#ifdef BUILD_STATIC
# ifdef HAVE_LIBSYSTEMD
   if (bus) {
      sym_sd_bus_unref(bus);
   }
   bus = NULL;
# endif /* HAVE_LIBSYSTEMD */
#else /* BUILD_STATIC */
   if (bus && dlopenHandle) {
      sym_sd_bus_unref(bus);
   }
   bus = NULL;

   if (dlopenHandle) {
      dlclose(dlopenHandle);
      dlopenHandle = NULL;
   }
#endif /* BUILD_STATIC */
}

#if !defined(BUILD_STATIC) || defined(HAVE_LIBSYSTEMD)
static int updateViaLib(void) {
#ifndef BUILD_STATIC
   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
   }
#endif /* !BUILD_STATIC */

   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;

#ifndef BUILD_STATIC
dlfailure:
   if (dlopenHandle) {
      dlclose(dlopenHandle);
      dlopenHandle = NULL;
   }
   return -1;
#endif /* !BUILD_STATIC */
}
#endif /* !BUILD_STATIC || HAVE_LIBSYSTEMD */

static void updateViaExec(void) {
   if (Settings_isReadonly())
      return;

   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);
      execlp("systemctl",
             "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');
         if (newline) {
            *newline = '\0';
         }
         free_and_xStrdup(&systemState, lineBuffer + strlen("SystemState="));
      } 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);
}

static void SystemdMeter_updateValues(Meter* this) {
   free(systemState);
   systemState = NULL;
   nFailedUnits = nInstalledJobs = nNames = nJobs = INVALID_VALUE;

#if !defined(BUILD_STATIC) || defined(HAVE_LIBSYSTEMD)
   if (updateViaLib() < 0)
      updateViaExec();
#else
   updateViaExec();
#endif /* !BUILD_STATIC || HAVE_LIBSYSTEMD */

   xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%s", systemState ? systemState : "???");
}

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];
   int len;

   int color = (systemState && String_eq(systemState, "running")) ? METER_VALUE_OK : METER_VALUE_ERROR;
   RichString_writeAscii(out, CRT_colors[color], systemState ? systemState : "N/A");

   RichString_appendAscii(out, CRT_colors[METER_TEXT], " (");

   if (nFailedUnits == INVALID_VALUE) {
      buffer[0] = '?';
      buffer[1] = '\0';
      len = 1;
   } else {
      len = xSnprintf(buffer, sizeof(buffer), "%u", nFailedUnits);
   }
   RichString_appendnAscii(out, zeroDigitColor(nFailedUnits), buffer, len);

   RichString_appendAscii(out, CRT_colors[METER_TEXT], "/");

   if (nNames == INVALID_VALUE) {
      buffer[0] = '?';
      buffer[1] = '\0';
      len = 1;
   } else {
      len = xSnprintf(buffer, sizeof(buffer), "%u", nNames);
   }
   RichString_appendnAscii(out, valueDigitColor(nNames), buffer, len);

   RichString_appendAscii(out, CRT_colors[METER_TEXT], " failed) (");

   if (nJobs == INVALID_VALUE) {
      buffer[0] = '?';
      buffer[1] = '\0';
      len = 1;
   } else {
      len = xSnprintf(buffer, sizeof(buffer), "%u", nJobs);
   }
   RichString_appendnAscii(out, zeroDigitColor(nJobs), buffer, len);

   RichString_appendAscii(out, CRT_colors[METER_TEXT], "/");

   if (nInstalledJobs == INVALID_VALUE) {
      buffer[0] = '?';
      buffer[1] = '\0';
      len = 1;
   } else {
      len = xSnprintf(buffer, sizeof(buffer), "%u", nInstalledJobs);
   }
   RichString_appendnAscii(out, valueDigitColor(nInstalledJobs), buffer, len);

   RichString_appendAscii(out, CRT_colors[METER_TEXT], " jobs)");
}

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: ",
};

© 2014-2024 Faster IT GmbH | imprint | privacy policy