summaryrefslogtreecommitdiff
path: root/core/notify.c
blob: 2e267dbe6c468fc26aa72ebd892db163bb34094a (plain)
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
#include "uwsgi.h"

extern struct uwsgi_server uwsgi;



void uwsgi_systemd_notify(char *message) {

	struct msghdr *msghdr = (struct msghdr *) uwsgi.notification_object;
	struct iovec *iovec = msghdr->msg_iov;

	iovec[0].iov_base = "STATUS=";
	iovec[0].iov_len = 7;

	iovec[1].iov_base = message;
	iovec[1].iov_len = strlen(message);

	iovec[2].iov_base = "\n";
	iovec[2].iov_len = 1;

	msghdr->msg_iovlen = 3;

	if (sendmsg(uwsgi.notification_fd, msghdr, 0) < 0) {
		uwsgi_error("sendmsg()");
	}
}

void uwsgi_systemd_notify_ready(void) {

	struct msghdr *msghdr = (struct msghdr *) uwsgi.notification_object;
	struct iovec *iovec = msghdr->msg_iov;

	iovec[0].iov_base = "STATUS=uWSGI is ready\nREADY=1\n";
	iovec[0].iov_len = 30;

	msghdr->msg_iovlen = 1;

	if (sendmsg(uwsgi.notification_fd, msghdr, 0) < 0) {
		uwsgi_error("sendmsg()");
	}
}


void uwsgi_systemd_init(char *systemd_socket) {

	struct sockaddr_un *sd_sun;
	struct msghdr *msghdr;

	uwsgi.notification_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
	if (uwsgi.notification_fd < 0) {
		uwsgi_error("socket()");
		return;
	}

	size_t len = strlen(systemd_socket);
	sd_sun = uwsgi_malloc(sizeof(struct sockaddr_un));
	memset(sd_sun, 0, sizeof(struct sockaddr_un));
	sd_sun->sun_family = AF_UNIX;
	strncpy(sd_sun->sun_path, systemd_socket, UMIN(len, sizeof(sd_sun->sun_path)));
	if (sd_sun->sun_path[0] == '@')
		sd_sun->sun_path[0] = 0;

	msghdr = uwsgi_malloc(sizeof(struct msghdr));
	memset(msghdr, 0, sizeof(struct msghdr));

	msghdr->msg_iov = uwsgi_malloc(sizeof(struct iovec) * 3);
	memset(msghdr->msg_iov, 0, sizeof(struct iovec) * 3);

	msghdr->msg_name = sd_sun;
	msghdr->msg_namelen = sizeof(struct sockaddr_un) - (sizeof(sd_sun->sun_path) - len);

	uwsgi.notification_object = msghdr;

	uwsgi.notify = uwsgi_systemd_notify;
	uwsgi.notify_ready = uwsgi_systemd_notify_ready;

}

int uwsgi_notify_socket_manage(int fd) {
	char buf[8192];
        ssize_t rlen = read(fd, buf, 8192);
        if (rlen < 0) {
                if (uwsgi_is_again()) return 0;
                uwsgi_error("uwsgi_notify_socket_manage()/read()");
                exit(1);
        }

	if (rlen > 0) {
		uwsgi_log_verbose("[notify-socket] %.*s\n", rlen, buf);
        }

        return 0;
}

int uwsgi_notify_msg(char *dst, char *msg, size_t len) {
	static int notify_fd = -1;
	if (notify_fd < 0) {
		notify_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
		if (notify_fd < 0) {
			uwsgi_error("uwsgi_notify_msg()/socket()");
			return -1;
		}
	}
	struct sockaddr_un un_addr;
	memset(&un_addr, 0, sizeof(struct sockaddr_un));
        un_addr.sun_family = AF_UNIX;
        // use 102 as the magic number
        strncat(un_addr.sun_path, dst, 102);
        if (sendto(notify_fd, msg, len, 0, (struct sockaddr *) &un_addr, sizeof(un_addr)) < 0) {
		return -1;
	}
	return 0;
}