2 * All rights reserved. See COPYRIGHT.
4 * IPC over socketpair between parent and children.
11 #include <sys/types.h>
18 #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>
30 #define IPC_HEADERLEN 14
31 #define IPC_MAXMSGSIZE 90
33 typedef struct ipc_header {
40 uint16_t DSI_requestID;
43 static char *ipc_cmd_str[] = { "IPC_DISCOLDSESSION",
47 * Pass afp_socket to old disconnected session if one has a matching token (token = pid)
48 * @returns -1 on error, 0 if no matching session was found, 1 if session was found and socket passed
50 static int ipc_kill_token(struct ipc_header *ipc, server_child *children)
54 if (ipc->len != sizeof(pid_t)) {
57 /* assume signals SA_RESTART set */
58 memcpy (&pid, ipc->msg, sizeof(pid_t));
60 return server_child_transfer_session(children,
68 /* ----------------- */
69 static int ipc_get_session(struct ipc_header *ipc, server_child *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,
108 /***********************************************************************************
110 ***********************************************************************************/
113 * Listen on UNIX domain socket "name" for IPC from old sesssion
115 * @args name (r) file name to use for UNIX domain socket
116 * @returns socket fd, -1 on error
118 int ipc_server_uds(const char *name)
121 struct sockaddr_un address;
122 socklen_t address_length;
125 EC_NEG1_LOG( fd = socket(PF_UNIX, SOCK_STREAM, 0) );
126 EC_ZERO_LOG( setnonblock(fd, 1) );
128 address.sun_family = AF_UNIX;
129 address_length = sizeof(address.sun_family) + sprintf(address.sun_path, name);
130 EC_ZERO_LOG( bind(fd, (struct sockaddr *)&address, address_length) );
131 EC_ZERO_LOG( listen(fd, 1024) );
137 LOG(log_note, logtype_afpd, "ipc_server_uds: fd: %d", fd);
142 * Connect to UNIX domain socket "name" for IPC with new afpd master
145 * 2. send pid, which establishes a child structure for us in the master
147 * @args name (r) file name to use for UNIX domain socket
148 * @returns socket fd, -1 on error
150 int ipc_client_uds(const char *name)
153 struct sockaddr_un address;
154 socklen_t address_length;
156 pid_t pid = getpid();
158 EC_NEG1_LOG( fd = socket(PF_UNIX, SOCK_STREAM, 0) );
159 EC_ZERO_LOG( setnonblock(fd, 1) );
160 address.sun_family = AF_UNIX;
161 address_length = sizeof(address.sun_family) + sprintf(address.sun_path, name);
163 EC_ZERO_LOG( connect(fd, (struct sockaddr *)&address, address_length) ); /* 1 */
165 if (writet(fd, &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
166 LOG(log_error, logtype_afpd, "ipc_client_uds: writet: %s", strerror(errno));
174 LOG(log_note, logtype_afpd, "ipc_client_uds: fd: %d", fd);
187 * Read a IPC message from a child
189 * @args children (rw) pointer to our structure with all childs
190 * @args fd (r) IPC socket with child
192 * @returns number of bytes transfered, -1 on error, 0 on EOF
194 int ipc_server_read(server_child *children, int fd)
197 struct ipc_header ipc;
198 char buf[IPC_MAXMSGSIZE], *p;
200 if ((ret = read(fd, buf, IPC_HEADERLEN)) != IPC_HEADERLEN) {
201 LOG(log_error, logtype_afpd, "Reading IPC header failed (%i of %u bytes read): %s",
202 ret, IPC_HEADERLEN, strerror(errno));
208 memcpy(&ipc.command, p, sizeof(ipc.command));
209 p += sizeof(ipc.command);
211 memcpy(&ipc.child_pid, p, sizeof(ipc.child_pid));
212 p += sizeof(ipc.child_pid);
214 memcpy(&ipc.uid, p, sizeof(ipc.uid));
215 p += sizeof(ipc.uid);
217 memcpy(&ipc.len, p, sizeof(ipc.len));
219 /* This should never happen */
220 if (ipc.len > (IPC_MAXMSGSIZE - IPC_HEADERLEN)) {
221 LOG (log_info, logtype_afpd, "IPC message exceeds allowed size (%u)", ipc.len);
225 memset (buf, 0, IPC_MAXMSGSIZE);
227 if ((ret = read(fd, buf, ipc.len)) != (int) ipc.len) {
228 LOG(log_info, logtype_afpd, "Reading IPC message failed (%u of %u bytes read): %s",
229 ret, ipc.len, strerror(errno));
235 LOG(log_debug, logtype_afpd, "ipc_server_read(%s): pid: %u",
236 ipc_cmd_str[ipc.command], ipc.child_pid);
240 switch (ipc.command) {
242 case IPC_DISCOLDSESSION:
243 if (readt(fd, &ipc.DSI_requestID, 2, 0, 2) != 2) {
244 LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): couldnt read DSI id: %s",
245 ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
247 if ((ipc.afp_socket = recv_fd(fd, 1)) == -1) {
248 LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): recv_fd: %s",
249 ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
252 if (ipc_kill_token(&ipc, children) == 1) {
253 /* Transfered session (ie afp_socket) to old disconnected child, now kill the new one */
254 LOG(log_note, logtype_afpd, "Reconnect: killing new session child[%u] after transfer",
256 kill(ipc.child_pid, SIGTERM);
258 close(ipc.afp_socket);
262 if (ipc_get_session(&ipc, children) != 0)
267 LOG (log_info, logtype_afpd, "ipc_read: unknown command: %d", ipc.command);
274 /* ----------------- */
275 ssize_t ipc_child_write(int *fd, uint16_t command, int len, void *msg)
277 static int fd_saved = -1;
278 char block[IPC_MAXMSGSIZE], *p;
288 memset ( p, 0 , IPC_MAXMSGSIZE);
289 if (len + IPC_HEADERLEN > IPC_MAXMSGSIZE)
292 memcpy(p, &command, sizeof(command));
293 p += sizeof(command);
296 memcpy(p, &pid, sizeof(pid_t));
300 * using uid is wrong. It will not disconnect if the new connection
301 * is with a different user.
302 * But we really don't want a remote kill command.
305 memcpy(p, &uid, sizeof(uid_t));
313 LOG(log_debug, logtype_afpd, "ipc_child_write(%s)", ipc_cmd_str[command]);
315 if ((ret = writet(*fd, block, len+IPC_HEADERLEN, 0, 1)) == -1) {
316 if (*fd == fd_saved && getppid() == 1) {
317 /* still using original socketpair IPC fd, master was possibly restarted, try reestablish connection across uds */
318 if ((*fd = ipc_client_uds(_PATH_AFP_IPC)) == -1)
321 ret = writet(*fd, block, len+IPC_HEADERLEN, 0, 1);