Skip to content

Commit 8f5757b

Browse files
committed
Merge pull request #178 from acv/refactor-wdctl-thread
Improve the wdctl_thread code
2 parents ec23a7b + 0760602 commit 8f5757b

File tree

1 file changed

+47
-67
lines changed

1 file changed

+47
-67
lines changed

src/wdctl_thread.c

Lines changed: 47 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "safe.h"
5656

5757

58+
static int create_unix_socket(const char *);
5859
static int write_to_socket(int, char *, size_t);
5960
static void *thread_wdctl_handler(void *);
6061
static void wdctl_status(int);
@@ -64,59 +65,72 @@ static void wdctl_restart(int);
6465

6566
static int wdctl_socket_server;
6667

67-
/** Launches a thread that monitors the control socket for request
68-
@param arg Must contain a pointer to a string containing the Unix domain socket to open
69-
@todo This thread loops infinitely, need a watchdog to verify that it is still running?
70-
*/
71-
void
72-
thread_wdctl(void *arg)
68+
static int
69+
create_unix_socket(const char *sock_name)
7370
{
74-
int *fd;
75-
char *sock_name;
7671
struct sockaddr_un sa_un;
77-
int result;
78-
pthread_t tid;
79-
socklen_t len;
80-
81-
debug(LOG_DEBUG, "Starting wdctl.");
72+
int sock;
8273

8374
memset(&sa_un, 0, sizeof(sa_un));
84-
sock_name = (char *)arg;
85-
debug(LOG_DEBUG, "Socket name: %s", sock_name);
8675

8776
if (strlen(sock_name) > (sizeof(sa_un.sun_path) - 1)) {
8877
/* TODO: Die handler with logging.... */
8978
debug(LOG_ERR, "WDCTL socket name too long");
90-
exit(1);
79+
return -1;
9180
}
9281

93-
debug(LOG_DEBUG, "Creating socket");
94-
wdctl_socket_server = socket(PF_UNIX, SOCK_STREAM, 0);
82+
sock = socket(PF_UNIX, SOCK_STREAM, 0);
9583

96-
if (wdctl_socket_server < 0) {
97-
debug(LOG_DEBUG, "Could not get server socket: %s", strerror(errno));
98-
termination_handler(0);
84+
if (sock < 0) {
85+
debug(LOG_DEBUG, "Could not get unix socket: %s", strerror(errno));
86+
return -1;
9987
}
100-
debug(LOG_DEBUG, "Got server socket %d", wdctl_socket_server);
88+
debug(LOG_DEBUG, "Got unix socket %d", sock);
10189

10290
/* If it exists, delete... Not the cleanest way to deal. */
10391
unlink(sock_name);
10492

10593
debug(LOG_DEBUG, "Filling sockaddr_un");
106-
strcpy(sa_un.sun_path, sock_name); /* XXX No size check because we
107-
* check a few lines before. */
94+
strcpy(sa_un.sun_path, sock_name);
10895
sa_un.sun_family = AF_UNIX;
10996

11097
debug(LOG_DEBUG, "Binding socket (%s) (%d)", sa_un.sun_path, strlen(sock_name));
11198

11299
/* Which to use, AF_UNIX, PF_UNIX, AF_LOCAL, PF_LOCAL? */
113-
if (bind(wdctl_socket_server, (struct sockaddr *)&sa_un, sizeof(struct sockaddr_un))) {
114-
debug(LOG_ERR, "Could not bind control socket: %s", strerror(errno));
115-
termination_handler(0);
100+
if (bind(sock, (struct sockaddr *)&sa_un, sizeof(struct sockaddr_un))) {
101+
debug(LOG_ERR, "Could not bind unix socket: %s", strerror(errno));
102+
return -1;
116103
}
117104

118-
if (listen(wdctl_socket_server, 5)) {
105+
if (listen(sock, 5)) {
119106
debug(LOG_ERR, "Could not listen on control socket: %s", strerror(errno));
107+
return -1;
108+
}
109+
return sock;
110+
}
111+
112+
/** Launches a thread that monitors the control socket for request
113+
@param arg Must contain a pointer to a string containing the Unix domain socket to open
114+
@todo This thread loops infinitely, need a watchdog to verify that it is still running?
115+
*/
116+
void
117+
thread_wdctl(void *arg)
118+
{
119+
int *fd;
120+
char *sock_name;
121+
struct sockaddr_un sa_un;
122+
int result;
123+
pthread_t tid;
124+
socklen_t len;
125+
126+
debug(LOG_DEBUG, "Starting wdctl.");
127+
128+
sock_name = (char *)arg;
129+
debug(LOG_DEBUG, "Socket name: %s", sock_name);
130+
131+
debug(LOG_DEBUG, "Creating socket");
132+
wdctl_socket_server = create_unix_socket(sock_name);
133+
if (-1 == wdctl_socket_server) {
120134
termination_handler(0);
121135
}
122136

@@ -251,9 +265,9 @@ wdctl_restart(int afd)
251265
{
252266
int sock, fd;
253267
char *sock_name;
254-
struct sockaddr_un sa_un;
255268
s_config *conf = NULL;
256-
t_client *client = NULL;
269+
struct sockaddr_un sa_un;
270+
t_client *client;
257271
char *tempstring = NULL;
258272
pid_t pid;
259273
socklen_t len;
@@ -262,47 +276,13 @@ wdctl_restart(int afd)
262276

263277
debug(LOG_NOTICE, "Will restart myself");
264278

265-
/*
266-
* First, prepare the internal socket
267-
*/
268-
memset(&sa_un, 0, sizeof(sa_un));
279+
/* First, prepare the internal socket */
269280
sock_name = conf->internal_sock;
270281
debug(LOG_DEBUG, "Socket name: %s", sock_name);
271282

272-
if (strlen(sock_name) > (sizeof(sa_un.sun_path) - 1)) {
273-
/* TODO: Die handler with logging.... */
274-
debug(LOG_ERR, "INTERNAL socket name too long");
275-
return;
276-
}
277-
278283
debug(LOG_DEBUG, "Creating socket");
279-
sock = socket(PF_UNIX, SOCK_STREAM, 0);
280-
281-
if (sock < 0) {
282-
debug(LOG_DEBUG, "Could not get server socket: %s", strerror(errno));
283-
return;
284-
}
285-
debug(LOG_DEBUG, "Got internal socket %d", sock);
286-
287-
/* If it exists, delete... Not the cleanest way to deal. */
288-
unlink(sock_name);
289-
290-
debug(LOG_DEBUG, "Filling sockaddr_un");
291-
strcpy(sa_un.sun_path, sock_name); /* XXX No size check because we check a few lines before. */
292-
sa_un.sun_family = AF_UNIX;
293-
294-
debug(LOG_DEBUG, "Binding socket (%s) (%d)", sa_un.sun_path, strlen(sock_name));
295-
296-
/* Which to use, AF_UNIX, PF_UNIX, AF_LOCAL, PF_LOCAL? */
297-
if (bind(sock, (struct sockaddr *)&sa_un, strlen(sock_name) + sizeof(sa_un.sun_family))) {
298-
debug(LOG_ERR, "Could not bind internal socket: %s", strerror(errno));
299-
close(sock);
300-
return;
301-
}
302-
303-
if (listen(sock, 5)) {
304-
debug(LOG_ERR, "Could not listen on internal socket: %s", strerror(errno));
305-
close(sock);
284+
sock = create_unix_socket(sock_name);
285+
if (-1 == sock) {
306286
return;
307287
}
308288

0 commit comments

Comments
 (0)