File: sd-daemon.c

package info (click to toggle)
finit 4.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,216 kB
  • sloc: ansic: 17,060; sh: 6,281; makefile: 532
file content (221 lines) | stat: -rw-r--r-- 4,135 bytes parent folder | download
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
/* SPDX-License-Identifier: MIT-0 */

/*
 * systemd notify protocol, supporting readiness notification on startup
 * and reloading, according to the protocol defined at:
 * https://www.freedesktop.org/software/systemd/man/latest/sd_notify.html
 *
 * This protocol is guaranteed to be stable as per:
 * https://systemd.io/PORTABILITY_AND_STABILITY/
 */

#define _GNU_SOURCE 1
#include <errno.h>
#include <inttypes.h>
#include <signal.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>
#include <unistd.h>
#ifdef _LIBITE_LITE
# include <libite/lite.h>
#else
# include <lite/lite.h>
#endif

#include "sd-daemon.h"

/*
 * NOTE: As per spec., this function does not return POSIX OK(0) but
 *       rather "a positive value", i.e., 1.
 */
int sd_notify(int unset_environment, const char *state)
{
	struct sockaddr_un sun = {
		.sun_family = AF_UNIX,
	};
	size_t len, sun_len;
	const char *path;
	ssize_t written;
	int sd;

	if (!state)
		return -EINVAL;

	len = strlen(state);
	if (len == 0)
		return -EINVAL;

	/* If the variable is not set, the protocol is a noop */
	path = getenv("NOTIFY_SOCKET");
	if (!path)
		return 0;	/* Not set? Nothing to do */

	/* Only AF_UNIX is supported, with path or abstract sockets */
	if (path[0] != '/' && path[0] != '@')
		return -EAFNOSUPPORT;

	/* Ensure there is room for NUL byte */
	sun_len = strlen(path);
	if (sun_len >= sizeof(sun.sun_path))
		return -E2BIG;

	strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
	if (sun.sun_path[0] == '@')
		sun.sun_path[0] = 0;

	sd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
	if (sd == -1)
		return -errno;

	if (connect(sd, (struct sockaddr *)&sun, offsetof(struct sockaddr_un, sun_path) + sun_len) == -1) {
		close(sd);
		return -errno;
	}

	if (unset_environment)
		unsetenv("NOTIFY_SOCKET");

	written = write(sd, state, len);
	close(sd);
	if (written != (ssize_t) len)
		return written < 0 ? -errno : -EPROTO;

	return 1;
}

/*
 * Printf-style notification function
 */
int sd_notifyf(int unset_environment, const char *format, ...)
{
	va_list ap;
	char *state;
	int ret;

	if (!format)
		return -EINVAL;

	va_start(ap, format);
	ret = vasprintf(&state, format, ap);
	va_end(ap);

	if (ret < 0)
		return -ENOMEM;

	ret = sd_notify(unset_environment, state);
	free(state);

	return ret;
}

/*
 * PID-specific notification function
 */
int sd_pid_notify(pid_t pid, int unset_environment, const char *state)
{
	char path[256], *env;
	size_t len;
	int ret;

	if (!state)
		return -EINVAL;

	len = strlen(state);
	if (len == 0)
		return -EINVAL;

	if (pid == 0)
		pid = getpid();

	env = getenv("NOTIFY_SOCKET");
	if (env) {
		env = strdup(env);
		if (!env)
			return -errno;
	}

	snprintf(path, sizeof(path), "@run/finit/notify/%d", pid);
	setenv("NOTIFY_SOCKET", path, 1);

	ret = sd_notify(unset_environment, state);

	if (env) {
		setenv("NOTIFY_SOCKET", env, 1);
		free(env);
	}

	return ret;
}

/*
 * PID-specific printf-style notification function
 */
int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...)
{
	va_list ap;
	char *state;
	int ret;

	if (!format)
		return -EINVAL;

	va_start(ap, format);
	ret = vasprintf(&state, format, ap);
	va_end(ap);

	if (ret < 0)
		return -ENOMEM;

	ret = sd_pid_notify(pid, unset_environment, state);
	free(state);

	return ret;
}

/*
 * Socket activation stub - Finit doesn't support socket activation yet
 * Returns 0 (no file descriptors passed)
 */
int sd_listen_fds(int unset_environment)
{
	if (unset_environment) {
		unsetenv("LISTEN_PID");
		unsetenv("LISTEN_FDS");
		unsetenv("LISTEN_FDNAMES");
	}

	return 0;
}

/*
 * Watchdog stub - Finit doesn't support watchdog yet
 * Returns 0 (no watchdog enabled)
 */
int sd_watchdog_enabled(int unset_environment, uint64_t *usec)
{
	if (unset_environment) {
		unsetenv("WATCHDOG_PID");
		unsetenv("WATCHDOG_USEC");
	}

	if (usec)
		*usec = 0;

	return 0;
}

/*
 * System detection function
 * Returns 1 to indicate systemd compatibility (trick applications)
 */
int sd_booted(void)
{
	return 1;
}