]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/server_ipc.c
Merge 2-2
[netatalk.git] / libatalk / util / server_ipc.c
1 /*
2  * All rights reserved. See COPYRIGHT.
3  *
4  * IPC over socketpair between parent and children.
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif 
10
11 #include <sys/types.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/socket.h>
17 #include <sys/un.h>
18 #include <errno.h>
19 #include <signal.h>
20 #include <time.h>
21
22 #include <atalk/server_child.h>
23 #include <atalk/server_ipc.h>
24 #include <atalk/logger.h>
25 #include <atalk/util.h>
26 #include <atalk/errchk.h>
27 #include <atalk/paths.h>
28 #include <atalk/globals.h>
29 #include <atalk/dsi.h>
30
31 #define IPC_HEADERLEN 14
32 #define IPC_MAXMSGSIZE 90
33
34 typedef struct ipc_header {
35         uint16_t command;
36     pid_t        child_pid;
37     uid_t    uid;
38     uint32_t len;
39         char     *msg;
40     int      afp_socket;
41     uint16_t DSI_requestID;
42 } ipc_header_t;
43
44 static char *ipc_cmd_str[] = { "IPC_DISCOLDSESSION",
45                                "IPC_GETSESSION"};
46
47 /*
48  * Pass afp_socket to old disconnected session if one has a matching token (token = pid)
49  * @returns -1 on error, 0 if no matching session was found, 1 if session was found and socket passed
50  */
51 static int ipc_kill_token(struct ipc_header *ipc, server_child *children)
52 {
53     pid_t pid;
54
55     if (ipc->len != sizeof(pid_t)) {
56         return -1;
57     }
58     /* assume signals SA_RESTART set */
59     memcpy (&pid, ipc->msg, sizeof(pid_t));
60
61     return server_child_transfer_session(children,
62                                          CHILD_DSIFORK,
63                                          pid,
64                                          ipc->uid,
65                                          ipc->afp_socket,
66                                          ipc->DSI_requestID);
67 }
68
69 /* ----------------- */
70 static int ipc_get_session(struct ipc_header *ipc, server_child *children)
71 {
72     uint32_t boottime;
73     uint32_t idlen;
74     char     *clientid, *p;
75
76     
77     if (ipc->len < (sizeof(idlen) + sizeof(boottime)) )
78         return -1;
79
80     p = ipc->msg;
81     memcpy (&idlen, p, sizeof(idlen));
82     idlen = ntohl (idlen);
83     p += sizeof(idlen); 
84
85     memcpy (&boottime, p, sizeof(boottime));
86     p += sizeof(boottime);
87     
88     if (ipc->len < idlen + sizeof(idlen) + sizeof(boottime))
89         return -1;
90
91     if (NULL == (clientid = (char*) malloc(idlen)) )
92         return -1;
93     memcpy (clientid, p, idlen);
94   
95     LOG(log_debug, logtype_afpd, "ipc_get_session(pid: %u, uid: %u, time: 0x%08x)",
96         ipc->child_pid, ipc->uid, boottime); 
97
98     server_child_kill_one_by_id(children,
99                                 CHILD_DSIFORK,
100                                 ipc->child_pid,
101                                 ipc->uid,
102                                 idlen,
103                                 clientid,
104                                 boottime);
105
106     return 0;
107 }
108
109 /***********************************************************************************
110  * Public functions
111  ***********************************************************************************/
112
113 /*!
114  * Listen on UNIX domain socket "name" for IPC from old sesssion
115  *
116  * @args name    (r) file name to use for UNIX domain socket
117  * @returns      socket fd, -1 on error
118  */
119 int ipc_server_uds(const char *name)
120 {
121     EC_INIT;
122     struct sockaddr_un address;
123     socklen_t address_length;
124     int fd = -1;
125
126     EC_NEG1_LOG( fd = socket(PF_UNIX, SOCK_STREAM, 0) );
127     EC_ZERO_LOG( setnonblock(fd, 1) );
128     unlink(name);
129     address.sun_family = AF_UNIX;
130     address_length = sizeof(address.sun_family) + sprintf(address.sun_path, "%s", name);
131     EC_ZERO_LOG( bind(fd, (struct sockaddr *)&address, address_length) );
132     EC_ZERO_LOG( listen(fd, 1024) );
133
134 EC_CLEANUP:
135     if (ret != 0) {
136         return -1;
137     }
138
139     return fd;
140 }
141
142 /*!
143  * Connect to UNIX domain socket "name" for IPC with new afpd master
144  *
145  * 1. Connect
146  * 2. send pid, which establishes a child structure for us in the master
147  *
148  * @args name    (r) file name to use for UNIX domain socket
149  * @returns      socket fd, -1 on error
150  */
151 int ipc_client_uds(const char *name)
152 {
153     EC_INIT;
154     struct sockaddr_un address;
155     socklen_t address_length;
156     int fd = -1;
157     pid_t pid = getpid();
158
159     EC_NEG1_LOG( fd = socket(PF_UNIX, SOCK_STREAM, 0) );
160     address.sun_family = AF_UNIX;
161     address_length = sizeof(address.sun_family) + sprintf(address.sun_path, "%s", name);
162
163     EC_ZERO_LOG( connect(fd, (struct sockaddr *)&address, address_length) ); /* 1 */
164     LOG(log_debug, logtype_afpd, "ipc_client_uds: connected to master");
165
166     EC_ZERO_LOG( setnonblock(fd, 1) );
167
168     if (writet(fd, &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
169         LOG(log_error, logtype_afpd, "ipc_client_uds: writet: %s", strerror(errno));
170         EC_FAIL;
171     }
172
173 EC_CLEANUP:
174     if (ret != 0) {
175         return -1;
176     }
177     LOG(log_debug, logtype_afpd, "ipc_client_uds: fd: %d", fd);
178     return fd;
179 }
180
181 int reconnect_ipc(AFPObj *obj)
182 {
183     int retrycount = 0;
184
185     LOG(log_debug, logtype_afpd, "reconnect_ipc: start");
186
187     close(obj->ipc_fd);
188     obj->ipc_fd = -1;
189
190     sleep((getpid() % 5) + 15);  /* give it enough time to start */
191
192     while (retrycount++ < 10) {
193         if ((obj->ipc_fd = ipc_client_uds(_PATH_AFP_IPC)) == -1) {
194             LOG(log_error, logtype_afpd, "reconnect_ipc: cant reconnect to master");
195             sleep(1);
196             continue;
197         }
198         LOG(log_debug, logtype_afpd, "reconnect_ipc: succesfull IPC reconnect");
199         return 0;
200     }
201     return -1;
202 }
203
204 /* ----------------- 
205  * Ipc format
206  * command
207  * pid
208  * uid
209  * 
210  */
211
212 /*!
213  * Read a IPC message from a child
214  *
215  * This is using an fd with non-blocking IO, so EAGAIN is not an error
216  *
217  * @args children  (rw) pointer to our structure with all childs
218  * @args fd        (r)  IPC socket with child
219  *
220  * @returns -1 on error, 0 on success
221  */
222 int ipc_server_read(server_child *children, int fd)
223 {
224     int       ret;
225     struct ipc_header ipc;
226     char      buf[IPC_MAXMSGSIZE], *p;
227
228     if ((ret = read(fd, buf, IPC_HEADERLEN)) != IPC_HEADERLEN) {
229         if (ret != 0) {
230             if (errno == EAGAIN)
231                 return 0;
232             LOG(log_error, logtype_afpd, "Reading IPC header failed (%i of %u bytes read): %s",
233                 ret, IPC_HEADERLEN, strerror(errno));
234         }
235         return -1;
236     }
237
238     p = buf;
239
240     memcpy(&ipc.command, p, sizeof(ipc.command));
241     p += sizeof(ipc.command);
242
243     memcpy(&ipc.child_pid, p, sizeof(ipc.child_pid));
244     p += sizeof(ipc.child_pid);
245
246     memcpy(&ipc.uid, p, sizeof(ipc.uid));
247     p += sizeof(ipc.uid);
248
249     memcpy(&ipc.len, p, sizeof(ipc.len));
250
251     /* This should never happen */
252     if (ipc.len > (IPC_MAXMSGSIZE - IPC_HEADERLEN)) {
253         LOG (log_info, logtype_afpd, "IPC message exceeds allowed size (%u)", ipc.len);
254         return -1;
255     }
256
257     memset (buf, 0, IPC_MAXMSGSIZE);
258     if ( ipc.len != 0) {
259             if ((ret = read(fd, buf, ipc.len)) != (int) ipc.len) {
260             LOG(log_info, logtype_afpd, "Reading IPC message failed (%u of %u  bytes read): %s",
261                 ret, ipc.len, strerror(errno));
262             return -1;
263         }        
264     }
265     ipc.msg = buf;
266
267     LOG(log_debug, logtype_afpd, "ipc_server_read(%s): pid: %u",
268         ipc_cmd_str[ipc.command], ipc.child_pid); 
269
270     int afp_socket;
271
272     switch (ipc.command) {
273
274         case IPC_DISCOLDSESSION:
275         if (readt(fd, &ipc.DSI_requestID, 2, 0, 2) != 2) {
276             LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): couldnt read DSI id: %s",
277                  ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
278             return -1;
279         }
280         if ((ipc.afp_socket = recv_fd(fd, 1)) == -1) {
281             LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): recv_fd: %s",
282                  ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
283             return -1;
284         }
285                 if (ipc_kill_token(&ipc, children) == 1) {
286             /* Transfered session (ie afp_socket) to old disconnected child, now kill the new one */
287             LOG(log_note, logtype_afpd, "Reconnect: killing new session child[%u] after transfer",
288                 ipc.child_pid);
289             kill(ipc.child_pid, SIGTERM);
290         }        
291         close(ipc.afp_socket);
292         break;
293
294         case IPC_GETSESSION:
295                 if (ipc_get_session(&ipc, children) != 0)
296             return -1;
297         break;
298
299         default:
300                 LOG (log_info, logtype_afpd, "ipc_read: unknown command: %d", ipc.command);
301                 return -1;
302     }
303
304     return 0;
305 }
306
307 /* ----------------- */
308 int ipc_child_write(int fd, uint16_t command, int len, void *msg)
309 {
310    char block[IPC_MAXMSGSIZE], *p;
311    pid_t pid;
312    uid_t uid;
313    ssize_t ret;
314
315    p = block;
316
317    memset ( p, 0 , IPC_MAXMSGSIZE);
318    if (len + IPC_HEADERLEN > IPC_MAXMSGSIZE)
319        return -1;
320
321    memcpy(p, &command, sizeof(command));
322    p   += sizeof(command);
323
324    pid = getpid();
325    memcpy(p, &pid, sizeof(pid_t));
326    p += sizeof(pid_t);
327    
328    /* FIXME 
329     * using uid is wrong. It will not disconnect if the new connection
330     * is with a different user. 
331     * But we really don't want a remote kill command.
332    */
333    uid = geteuid();
334    memcpy(p, &uid, sizeof(uid_t));
335    p += sizeof(uid_t);
336
337    memcpy(p, &len, 4);
338    p += 4;
339
340    memcpy(p, msg, len);
341
342    LOG(log_debug, logtype_afpd, "ipc_child_write(%s)", ipc_cmd_str[command]);
343
344    if ((ret = writet(fd, block, len+IPC_HEADERLEN, 0, 1)) != len + IPC_HEADERLEN) {
345        return -1;
346    }
347
348    return 0;
349 }