Debian Codegraph @

src/alt/nametag_libsystemd.c

raw
1 /*
2 * Copyright (C) 2016 Jan Pokorny <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include <assert.h>
20 #include <ctype.h>
21 #include <stdarg.h>
22 #include <string.h>
23
24 #include <systemd/sd-daemon.h>
25
26 #include "nametag_libsystemd.h"
27 #include "booth.h"
28 #include "log.h"
29 #include "transport.h"
30
31 /* assume first argument after "fmt" is for DAEMON_NAME, that is
32 really not of interest in our "nametag" function based on
33 sd_notify (that very data point is provided implicitly) */
34 void sd_notify_wrapper(const char *fmt, ...)
35 {
36 /* assume that first %s in fmt is intended for DAEMON_NAME,
37 i.e., for first argument following fmt in original
38 set_proc_title invocation, which has already been dropped
39 before it boils down here (using the wrapping macro trick);
40 we now simply append the reset after that first %s
41 (with whitespace stripped) to the "Running: " prefix */
42 int rv;
43 char buffer[255];
44 char *fmt_iter;
45 char *suffix = NULL;
46 va_list ap;
47
48 switch (local->type) {
49 case ARBITRATOR:
50 case GEOSTORE:
51 break;
52 default:
53 return; /* not expected to be run as system service */
54 }
55
56 fmt_iter = strchr(fmt, '%');
57 while (fmt_iter) {
58 switch (*++fmt_iter) {
59 case 's': suffix = fmt_iter;
60 /* fall through */
61 default: fmt_iter = NULL;
62 }
63 }
64 if (!suffix) {
65 log_warn("%s:%d: invalid format: %s", __FILE__, __LINE__, fmt);
66 return;
67 }
68 while (isspace(*++suffix)) /* noop */ ;
69
70 va_start(ap, fmt);
71 fmt_iter = va_arg(ap, char *); /* just shift by one */
72 assert(!strcmp(fmt_iter, DAEMON_NAME));
73 rv = vsnprintf(buffer, sizeof(buffer), suffix, ap);
74 va_end(ap);
75
76 rv = sd_notifyf(0, "READY=1\n"
77 "STATUS=Running: %s",
78 buffer);
79 if (rv < 0)
80 log_warn("%s:%d: sd_notifyf fail", __FILE__, __LINE__);
81 }