2 * All rights reserved. See COPYRIGHT.
4 * IPC over socketpair between parent and children.
11 #include <sys/types.h>
16 #include <sys/socket.h>
20 #include <atalk/server_child.h>
21 #include <atalk/server_ipc.h>
22 #include <atalk/logger.h>
23 #include <atalk/util.h>
25 typedef struct ipc_header {
32 uint16_t DSI_requestID;
35 static char *ipc_cmd_str[] = { "IPC_DISCOLDSESSION",
39 * Pass afp_socket to old disconnected session if one has a matching token (token = pid)
40 * @returns -1 on error, 0 if no matching session was found, 1 if session was found and socket passed
42 static int ipc_kill_token(struct ipc_header *ipc, server_child *children)
46 if (ipc->len != sizeof(pid_t)) {
49 /* assume signals SA_RESTART set */
50 memcpy (&pid, ipc->msg, sizeof(pid_t));
52 return server_child_transfer_session(children,
60 /* ----------------- */
61 static int ipc_get_session(struct ipc_header *ipc, server_child *children)
68 if (ipc->len < (sizeof(idlen) + sizeof(boottime)) )
72 memcpy (&idlen, p, sizeof(idlen));
73 idlen = ntohl (idlen);
76 memcpy (&boottime, p, sizeof(boottime));
77 p += sizeof(boottime);
79 if (ipc->len < idlen + sizeof(idlen) + sizeof(boottime))
82 if (NULL == (clientid = (char*) malloc(idlen)) )
84 memcpy (clientid, p, idlen);
86 LOG(log_debug, logtype_afpd, "ipc_get_session(pid: %u, uid: %u, time: 0x%08x)",
87 ipc->child_pid, ipc->uid, boottime);
89 server_child_kill_one_by_id(children,
100 #define IPC_HEADERLEN 14
101 #define IPC_MAXMSGSIZE 90
112 * Read a IPC message from a child
114 * @args children (rw) pointer to our structure with all childs
115 * @args fd (r) IPC socket with child
117 * @returns number of bytes transfered, -1 on error, 0 on EOF
119 int ipc_server_read(server_child *children, int fd)
122 struct ipc_header ipc;
123 char buf[IPC_MAXMSGSIZE], *p;
125 if ((ret = read(fd, buf, IPC_HEADERLEN)) != IPC_HEADERLEN) {
126 LOG(log_error, logtype_afpd, "Reading IPC header failed (%i of %u bytes read): %s",
127 ret, IPC_HEADERLEN, strerror(errno));
133 memcpy(&ipc.command, p, sizeof(ipc.command));
134 p += sizeof(ipc.command);
136 memcpy(&ipc.child_pid, p, sizeof(ipc.child_pid));
137 p += sizeof(ipc.child_pid);
139 memcpy(&ipc.uid, p, sizeof(ipc.uid));
140 p += sizeof(ipc.uid);
142 memcpy(&ipc.len, p, sizeof(ipc.len));
144 /* This should never happen */
145 if (ipc.len > (IPC_MAXMSGSIZE - IPC_HEADERLEN)) {
146 LOG (log_info, logtype_afpd, "IPC message exceeds allowed size (%u)", ipc.len);
150 memset (buf, 0, IPC_MAXMSGSIZE);
152 if ((ret = read(fd, buf, ipc.len)) != (int) ipc.len) {
153 LOG(log_info, logtype_afpd, "Reading IPC message failed (%u of %u bytes read): %s",
154 ret, ipc.len, strerror(errno));
160 LOG(log_debug, logtype_afpd, "ipc_server_read(%s): pid: %u",
161 ipc_cmd_str[ipc.command], ipc.child_pid);
165 switch (ipc.command) {
167 case IPC_DISCOLDSESSION:
168 if (readt(fd, &ipc.DSI_requestID, 2, 0, 2) != 2) {
169 LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): couldnt read DSI id: %s",
170 ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
172 if ((ipc.afp_socket = recv_fd(fd, 1)) == -1) {
173 LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): recv_fd: %s",
174 ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
177 if (ipc_kill_token(&ipc, children) == 1) {
178 /* Transfered session (ie afp_socket) to old disconnected child, now kill the new one */
179 LOG(log_note, logtype_afpd, "Reconnect: killing new session child[%u] after transfer",
181 kill(ipc.child_pid, SIGTERM);
183 close(ipc.afp_socket);
187 if (ipc_get_session(&ipc, children) != 0)
192 LOG (log_info, logtype_afpd, "ipc_read: unknown command: %d", ipc.command);
199 /* ----------------- */
200 int ipc_child_write(int fd, uint16_t command, int len, void *msg)
202 char block[IPC_MAXMSGSIZE], *p;
207 memset ( p, 0 , IPC_MAXMSGSIZE);
208 if (len + IPC_HEADERLEN > IPC_MAXMSGSIZE)
211 memcpy(p, &command, sizeof(command));
212 p += sizeof(command);
215 memcpy(p, &pid, sizeof(pid_t));
219 * using uid is wrong. It will not disconnect if the new connection
220 * is with a different user.
221 * But we really don't want a remote kill command.
224 memcpy(p, &uid, sizeof(uid_t));
232 LOG(log_debug, logtype_afpd, "ipc_child_write(%s)", ipc_cmd_str[command]);
234 return write(fd, block, len+IPC_HEADERLEN );