2 * All rights reserved. See COPYRIGHT.
4 * IPC over socketpair between parent and children.
11 #include <sys/types.h>
18 #include <sys/socket.h>
24 #include <atalk/server_child.h>
25 #include <atalk/server_ipc.h>
26 #include <atalk/logger.h>
27 #include <atalk/util.h>
28 #include <atalk/errchk.h>
29 #include <atalk/paths.h>
30 #include <atalk/globals.h>
31 #include <atalk/dsi.h>
33 #define IPC_HEADERLEN 14
34 #define IPC_MAXMSGSIZE 90
36 typedef struct ipc_header {
43 uint16_t DSI_requestID;
46 static char *ipc_cmd_str[] = { "IPC_DISCOLDSESSION",
50 * Pass afp_socket to old disconnected session if one has a matching token (token = pid)
51 * @returns -1 on error, 0 if no matching session was found, 1 if session was found and socket passed
53 static int ipc_kill_token(struct ipc_header *ipc, server_child *children)
57 if (ipc->len != sizeof(pid_t)) {
60 /* assume signals SA_RESTART set */
61 memcpy (&pid, ipc->msg, sizeof(pid_t));
63 return server_child_transfer_session(children,
71 /* ----------------- */
72 static int ipc_get_session(struct ipc_header *ipc, server_child *children)
79 if (ipc->len < (sizeof(idlen) + sizeof(boottime)) )
83 memcpy (&idlen, p, sizeof(idlen));
84 idlen = ntohl (idlen);
87 memcpy (&boottime, p, sizeof(boottime));
88 p += sizeof(boottime);
90 if (ipc->len < idlen + sizeof(idlen) + sizeof(boottime))
93 if (NULL == (clientid = (char*) malloc(idlen)) )
95 memcpy (clientid, p, idlen);
97 LOG(log_debug, logtype_afpd, "ipc_get_session(pid: %u, uid: %u, time: 0x%08x)",
98 ipc->child_pid, ipc->uid, boottime);
100 server_child_kill_one_by_id(children,
111 /***********************************************************************************
113 ***********************************************************************************/
116 * Listen on UNIX domain socket "name" for IPC from old sesssion
118 * @args name (r) file name to use for UNIX domain socket
119 * @returns socket fd, -1 on error
121 int ipc_server_uds(const char *name)
124 struct sockaddr_un address;
125 socklen_t address_length;
128 EC_NEG1_LOG( fd = socket(PF_UNIX, SOCK_STREAM, 0) );
129 EC_ZERO_LOG( setnonblock(fd, 1) );
131 address.sun_family = AF_UNIX;
132 address_length = sizeof(address.sun_family) + sprintf(address.sun_path, name);
133 EC_ZERO_LOG( bind(fd, (struct sockaddr *)&address, address_length) );
134 EC_ZERO_LOG( listen(fd, 1024) );
140 LOG(log_note, logtype_afpd, "ipc_server_uds: fd: %d", fd);
145 * Connect to UNIX domain socket "name" for IPC with new afpd master
148 * 2. send pid, which establishes a child structure for us in the master
150 * @args name (r) file name to use for UNIX domain socket
151 * @returns socket fd, -1 on error
153 int ipc_client_uds(const char *name)
156 struct sockaddr_un address;
157 socklen_t address_length;
159 pid_t pid = getpid();
161 EC_NEG1_LOG( fd = socket(PF_UNIX, SOCK_STREAM, 0) );
162 EC_ZERO_LOG( setnonblock(fd, 1) );
163 address.sun_family = AF_UNIX;
164 address_length = sizeof(address.sun_family) + sprintf(address.sun_path, name);
166 EC_ZERO_LOG( connect(fd, (struct sockaddr *)&address, address_length) ); /* 1 */
167 LOG(log_note, logtype_afpd, "ipc_client_uds: connected to master");
169 if (writet(fd, &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
170 LOG(log_error, logtype_afpd, "ipc_client_uds: writet: %s", strerror(errno));
173 LOG(log_note, logtype_afpd, "ipc_client_uds: sent pid");
179 LOG(log_note, logtype_afpd, "ipc_client_uds: fd: %d", fd);
183 int reconnect_ipc(AFPObj *obj)
187 LOG(log_note, logtype_afpd, "reconnect_ipc: start");
193 sleep((random() % 10) + 5); /* give it enough time to start */
195 while (retrycount++ < 10) {
196 if ((obj->ipc_fd = ipc_client_uds(_PATH_AFP_IPC)) == -1) {
197 LOG(log_error, logtype_afpd, "reconnect_ipc: cant reconnect to master");
201 LOG(log_note, logtype_afpd, "reconnect_ipc: succesfull IPC reconnect");
216 * Read a IPC message from a child
218 * @args children (rw) pointer to our structure with all childs
219 * @args fd (r) IPC socket with child
221 * @returns number of bytes transfered, -1 on error, 0 on EOF
223 int ipc_server_read(server_child *children, int fd)
226 struct ipc_header ipc;
227 char buf[IPC_MAXMSGSIZE], *p;
229 if ((ret = read(fd, buf, IPC_HEADERLEN)) != IPC_HEADERLEN) {
230 LOG(log_error, logtype_afpd, "Reading IPC header failed (%i of %u bytes read): %s",
231 ret, IPC_HEADERLEN, strerror(errno));
237 memcpy(&ipc.command, p, sizeof(ipc.command));
238 p += sizeof(ipc.command);
240 memcpy(&ipc.child_pid, p, sizeof(ipc.child_pid));
241 p += sizeof(ipc.child_pid);
243 memcpy(&ipc.uid, p, sizeof(ipc.uid));
244 p += sizeof(ipc.uid);
246 memcpy(&ipc.len, p, sizeof(ipc.len));
248 /* This should never happen */
249 if (ipc.len > (IPC_MAXMSGSIZE - IPC_HEADERLEN)) {
250 LOG (log_info, logtype_afpd, "IPC message exceeds allowed size (%u)", ipc.len);
254 memset (buf, 0, IPC_MAXMSGSIZE);
256 if ((ret = read(fd, buf, ipc.len)) != (int) ipc.len) {
257 LOG(log_info, logtype_afpd, "Reading IPC message failed (%u of %u bytes read): %s",
258 ret, ipc.len, strerror(errno));
264 LOG(log_debug, logtype_afpd, "ipc_server_read(%s): pid: %u",
265 ipc_cmd_str[ipc.command], ipc.child_pid);
269 switch (ipc.command) {
271 case IPC_DISCOLDSESSION:
272 if (readt(fd, &ipc.DSI_requestID, 2, 0, 2) != 2) {
273 LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): couldnt read DSI id: %s",
274 ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
276 if ((ipc.afp_socket = recv_fd(fd, 1)) == -1) {
277 LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): recv_fd: %s",
278 ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno));
281 if (ipc_kill_token(&ipc, children) == 1) {
282 /* Transfered session (ie afp_socket) to old disconnected child, now kill the new one */
283 LOG(log_note, logtype_afpd, "Reconnect: killing new session child[%u] after transfer",
285 kill(ipc.child_pid, SIGTERM);
287 close(ipc.afp_socket);
291 if (ipc_get_session(&ipc, children) != 0)
296 LOG (log_info, logtype_afpd, "ipc_read: unknown command: %d", ipc.command);
303 /* ----------------- */
304 int ipc_child_write(int fd, uint16_t command, int len, void *msg)
306 char block[IPC_MAXMSGSIZE], *p;
313 memset ( p, 0 , IPC_MAXMSGSIZE);
314 if (len + IPC_HEADERLEN > IPC_MAXMSGSIZE)
317 memcpy(p, &command, sizeof(command));
318 p += sizeof(command);
321 memcpy(p, &pid, sizeof(pid_t));
325 * using uid is wrong. It will not disconnect if the new connection
326 * is with a different user.
327 * But we really don't want a remote kill command.
330 memcpy(p, &uid, sizeof(uid_t));
338 LOG(log_debug, logtype_afpd, "ipc_child_write(%s)", ipc_cmd_str[command]);
340 if ((ret = writet(fd, block, len+IPC_HEADERLEN, 0, 1)) != -len+IPC_HEADERLEN) {