-
Notifications
You must be signed in to change notification settings - Fork 0
/
libfreeinit.c
83 lines (66 loc) · 1.22 KB
/
libfreeinit.c
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
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "libfreeinit.h"
int
freeinit_notify(const char *info)
{
ssize_t r;
int fd;
const char *path;
if (info == NULL)
return -EINVAL;
path = getenv("NOTIFY_SOCKET");
if (path == NULL)
return 0;
fd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (fd < 0)
return -errno;
struct sockaddr_un addr;
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
r = sendto(fd, info, strlen(info), 0, (struct sockaddr *)&addr,
sizeof(struct sockaddr_un));
if (r < 0) {
r = errno;
close(fd);
return -r;
}
close(fd);
return r;
}
int
freeinit_notifyf(const char *fmt, ...)
{
char *info;
va_list args;
ssize_t r;
if (fmt == NULL)
return -EINVAL;
va_start(args, fmt);
r = vasprintf(&info, fmt, args);
va_end(args);
if (r < 0)
return -ENOMEM;
r = freeinit_notify(info);
free(info);
return r;
}
void
freeinit_clearenv_notify()
{
unsetenv("NOTIFY_SOCKET");
}
void
freeinit_clearenv_fdpassing()
{
unsetenv("LISTEN_FDS");
unsetenv("LISTEN_FDNAMES");
unsetenv("LISTEN_PID");
}