]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/server_ipc.c
AFP statistics via dbus IPC
[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 #include <pthread.h>
22
23 #include <atalk/server_child.h>
24 #include <atalk/server_ipc.h>
25 #include <atalk/logger.h>
26 #include <atalk/util.h>
27 #include <atalk/errchk.h>
28 #include <atalk/paths.h>
29 #include <atalk/globals.h>
30 #include <atalk/dsi.h>
31
32 #define IPC_HEADERLEN 14
33 #define IPC_MAXMSGSIZE 90
34
35 typedef struct ipc_header {
36         uint16_t command;
37     pid_t        child_pid;
38     uid_t    uid;
39     uint32_t len;
40         char     *msg;
41     int      afp_socket;
42     uint16_t DSI_requestID;
43 } ipc_header_t;
44
45 static char *ipc_cmd_str[] = { "IPC_DISCOLDSESSION",
46                                "IPC_GETSESSION"};
47
48 /*
49  * Pass afp_socket to old disconnected session if one has a matching token (token = pid)
50  * @returns -1 on error, 0 if no matching session was found, 1 if session was found and socket passed
51  */
52 static int ipc_kill_token(struct ipc_header *ipc, server_child_t *children)
53 {
54     pid_t pid;
55
56     if (ipc->len != sizeof(pid_t)) {
57         return -1;
58     }
59     /* assume signals SA_RESTART set */
60     memcpy (&pid, ipc->msg, sizeof(pid_t));
61
62     return server_child_transfer_session(children,
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_t *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                                 ipc->child_pid,
100                                 ipc->uid,
101                                 idlen,
102                                 clientid,
103                                 boottime);
104
105     return 0;
106 }
107
108 static int ipc_set_state(struct ipc_header *ipc, server_child_t *children)
109 {
110     EC_INIT;
111     afp_child_t *child;
112
113     pthread_mutex_lock(&children->servch_lock);
114
115     if ((child = server_child_resolve(children, ipc->child_pid)) == NULL)
116         EC_FAIL;
117
118     memcpy(&child->afpch_state, ipc->msg, sizeof(uint16_t));
119
120 EC_CLEANUP:
121     pthread_mutex_unlock(&children->servch_lock);
122     EC_EXIT;
123 }
124
125 static int ipc_set_volumes(struct ipc_header *ipc, server_child_t *children)
126 {
127     EC_INIT;
128     afp_child_t *child;
129
130     pthread_mutex_lock(&children->servch_lock);
131
132     if ((child = server_child_resolve(children, ipc->child_pid)) == NULL)
133         EC_FAIL;
134
135     if (child->afpch_volumes) {
136         free(child->afpch_volumes);
137         child->afpch_volumes = NULL;
138     }
139     if (ipc->len)
140         child->afpch_volumes = strdup(ipc->msg);
141
142 EC_CLEANUP:
143     pthread_mutex_unlock(&children->servch_lock);
144     EC_EXIT;
145 }
146
147 /***********************************************************************************
148  * Public functions
149  ***********************************************************************************/
150
151 /* ----------------- 
152  * Ipc format
153  * command
154  * pid
155  * uid
156  * 
157  */
158
159 /*!
160  * Read a IPC message from a child
161  *
162  * This is using an fd with non-blocking IO, so EAGAIN is not an error
163  *
164  * @args children  (rw) pointer to our structure with all childs
165  * @args fd        (r)  IPC socket with child
166  *
167  * @returns -1 on error, 0 on success
168  */
169 int ipc_server_read(server_child_t *children, int fd)
170 {
171     int       ret;
172     struct ipc_header ipc;
173     char      buf[IPC_MAXMSGSIZE], *p;
174
175     if ((ret = read(fd, buf, IPC_HEADERLEN)) != IPC_HEADERLEN) {
176         if (ret != 0) {
177             if (errno == EAGAIN)
178                 return 0;
179             LOG(log_error, logtype_afpd, "Reading IPC header failed (%i of %u bytes read): %s",
180                 ret, IPC_HEADERLEN, strerror(errno));
181         }
182         return -1;
183     }
184
185     p = buf;
186
187     memcpy(&ipc.command, p, sizeof(ipc.command));
188     p += sizeof(ipc.command);
189
190     memcpy(&ipc.child_pid, p, sizeof(ipc.child_pid));
191     p += sizeof(ipc.child_pid);
192
193     memcpy(&ipc.uid, p, sizeof(ipc.uid));
194     p += sizeof(ipc.uid);
195
196     memcpy(&ipc.len, p, sizeof(ipc.len));
197
198     /* This should never happen */
199     if (ipc.len > (IPC_MAXMSGSIZE - IPC_HEADERLEN)) {
200         LOG (log_info, logtype_afpd, "IPC message exceeds allowed size (%u)", ipc.len);
201         return -1;
202     }
203
204     memset (buf, 0, IPC_MAXMSGSIZE);
205     if ( ipc.len != 0) {
206             if ((ret = read(fd, buf, ipc.len)) != (int) ipc.len) {
207             LOG(log_info, logtype_afpd, "Reading IPC message failed (%u of %u  bytes read): %s",
208                 ret, ipc.len, strerror(errno));
209             return -1;
210         }        
211     }
212     ipc.msg = buf;
213
214     LOG(log_debug, logtype_afpd, "ipc_server_read(%s): pid: %u",
215         ipc_cmd_str[ipc.command], ipc.child_pid); 
216
217     switch (ipc.command) {
218
219         case IPC_DISCOLDSESSION:
220         if (readt(fd, &ipc.DSI_requestID, 2, 0, 2) != 2) {
221             LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): couldnt read DSI id: %s",
222                  ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
223             return -1;
224         }
225         if ((ipc.afp_socket = recv_fd(fd, 1)) == -1) {
226             LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): recv_fd: %s",
227                  ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
228             return -1;
229         }
230                 if (ipc_kill_token(&ipc, children) == 1) {
231             /* Transfered session (ie afp_socket) to old disconnected child, now kill the new one */
232             LOG(log_note, logtype_afpd, "Reconnect: killing new session child[%u] after transfer",
233                 ipc.child_pid);
234             kill(ipc.child_pid, SIGTERM);
235         }        
236         close(ipc.afp_socket);
237         break;
238
239         case IPC_GETSESSION:
240                 if (ipc_get_session(&ipc, children) != 0)
241             return -1;
242         break;
243
244     case IPC_STATE:
245         if (ipc_set_state(&ipc, children) != 0)
246             return -1;
247         break;
248
249     case IPC_VOLUMES:
250         if (ipc_set_volumes(&ipc, children) != 0)
251             return -1;
252         break;
253
254         default:
255                 LOG (log_info, logtype_afpd, "ipc_read: unknown command: %d", ipc.command);
256                 return -1;
257     }
258
259     return 0;
260 }
261
262 /* ----------------- */
263 int ipc_child_write(int fd, uint16_t command, int len, void *msg)
264 {
265    char block[IPC_MAXMSGSIZE], *p;
266    pid_t pid;
267    uid_t uid;
268    ssize_t ret;
269
270    p = block;
271
272    memset ( p, 0 , IPC_MAXMSGSIZE);
273    if (len + IPC_HEADERLEN > IPC_MAXMSGSIZE)
274        return -1;
275
276    memcpy(p, &command, sizeof(command));
277    p   += sizeof(command);
278
279    pid = getpid();
280    memcpy(p, &pid, sizeof(pid_t));
281    p += sizeof(pid_t);
282    
283    /* FIXME 
284     * using uid is wrong. It will not disconnect if the new connection
285     * is with a different user. 
286     * But we really don't want a remote kill command.
287    */
288    uid = geteuid();
289    memcpy(p, &uid, sizeof(uid_t));
290    p += sizeof(uid_t);
291
292    memcpy(p, &len, 4);
293    p += 4;
294
295    memcpy(p, msg, len);
296
297    LOG(log_debug, logtype_afpd, "ipc_child_write(%s)", ipc_cmd_str[command]);
298
299    if ((ret = writet(fd, block, len+IPC_HEADERLEN, 0, 1)) != len + IPC_HEADERLEN) {
300        return -1;
301    }
302
303    return 0;
304 }
305
306 int ipc_child_state(AFPObj *obj, uint16_t state)
307 {
308     return ipc_child_write(obj->ipc_fd, IPC_STATE, sizeof(uint16_t), &state);
309 }