Skip to content

Commit 92e8787

Browse files
Alphixsimo5
authored andcommitted
[gssproxy] Change daemon to Type=notify with systemd
This avoids the need for daemonization, pid files, etc and also provides nicer output from systemctl. The notify integration is already prepared to work with Type=notify-reload (which is a bit too recent to make the default at the moment, requires systemd 253+). With this patch applied: root@qtest1:~# systemctl status gssproxy ● gssproxy.service - GSSAPI Proxy Daemon Loaded: loaded (/lib/systemd/system/gssproxy.service; enabled; preset: enabled) Active: active (running) since Fri 2023-10-20 12:59:32 CEST; 4s ago Main PID: 58516 (gssproxy) Status: "Running, 1 service(s) configured" ... root@qtest1:~# ls -1 /etc/gssproxy/ 24-nfs-server.conf gssproxy.conf root@qtest1:~# vi /etc/gssproxy/50-nfs-client.conf root@qtest1:~# ls -1 /etc/gssproxy/ 24-nfs-server.conf 50-nfs-client.conf gssproxy.conf root@qtest1:~# systemctl reload gssproxy root@qtest1:~# systemctl status gssproxy ● gssproxy.service - GSSAPI Proxy Daemon Loaded: loaded (/lib/systemd/system/gssproxy.service; enabled; preset: enabled) Active: active (running) since Fri 2023-10-20 12:59:32 CEST; 1min 39s ago Process: 58576 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS) Main PID: 58516 (gssproxy) Status: "Running, 2 service(s) configured" ... Signed-off-by: David Härdeman <david@hardeman.nu>
1 parent b700431 commit 92e8787

File tree

7 files changed

+67
-8
lines changed

7 files changed

+67
-8
lines changed

contrib/gssproxy.spec.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ BuildRequires: libcap-devel
4444
BuildRequires: popt-devel
4545
BuildRequires: findutils
4646
BuildRequires: systemd-units
47+
BuildRequires: systemd-devel
4748

4849

4950
%description

src/gp_common.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@
4646
/* max out at 1MB for now */
4747
#define MAX_RPC_SIZE 1024*1024
4848

49+
#ifdef HAVE_SYSTEMD_DAEMON
50+
#include <systemd/sd-daemon.h>
51+
#else
52+
__inline__ int sd_notifyf(int unset_environment UNUSED, const char *format UNUSED, ...)
53+
{
54+
return 0;
55+
}
56+
#endif
57+
58+
uint64_t time_now_usec(void);
4959
bool gp_same(const char *a, const char *b);
5060
bool gp_boolean_is_true(const char *s);
5161
char *gp_getenv(const char *name);

src/gp_init.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <sys/stat.h>
1515
#include <sys/types.h>
1616
#include <unistd.h>
17+
#include <inttypes.h>
1718

1819
#ifdef HAVE_CAP
1920

@@ -260,10 +261,19 @@ static void hup_handler(verto_ctx *vctx UNUSED, verto_ev *ev)
260261

261262
gpctx = verto_get_private(ev);
262263

264+
sd_notifyf(0, "RELOADING=1\n"
265+
"MONOTONIC_USEC=%" PRIu64 "\n"
266+
"STATUS=Reloading configuration\n",
267+
time_now_usec());
268+
263269
GPDEBUG("Received SIGHUP; re-reading config.\n");
264270
new_config = read_config(gpctx->config_file, gpctx->config_dir,
265271
gpctx->config_socket, gpctx->daemonize);
266272
if (!new_config) {
273+
sd_notifyf(0, "READY=1\n"
274+
"STATUS=Running, %i service(s) configured"
275+
" (failed to re-read config)\n",
276+
gpctx->config->num_svcs);
267277
GPERROR("Error reading new configuration on SIGHUP; keeping old "
268278
"configuration instead!\n");
269279
return;
@@ -281,12 +291,16 @@ static void hup_handler(verto_ctx *vctx UNUSED, verto_ev *ev)
281291

282292
free_config(&old_config);
283293

294+
sd_notifyf(0, "READY=1\n"
295+
"STATUS=Running, %i service(s) configured\n",
296+
gpctx->config->num_svcs);
284297
GPDEBUG("New config loaded successfully.\n");
285298
return;
286299
}
287300

288301
static void break_loop(verto_ctx *vctx, verto_ev *ev UNUSED)
289302
{
303+
sd_notifyf(0, "STOPPING=1\nSTATUS=Signal received, stopping\n");
290304
GPDEBUG("Exiting after receiving a signal\n");
291305
verto_break(vctx);
292306
}
@@ -354,11 +368,14 @@ void init_event_loop(struct gssproxy_ctx *gpctx)
354368
* is done. */
355369
static void delayed_init(verto_ctx *vctx UNUSED, verto_ev *ev)
356370
{
357-
struct gssproxy_ctx *gpctx;
371+
struct gssproxy_ctx *gpctx = verto_get_private(ev);
372+
373+
sd_notifyf(0, "READY=1\n"
374+
"STATUS=Running, %i service(s) configured\n",
375+
gpctx->config->num_svcs);
358376

359377
GPDEBUG("Initialization complete.\n");
360378

361-
gpctx = verto_get_private(ev);
362379
idle_handler(gpctx);
363380
}
364381

src/gp_mgmt.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ static void idle_terminate(verto_ctx *vctx, verto_ev *ev)
1818
{
1919
struct gssproxy_ctx *gpctx = verto_get_private(ev);
2020

21+
sd_notifyf(0, "STOPPING=1\nSTATUS=Idle for %ld seconds, stopping\n",
22+
(long)gpctx->term_timeout/1000);
23+
2124
GPDEBUG("Terminating, after idling for %ld seconds!\n",
2225
(long)gpctx->term_timeout/1000);
2326
verto_break(vctx);

src/gp_util.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,38 @@
77
#include <stdio.h>
88
#include <errno.h>
99
#include <unistd.h>
10+
#include <stdint.h>
11+
#include <time.h>
1012

1113
#include "gp_common.h"
1214

15+
#define USEC_INFINITY ((uint64_t)UINT64_MAX)
16+
#define NSEC_PER_USEC ((uint64_t)1000ULL)
17+
#define USEC_PER_SEC ((uint64_t)1000000ULL)
18+
uint64_t time_now_usec(void)
19+
{
20+
struct timespec ts;
21+
22+
if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
23+
goto out;
24+
}
25+
26+
if (ts.tv_sec < 0 || ts.tv_nsec < 0) {
27+
goto out;
28+
}
29+
30+
if ((uint64_t)ts.tv_sec >
31+
(UINT64_MAX - (ts.tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC) {
32+
goto out;
33+
}
34+
35+
return (uint64_t)ts.tv_sec * USEC_PER_SEC +
36+
(uint64_t)ts.tv_nsec / NSEC_PER_USEC;
37+
38+
out:
39+
return USEC_INFINITY;
40+
}
41+
1342
bool gp_same(const char *a, const char *b)
1443
{
1544
if (a == b || (a && b && strcmp(a, b) == 0)) {

systemd/gssproxy.service.in

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ Before=rpc-gssd.service
77
[Service]
88
StateDirectory=gssproxy/clients gssproxy/rcache
99
Environment=KRB5RCACHEDIR=/var/lib/gssproxy/rcache
10-
ExecStart=@sbindir@/gssproxy -D
11-
# These two should be used with traditional UNIX forking daemons
12-
# consult systemd.service(5) for more details
13-
Type=forking
14-
PIDFile=/run/gssproxy.pid
10+
ExecStart=@sbindir@/gssproxy -i
11+
# This can be changed to notify-reload and ExecReload= can be removed once
12+
# systemd 253 is common enough
13+
Type=notify
1514
ExecReload=/bin/kill -HUP $MAINPID
1615

1716
ProtectSystem=full

systemd/gssuserproxy.service.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Description=GSS User Proxy
33
Documentation=man:gssproxy(8)
44

55
[Service]
6-
Type=exec
6+
Type=notify
77
StandardError=journal
88
ExecStart=@sbindir@/gssproxy -i -u
99
Restart=on-failure

0 commit comments

Comments
 (0)