55
55
#include "safe.h"
56
56
57
57
58
+ static int create_unix_socket (const char * );
58
59
static int write_to_socket (int , char * , size_t );
59
60
static void * thread_wdctl_handler (void * );
60
61
static void wdctl_status (int );
@@ -64,59 +65,72 @@ static void wdctl_restart(int);
64
65
65
66
static int wdctl_socket_server ;
66
67
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 )
73
70
{
74
- int * fd ;
75
- char * sock_name ;
76
71
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 ;
82
73
83
74
memset (& sa_un , 0 , sizeof (sa_un ));
84
- sock_name = (char * )arg ;
85
- debug (LOG_DEBUG , "Socket name: %s" , sock_name );
86
75
87
76
if (strlen (sock_name ) > (sizeof (sa_un .sun_path ) - 1 )) {
88
77
/* TODO: Die handler with logging.... */
89
78
debug (LOG_ERR , "WDCTL socket name too long" );
90
- exit ( 1 ) ;
79
+ return -1 ;
91
80
}
92
81
93
- debug (LOG_DEBUG , "Creating socket" );
94
- wdctl_socket_server = socket (PF_UNIX , SOCK_STREAM , 0 );
82
+ sock = socket (PF_UNIX , SOCK_STREAM , 0 );
95
83
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 ;
99
87
}
100
- debug (LOG_DEBUG , "Got server socket %d" , wdctl_socket_server );
88
+ debug (LOG_DEBUG , "Got unix socket %d" , sock );
101
89
102
90
/* If it exists, delete... Not the cleanest way to deal. */
103
91
unlink (sock_name );
104
92
105
93
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 );
108
95
sa_un .sun_family = AF_UNIX ;
109
96
110
97
debug (LOG_DEBUG , "Binding socket (%s) (%d)" , sa_un .sun_path , strlen (sock_name ));
111
98
112
99
/* 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 ;
116
103
}
117
104
118
- if (listen (wdctl_socket_server , 5 )) {
105
+ if (listen (sock , 5 )) {
119
106
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 ) {
120
134
termination_handler (0 );
121
135
}
122
136
@@ -251,9 +265,9 @@ wdctl_restart(int afd)
251
265
{
252
266
int sock , fd ;
253
267
char * sock_name ;
254
- struct sockaddr_un sa_un ;
255
268
s_config * conf = NULL ;
256
- t_client * client = NULL ;
269
+ struct sockaddr_un sa_un ;
270
+ t_client * client ;
257
271
char * tempstring = NULL ;
258
272
pid_t pid ;
259
273
socklen_t len ;
@@ -262,47 +276,13 @@ wdctl_restart(int afd)
262
276
263
277
debug (LOG_NOTICE , "Will restart myself" );
264
278
265
- /*
266
- * First, prepare the internal socket
267
- */
268
- memset (& sa_un , 0 , sizeof (sa_un ));
279
+ /* First, prepare the internal socket */
269
280
sock_name = conf -> internal_sock ;
270
281
debug (LOG_DEBUG , "Socket name: %s" , sock_name );
271
282
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
-
278
283
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 ) {
306
286
return ;
307
287
}
308
288
0 commit comments