2 * All rights reserved. See COPYRIGHT.
4 * IPC over socketpair between parent and children.
11 #include <sys/types.h>
16 #include <sys/socket.h>
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>
32 #define IPC_HEADERLEN 14
33 #define IPC_MAXMSGSIZE 90
35 typedef struct ipc_header {
42 uint16_t DSI_requestID;
45 static char *ipc_cmd_str[] = { "IPC_DISCOLDSESSION",
51 * Pass afp_socket to old disconnected session if one has a matching token (token = pid)
52 * @returns -1 on error, 0 if no matching session was found, 1 if session was found and socket passed
54 static int ipc_kill_token(struct ipc_header *ipc, server_child_t *children)
58 if (ipc->len != sizeof(pid_t)) {
61 /* assume signals SA_RESTART set */
62 memcpy (&pid, ipc->msg, sizeof(pid_t));
64 return server_child_transfer_session(children,
71 /* ----------------- */
72 static int ipc_get_session(struct ipc_header *ipc, server_child_t *children)
79 if (ipc->len < (sizeof(idlen) + sizeof(boottime)) )
83 memcpy (&idlen, p, sizeof(idlen));
84 idlen = ntohl (idlen);
87 memcpy (&boottime, p, sizeof(boottime));
88 p += sizeof(boottime);
90 if (ipc->len < idlen + sizeof(idlen) + sizeof(boottime))
93 if (NULL == (clientid = (char*) malloc(idlen)) )
95 memcpy (clientid, p, idlen);
97 LOG(log_debug, logtype_afpd, "ipc_get_session(pid: %u, uid: %u, time: 0x%08x)",
98 ipc->child_pid, ipc->uid, boottime);
100 server_child_kill_one_by_id(children,
110 static int ipc_set_state(struct ipc_header *ipc, server_child_t *children)
115 pthread_mutex_lock(&children->servch_lock);
117 if ((child = server_child_resolve(children, ipc->child_pid)) == NULL)
120 memcpy(&child->afpch_state, ipc->msg, sizeof(uint16_t));
123 pthread_mutex_unlock(&children->servch_lock);
127 static int ipc_set_volumes(struct ipc_header *ipc, server_child_t *children)
132 pthread_mutex_lock(&children->servch_lock);
134 if ((child = server_child_resolve(children, ipc->child_pid)) == NULL)
137 if (child->afpch_volumes) {
138 free(child->afpch_volumes);
139 child->afpch_volumes = NULL;
142 child->afpch_volumes = strdup(ipc->msg);
145 pthread_mutex_unlock(&children->servch_lock);
149 /***********************************************************************************
151 ***********************************************************************************/
162 * Read a IPC message from a child
164 * This is using an fd with non-blocking IO, so EAGAIN is not an error
166 * @args children (rw) pointer to our structure with all childs
167 * @args fd (r) IPC socket with child
169 * @returns -1 on error, 0 on success
171 int ipc_server_read(server_child_t *children, int fd)
174 struct ipc_header ipc;
175 char buf[IPC_MAXMSGSIZE], *p;
177 if ((ret = read(fd, buf, IPC_HEADERLEN)) != IPC_HEADERLEN) {
181 LOG(log_error, logtype_afpd, "Reading IPC header failed (%i of %u bytes read): %s",
182 ret, IPC_HEADERLEN, strerror(errno));
189 memcpy(&ipc.command, p, sizeof(ipc.command));
190 p += sizeof(ipc.command);
192 memcpy(&ipc.child_pid, p, sizeof(ipc.child_pid));
193 p += sizeof(ipc.child_pid);
195 memcpy(&ipc.uid, p, sizeof(ipc.uid));
196 p += sizeof(ipc.uid);
198 memcpy(&ipc.len, p, sizeof(ipc.len));
200 /* This should never happen */
201 if (ipc.len > (IPC_MAXMSGSIZE - IPC_HEADERLEN)) {
202 LOG (log_info, logtype_afpd, "IPC message exceeds allowed size (%u)", ipc.len);
206 memset (buf, 0, IPC_MAXMSGSIZE);
208 if ((ret = read(fd, buf, ipc.len)) != (int) ipc.len) {
209 LOG(log_info, logtype_afpd, "Reading IPC message failed (%u of %u bytes read): %s",
210 ret, ipc.len, strerror(errno));
216 LOG(log_debug, logtype_afpd, "ipc_server_read(%s): pid: %u",
217 ipc_cmd_str[ipc.command], ipc.child_pid);
219 switch (ipc.command) {
221 case IPC_DISCOLDSESSION:
222 if (readt(fd, &ipc.DSI_requestID, 2, 0, 2) != 2) {
223 LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): couldnt read DSI id: %s",
224 ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
227 if ((ipc.afp_socket = recv_fd(fd, 1)) == -1) {
228 LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): recv_fd: %s",
229 ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
232 if (ipc_kill_token(&ipc, children) == 1) {
233 /* Transfered session (ie afp_socket) to old disconnected child, now kill the new one */
234 LOG(log_note, logtype_afpd, "Reconnect: killing new session child[%u] after transfer",
236 kill(ipc.child_pid, SIGTERM);
238 close(ipc.afp_socket);
242 if (ipc_get_session(&ipc, children) != 0)
247 if (ipc_set_state(&ipc, children) != 0)
252 if (ipc_set_volumes(&ipc, children) != 0)
257 LOG (log_info, logtype_afpd, "ipc_read: unknown command: %d", ipc.command);
264 /* ----------------- */
265 int ipc_child_write(int fd, uint16_t command, int len, void *msg)
267 char block[IPC_MAXMSGSIZE], *p;
274 memset ( p, 0 , IPC_MAXMSGSIZE);
275 if (len + IPC_HEADERLEN > IPC_MAXMSGSIZE)
278 memcpy(p, &command, sizeof(command));
279 p += sizeof(command);
282 memcpy(p, &pid, sizeof(pid_t));
286 * using uid is wrong. It will not disconnect if the new connection
287 * is with a different user.
288 * But we really don't want a remote kill command.
291 memcpy(p, &uid, sizeof(uid_t));
299 LOG(log_debug, logtype_afpd, "ipc_child_write(%s)", ipc_cmd_str[command]);
301 if ((ret = writet(fd, block, len+IPC_HEADERLEN, 0, 1)) != len + IPC_HEADERLEN) {
308 int ipc_child_state(AFPObj *obj, uint16_t state)
310 return ipc_child_write(obj->ipc_fd, IPC_STATE, sizeof(uint16_t), &state);