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 *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,
69 /* ----------------- */
70 static int ipc_get_session(struct ipc_header *ipc, server_child *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,
109 /***********************************************************************************
111 ***********************************************************************************/
114 * Listen on UNIX domain socket "name" for IPC from old sesssion
116 * @args name (r) file name to use for UNIX domain socket
117 * @returns socket fd, -1 on error
119 int ipc_server_uds(const char *name)
122 struct sockaddr_un address;
123 socklen_t address_length;
126 EC_NEG1_LOG( fd = socket(PF_UNIX, SOCK_STREAM, 0) );
127 EC_ZERO_LOG( setnonblock(fd, 1) );
129 address.sun_family = AF_UNIX;
130 address_length = sizeof(address.sun_family) + sprintf(address.sun_path, "%s", name);
131 EC_ZERO_LOG( bind(fd, (struct sockaddr *)&address, address_length) );
132 EC_ZERO_LOG( listen(fd, 1024) );
143 * Connect to UNIX domain socket "name" for IPC with new afpd master
146 * 2. send pid, which establishes a child structure for us in the master
148 * @args name (r) file name to use for UNIX domain socket
149 * @returns socket fd, -1 on error
151 int ipc_client_uds(const char *name)
154 struct sockaddr_un address;
155 socklen_t address_length;
157 pid_t pid = getpid();
159 EC_NEG1_LOG( fd = socket(PF_UNIX, SOCK_STREAM, 0) );
160 address.sun_family = AF_UNIX;
161 address_length = sizeof(address.sun_family) + sprintf(address.sun_path, "%s", name);
163 EC_ZERO_LOG( connect(fd, (struct sockaddr *)&address, address_length) ); /* 1 */
164 LOG(log_debug, logtype_afpd, "ipc_client_uds: connected to master");
166 EC_ZERO_LOG( setnonblock(fd, 1) );
168 if (writet(fd, &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
169 LOG(log_error, logtype_afpd, "ipc_client_uds: writet: %s", strerror(errno));
177 LOG(log_debug, logtype_afpd, "ipc_client_uds: fd: %d", fd);
181 int reconnect_ipc(AFPObj *obj)
185 LOG(log_debug, logtype_afpd, "reconnect_ipc: start");
190 sleep((getpid() % 5) + 15); /* give it enough time to start */
192 while (retrycount++ < 10) {
193 if ((obj->ipc_fd = ipc_client_uds(_PATH_AFP_IPC)) == -1) {
194 LOG(log_error, logtype_afpd, "reconnect_ipc: cant reconnect to master");
198 LOG(log_debug, logtype_afpd, "reconnect_ipc: succesfull IPC reconnect");
213 * Read a IPC message from a child
215 * This is using an fd with non-blocking IO, so EAGAIN is not an error
217 * @args children (rw) pointer to our structure with all childs
218 * @args fd (r) IPC socket with child
220 * @returns -1 on error, 0 on success
222 int ipc_server_read(server_child *children, int fd)
225 struct ipc_header ipc;
226 char buf[IPC_MAXMSGSIZE], *p;
228 if ((ret = read(fd, buf, IPC_HEADERLEN)) != IPC_HEADERLEN) {
232 LOG(log_error, logtype_afpd, "Reading IPC header failed (%i of %u bytes read): %s",
233 ret, IPC_HEADERLEN, strerror(errno));
240 memcpy(&ipc.command, p, sizeof(ipc.command));
241 p += sizeof(ipc.command);
243 memcpy(&ipc.child_pid, p, sizeof(ipc.child_pid));
244 p += sizeof(ipc.child_pid);
246 memcpy(&ipc.uid, p, sizeof(ipc.uid));
247 p += sizeof(ipc.uid);
249 memcpy(&ipc.len, p, sizeof(ipc.len));
251 /* This should never happen */
252 if (ipc.len > (IPC_MAXMSGSIZE - IPC_HEADERLEN)) {
253 LOG (log_info, logtype_afpd, "IPC message exceeds allowed size (%u)", ipc.len);
257 memset (buf, 0, IPC_MAXMSGSIZE);
259 if ((ret = read(fd, buf, ipc.len)) != (int) ipc.len) {
260 LOG(log_info, logtype_afpd, "Reading IPC message failed (%u of %u bytes read): %s",
261 ret, ipc.len, strerror(errno));
267 LOG(log_debug, logtype_afpd, "ipc_server_read(%s): pid: %u",
268 ipc_cmd_str[ipc.command], ipc.child_pid);
270 switch (ipc.command) {
272 case IPC_DISCOLDSESSION:
273 if (readt(fd, &ipc.DSI_requestID, 2, 0, 2) != 2) {
274 LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): couldnt read DSI id: %s",
275 ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
278 if ((ipc.afp_socket = recv_fd(fd, 1)) == -1) {
279 LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): recv_fd: %s",
280 ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
283 if (ipc_kill_token(&ipc, children) == 1) {
284 /* Transfered session (ie afp_socket) to old disconnected child, now kill the new one */
285 LOG(log_note, logtype_afpd, "Reconnect: killing new session child[%u] after transfer",
287 kill(ipc.child_pid, SIGTERM);
289 close(ipc.afp_socket);
293 if (ipc_get_session(&ipc, children) != 0)
298 LOG (log_info, logtype_afpd, "ipc_read: unknown command: %d", ipc.command);
305 /* ----------------- */
306 int ipc_child_write(int fd, uint16_t command, int len, void *msg)
308 char block[IPC_MAXMSGSIZE], *p;
315 memset ( p, 0 , IPC_MAXMSGSIZE);
316 if (len + IPC_HEADERLEN > IPC_MAXMSGSIZE)
319 memcpy(p, &command, sizeof(command));
320 p += sizeof(command);
323 memcpy(p, &pid, sizeof(pid_t));
327 * using uid is wrong. It will not disconnect if the new connection
328 * is with a different user.
329 * But we really don't want a remote kill command.
332 memcpy(p, &uid, sizeof(uid_t));
340 LOG(log_debug, logtype_afpd, "ipc_child_write(%s)", ipc_cmd_str[command]);
342 if ((ret = writet(fd, block, len+IPC_HEADERLEN, 0, 1)) != len + IPC_HEADERLEN) {