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",
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
52 static int ipc_kill_token(struct ipc_header *ipc, server_child_t *children)
56 if (ipc->len != sizeof(pid_t)) {
59 /* assume signals SA_RESTART set */
60 memcpy (&pid, ipc->msg, sizeof(pid_t));
62 return server_child_transfer_session(children,
69 /* ----------------- */
70 static int ipc_get_session(struct ipc_header *ipc, server_child_t *children)
77 if (ipc->len < (sizeof(idlen) + sizeof(boottime)) )
81 memcpy (&idlen, p, sizeof(idlen));
82 idlen = ntohl (idlen);
85 memcpy (&boottime, p, sizeof(boottime));
86 p += sizeof(boottime);
88 if (ipc->len < idlen + sizeof(idlen) + sizeof(boottime))
91 if (NULL == (clientid = (char*) malloc(idlen)) )
93 memcpy (clientid, p, idlen);
95 LOG(log_debug, logtype_afpd, "ipc_get_session(pid: %u, uid: %u, time: 0x%08x)",
96 ipc->child_pid, ipc->uid, boottime);
98 server_child_kill_one_by_id(children,
108 static int ipc_set_state(struct ipc_header *ipc, server_child_t *children)
113 pthread_mutex_lock(&children->servch_lock);
115 if ((child = server_child_resolve(children, ipc->child_pid)) == NULL)
118 memcpy(&child->afpch_state, ipc->msg, sizeof(uint16_t));
121 pthread_mutex_unlock(&children->servch_lock);
125 static int ipc_set_volumes(struct ipc_header *ipc, server_child_t *children)
130 pthread_mutex_lock(&children->servch_lock);
132 if ((child = server_child_resolve(children, ipc->child_pid)) == NULL)
135 if (child->afpch_volumes) {
136 free(child->afpch_volumes);
137 child->afpch_volumes = NULL;
140 child->afpch_volumes = strdup(ipc->msg);
143 pthread_mutex_unlock(&children->servch_lock);
147 /***********************************************************************************
149 ***********************************************************************************/
160 * Read a IPC message from a child
162 * This is using an fd with non-blocking IO, so EAGAIN is not an error
164 * @args children (rw) pointer to our structure with all childs
165 * @args fd (r) IPC socket with child
167 * @returns -1 on error, 0 on success
169 int ipc_server_read(server_child_t *children, int fd)
172 struct ipc_header ipc;
173 char buf[IPC_MAXMSGSIZE], *p;
175 if ((ret = read(fd, buf, IPC_HEADERLEN)) != IPC_HEADERLEN) {
179 LOG(log_error, logtype_afpd, "Reading IPC header failed (%i of %u bytes read): %s",
180 ret, IPC_HEADERLEN, strerror(errno));
187 memcpy(&ipc.command, p, sizeof(ipc.command));
188 p += sizeof(ipc.command);
190 memcpy(&ipc.child_pid, p, sizeof(ipc.child_pid));
191 p += sizeof(ipc.child_pid);
193 memcpy(&ipc.uid, p, sizeof(ipc.uid));
194 p += sizeof(ipc.uid);
196 memcpy(&ipc.len, p, sizeof(ipc.len));
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);
204 memset (buf, 0, IPC_MAXMSGSIZE);
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));
214 LOG(log_debug, logtype_afpd, "ipc_server_read(%s): pid: %u",
215 ipc_cmd_str[ipc.command], ipc.child_pid);
217 switch (ipc.command) {
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));
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));
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",
234 kill(ipc.child_pid, SIGTERM);
236 close(ipc.afp_socket);
240 if (ipc_get_session(&ipc, children) != 0)
245 if (ipc_set_state(&ipc, children) != 0)
250 if (ipc_set_volumes(&ipc, children) != 0)
255 LOG (log_info, logtype_afpd, "ipc_read: unknown command: %d", ipc.command);
262 /* ----------------- */
263 int ipc_child_write(int fd, uint16_t command, int len, void *msg)
265 char block[IPC_MAXMSGSIZE], *p;
272 memset ( p, 0 , IPC_MAXMSGSIZE);
273 if (len + IPC_HEADERLEN > IPC_MAXMSGSIZE)
276 memcpy(p, &command, sizeof(command));
277 p += sizeof(command);
280 memcpy(p, &pid, sizeof(pid_t));
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.
289 memcpy(p, &uid, sizeof(uid_t));
297 LOG(log_debug, logtype_afpd, "ipc_child_write(%s)", ipc_cmd_str[command]);
299 if ((ret = writet(fd, block, len+IPC_HEADERLEN, 0, 1)) != len + IPC_HEADERLEN) {
306 int ipc_child_state(AFPObj *obj, uint16_t state)
308 return ipc_child_write(obj->ipc_fd, IPC_STATE, sizeof(uint16_t), &state);