2 * All rights reserved. See COPYRIGHT.
4 * IPC over socketpair between parent and children.
11 #include <sys/types.h>
16 #include <sys/socket.h>
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>
31 #define IPC_HEADERLEN 14
32 #define IPC_MAXMSGSIZE 90
34 typedef struct ipc_header {
41 uint16_t DSI_requestID;
44 static char *ipc_cmd_str[] = { "IPC_DISCOLDSESSION",
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
51 static int ipc_kill_token(struct ipc_header *ipc, server_child_t *children)
55 if (ipc->len != sizeof(pid_t)) {
58 /* assume signals SA_RESTART set */
59 memcpy (&pid, ipc->msg, sizeof(pid_t));
61 return server_child_transfer_session(children,
68 /* ----------------- */
69 static int ipc_get_session(struct ipc_header *ipc, server_child_t *children)
76 if (ipc->len < (sizeof(idlen) + sizeof(boottime)) )
80 memcpy (&idlen, p, sizeof(idlen));
81 idlen = ntohl (idlen);
84 memcpy (&boottime, p, sizeof(boottime));
85 p += sizeof(boottime);
87 if (ipc->len < idlen + sizeof(idlen) + sizeof(boottime))
90 if (NULL == (clientid = (char*) malloc(idlen)) )
92 memcpy (clientid, p, idlen);
94 LOG(log_debug, logtype_afpd, "ipc_get_session(pid: %u, uid: %u, time: 0x%08x)",
95 ipc->child_pid, ipc->uid, boottime);
97 server_child_kill_one_by_id(children,
107 /***********************************************************************************
109 ***********************************************************************************/
120 * Read a IPC message from a child
122 * This is using an fd with non-blocking IO, so EAGAIN is not an error
124 * @args children (rw) pointer to our structure with all childs
125 * @args fd (r) IPC socket with child
127 * @returns -1 on error, 0 on success
129 int ipc_server_read(server_child_t *children, int fd)
132 struct ipc_header ipc;
133 char buf[IPC_MAXMSGSIZE], *p;
135 if ((ret = read(fd, buf, IPC_HEADERLEN)) != IPC_HEADERLEN) {
139 LOG(log_error, logtype_afpd, "Reading IPC header failed (%i of %u bytes read): %s",
140 ret, IPC_HEADERLEN, strerror(errno));
147 memcpy(&ipc.command, p, sizeof(ipc.command));
148 p += sizeof(ipc.command);
150 memcpy(&ipc.child_pid, p, sizeof(ipc.child_pid));
151 p += sizeof(ipc.child_pid);
153 memcpy(&ipc.uid, p, sizeof(ipc.uid));
154 p += sizeof(ipc.uid);
156 memcpy(&ipc.len, p, sizeof(ipc.len));
158 /* This should never happen */
159 if (ipc.len > (IPC_MAXMSGSIZE - IPC_HEADERLEN)) {
160 LOG (log_info, logtype_afpd, "IPC message exceeds allowed size (%u)", ipc.len);
164 memset (buf, 0, IPC_MAXMSGSIZE);
166 if ((ret = read(fd, buf, ipc.len)) != (int) ipc.len) {
167 LOG(log_info, logtype_afpd, "Reading IPC message failed (%u of %u bytes read): %s",
168 ret, ipc.len, strerror(errno));
174 LOG(log_debug, logtype_afpd, "ipc_server_read(%s): pid: %u",
175 ipc_cmd_str[ipc.command], ipc.child_pid);
177 switch (ipc.command) {
179 case IPC_DISCOLDSESSION:
180 if (readt(fd, &ipc.DSI_requestID, 2, 0, 2) != 2) {
181 LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): couldnt read DSI id: %s",
182 ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
185 if ((ipc.afp_socket = recv_fd(fd, 1)) == -1) {
186 LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): recv_fd: %s",
187 ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
190 if (ipc_kill_token(&ipc, children) == 1) {
191 /* Transfered session (ie afp_socket) to old disconnected child, now kill the new one */
192 LOG(log_note, logtype_afpd, "Reconnect: killing new session child[%u] after transfer",
194 kill(ipc.child_pid, SIGTERM);
196 close(ipc.afp_socket);
200 if (ipc_get_session(&ipc, children) != 0)
205 LOG (log_info, logtype_afpd, "ipc_read: unknown command: %d", ipc.command);
212 /* ----------------- */
213 int ipc_child_write(int fd, uint16_t command, int len, void *msg)
215 char block[IPC_MAXMSGSIZE], *p;
222 memset ( p, 0 , IPC_MAXMSGSIZE);
223 if (len + IPC_HEADERLEN > IPC_MAXMSGSIZE)
226 memcpy(p, &command, sizeof(command));
227 p += sizeof(command);
230 memcpy(p, &pid, sizeof(pid_t));
234 * using uid is wrong. It will not disconnect if the new connection
235 * is with a different user.
236 * But we really don't want a remote kill command.
239 memcpy(p, &uid, sizeof(uid_t));
247 LOG(log_debug, logtype_afpd, "ipc_child_write(%s)", ipc_cmd_str[command]);
249 if ((ret = writet(fd, block, len+IPC_HEADERLEN, 0, 1)) != len + IPC_HEADERLEN) {