X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=libatalk%2Futil%2Fserver_ipc.c;h=c258398d847d242944eb989d6348e59c84d2290e;hb=b0bcb8f6b0571592a50ce039882c9319e012a270;hp=dadeda6ae4c7b803f7559a31da26eea32a97aa19;hpb=ecfc96169ab669b578e53fa8e13592934fe37788;p=netatalk.git diff --git a/libatalk/util/server_ipc.c b/libatalk/util/server_ipc.c index dadeda6a..c258398d 100644 --- a/libatalk/util/server_ipc.c +++ b/libatalk/util/server_ipc.c @@ -1,10 +1,7 @@ /* - * $Id: server_ipc.c,v 1.2 2005-04-28 20:50:05 bfernhomberg Exp $ - * * All rights reserved. See COPYRIGHT. * - * - * ipc between parent and children. + * IPC over socketpair between parent and children. */ #ifdef HAVE_CONFIG_H @@ -12,51 +9,46 @@ #endif #include -#ifdef HAVE_UNISTD_H #include -#endif #include #include #include +#include +#include +#include +#include +#include #include #include #include +#include +#include +#include +#include +#include -typedef struct ipc_header { - u_int16_t command; - pid_t child_pid; - uid_t uid; - u_int32_t len; - char *msg; -} ipc_header; - -static int pipe_fd[2]; - -void *server_ipc_create(void) -{ - if (pipe(pipe_fd)) { - return NULL; - } - return &pipe_fd; -} - -/* ----------------- */ -int server_ipc_child(void *obj _U_) -{ - /* close input */ - close(pipe_fd[0]); - return pipe_fd[1]; -} +#define IPC_HEADERLEN 14 +#define IPC_MAXMSGSIZE 90 -/* ----------------- */ -int server_ipc_parent(void *obj _U_) -{ - return pipe_fd[0]; -} +typedef struct ipc_header { + uint16_t command; + pid_t child_pid; + uid_t uid; + uint32_t len; + char *msg; + int afp_socket; + uint16_t DSI_requestID; +} ipc_header_t; + +static char *ipc_cmd_str[] = { "IPC_DISCOLDSESSION", + "IPC_GETSESSION"}; -/* ----------------- */ -int ipc_kill_token (struct ipc_header *ipc, server_child *children) +/* + * Pass afp_socket to old disconnected session if one has a matching token (token = pid) + * @returns -1 on error, 0 if no matching session was found, 1 if session was found and socket passed + */ +static int ipc_kill_token(struct ipc_header *ipc, server_child *children) { pid_t pid; @@ -66,22 +58,25 @@ int ipc_kill_token (struct ipc_header *ipc, server_child *children) /* assume signals SA_RESTART set */ memcpy (&pid, ipc->msg, sizeof(pid_t)); - LOG(log_info, logtype_default, "child %d user %d disconnected", pid, ipc->uid); - server_child_kill_one(children, CHILD_DSIFORK, pid, ipc->uid); - return 0; + return server_child_transfer_session(children, + CHILD_DSIFORK, + pid, + ipc->uid, + ipc->afp_socket, + ipc->DSI_requestID); } /* ----------------- */ -int ipc_get_session (struct ipc_header *ipc, server_child *children) +static int ipc_get_session(struct ipc_header *ipc, server_child *children) { u_int32_t boottime; u_int32_t idlen; char *clientid, *p; + + if (ipc->len < (sizeof(idlen) + sizeof(boottime)) ) + return -1; - if (ipc->len < (sizeof(idlen) + sizeof(boottime)) ) { - return -1; - } p = ipc->msg; memcpy (&idlen, p, sizeof(idlen)); idlen = ntohl (idlen); @@ -90,22 +85,121 @@ int ipc_get_session (struct ipc_header *ipc, server_child *children) memcpy (&boottime, p, sizeof(boottime)); p += sizeof(boottime); - if (ipc->len < idlen + sizeof(idlen) + sizeof(boottime)) { - return -1; - } - if (NULL == (clientid = (char*) malloc(idlen)) ) { - return -1; - } + if (ipc->len < idlen + sizeof(idlen) + sizeof(boottime)) + return -1; + + if (NULL == (clientid = (char*) malloc(idlen)) ) + return -1; memcpy (clientid, p, idlen); - server_child_kill_one_by_id (children, CHILD_DSIFORK, ipc->child_pid, ipc->uid, idlen, clientid, boottime); - /* FIXME byte to ascii if we want to log clientid */ - LOG (log_info, logtype_afpd, "ipc_get_session: len: %u, idlen %d, time %x", ipc->len, idlen, boottime); + LOG(log_debug, logtype_afpd, "ipc_get_session(pid: %u, uid: %u, time: 0x%08x)", + ipc->child_pid, ipc->uid, boottime); + + server_child_kill_one_by_id(children, + CHILD_DSIFORK, + ipc->child_pid, + ipc->uid, + idlen, + clientid, + boottime); + return 0; } -#define IPC_HEADERLEN 14 -#define IPC_MAXMSGSIZE 90 +/*********************************************************************************** + * Public functions + ***********************************************************************************/ + +/*! + * Listen on UNIX domain socket "name" for IPC from old sesssion + * + * @args name (r) file name to use for UNIX domain socket + * @returns socket fd, -1 on error + */ +int ipc_server_uds(const char *name) +{ + EC_INIT; + struct sockaddr_un address; + socklen_t address_length; + int fd = -1; + + EC_NEG1_LOG( fd = socket(PF_UNIX, SOCK_STREAM, 0) ); + EC_ZERO_LOG( setnonblock(fd, 1) ); + unlink(name); + address.sun_family = AF_UNIX; + address_length = sizeof(address.sun_family) + sprintf(address.sun_path, name); + EC_ZERO_LOG( bind(fd, (struct sockaddr *)&address, address_length) ); + EC_ZERO_LOG( listen(fd, 1024) ); + +EC_CLEANUP: + if (ret != 0) { + return -1; + } + + return fd; +} + +/*! + * Connect to UNIX domain socket "name" for IPC with new afpd master + * + * 1. Connect + * 2. send pid, which establishes a child structure for us in the master + * + * @args name (r) file name to use for UNIX domain socket + * @returns socket fd, -1 on error + */ +int ipc_client_uds(const char *name) +{ + EC_INIT; + struct sockaddr_un address; + socklen_t address_length; + int fd = -1; + pid_t pid = getpid(); + + EC_NEG1_LOG( fd = socket(PF_UNIX, SOCK_STREAM, 0) ); + EC_ZERO_LOG( setnonblock(fd, 1) ); + address.sun_family = AF_UNIX; + address_length = sizeof(address.sun_family) + sprintf(address.sun_path, name); + + EC_ZERO_LOG( connect(fd, (struct sockaddr *)&address, address_length) ); /* 1 */ + LOG(log_debug, logtype_afpd, "ipc_client_uds: connected to master"); + + if (writet(fd, &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) { + LOG(log_error, logtype_afpd, "ipc_client_uds: writet: %s", strerror(errno)); + EC_FAIL; + } + +EC_CLEANUP: + if (ret != 0) { + return -1; + } + LOG(log_debug, logtype_afpd, "ipc_client_uds: fd: %d", fd); + return fd; +} + +int reconnect_ipc(AFPObj *obj) +{ + int retrycount = 0; + + LOG(log_debug, logtype_afpd, "reconnect_ipc: start"); + + close(obj->ipc_fd); + obj->ipc_fd = -1; + + srandom(getpid()); + sleep((random() % 5) + 5); /* give it enough time to start */ + + while (retrycount++ < 10) { + if ((obj->ipc_fd = ipc_client_uds(_PATH_AFP_IPC)) == -1) { + LOG(log_error, logtype_afpd, "reconnect_ipc: cant reconnect to master"); + sleep(1); + continue; + } + LOG(log_debug, logtype_afpd, "reconnect_ipc: succesfull IPC reconnect"); + return 0; + } + return -1; +} /* ----------------- * Ipc format @@ -113,17 +207,27 @@ int ipc_get_session (struct ipc_header *ipc, server_child *children) * pid * uid * -*/ -int server_ipc_read(server_child *children) + */ + +/*! + * Read a IPC message from a child + * + * @args children (rw) pointer to our structure with all childs + * @args fd (r) IPC socket with child + * + * @returns number of bytes transfered, -1 on error, 0 on EOF + */ +int ipc_server_read(server_child *children, int fd) { int ret = 0; struct ipc_header ipc; char buf[IPC_MAXMSGSIZE], *p; - if ((ret = read(pipe_fd[0], buf, IPC_HEADERLEN)) != IPC_HEADERLEN) { - LOG (log_info, logtype_afpd, "Reading IPC header failed (%u of %u bytes read)", ret, IPC_HEADERLEN); - return -1; - } + if ((ret = read(fd, buf, IPC_HEADERLEN)) != IPC_HEADERLEN) { + LOG(log_error, logtype_afpd, "Reading IPC header failed (%i of %u bytes read): %s", + ret, IPC_HEADERLEN, strerror(errno)); + return ret; + } p = buf; @@ -139,44 +243,68 @@ int server_ipc_read(server_child *children) memcpy(&ipc.len, p, sizeof(ipc.len)); /* This should never happen */ - if (ipc.len > (IPC_MAXMSGSIZE - IPC_HEADERLEN)) - { - LOG (log_info, logtype_afpd, "IPC message exceeds allowed size (%u)", ipc.len); - return -1; + if (ipc.len > (IPC_MAXMSGSIZE - IPC_HEADERLEN)) { + LOG (log_info, logtype_afpd, "IPC message exceeds allowed size (%u)", ipc.len); + return -1; } memset (buf, 0, IPC_MAXMSGSIZE); if ( ipc.len != 0) { - if ((ret = read(pipe_fd[0], buf, ipc.len)) != (int) ipc.len) { - LOG (log_info, logtype_afpd, "Reading IPC message failed (%u of %u bytes read)", ret, ipc.len); - return -1; + if ((ret = read(fd, buf, ipc.len)) != (int) ipc.len) { + LOG(log_info, logtype_afpd, "Reading IPC message failed (%u of %u bytes read): %s", + ret, ipc.len, strerror(errno)); + return ret; } } ipc.msg = buf; - - LOG (log_info, logtype_afpd, "ipc_read: command: %u, pid: %u, len: %u", ipc.command, ipc.child_pid, ipc.len); - switch (ipc.command) - { - case IPC_KILLTOKEN: - return (ipc_kill_token(&ipc, children)); - break; + LOG(log_debug, logtype_afpd, "ipc_server_read(%s): pid: %u", + ipc_cmd_str[ipc.command], ipc.child_pid); + + int afp_socket; + + switch (ipc.command) { + + case IPC_DISCOLDSESSION: + if (readt(fd, &ipc.DSI_requestID, 2, 0, 2) != 2) { + LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): couldnt read DSI id: %s", + ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno)); + } + if ((ipc.afp_socket = recv_fd(fd, 1)) == -1) { + LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): recv_fd: %s", + ipc_cmd_str[ipc.command], ipc.child_pid, strerror(errno)); + return -1; + } + if (ipc_kill_token(&ipc, children) == 1) { + /* Transfered session (ie afp_socket) to old disconnected child, now kill the new one */ + LOG(log_note, logtype_afpd, "Reconnect: killing new session child[%u] after transfer", + ipc.child_pid); + kill(ipc.child_pid, SIGTERM); + } + close(ipc.afp_socket); + break; + case IPC_GETSESSION: - return (ipc_get_session(&ipc, children)); - break; + if (ipc_get_session(&ipc, children) != 0) + return -1; + break; + default: LOG (log_info, logtype_afpd, "ipc_read: unknown command: %d", ipc.command); return -1; } + return ret; } /* ----------------- */ -int server_ipc_write( u_int16_t command, int len, void *msg) +int ipc_child_write(int fd, uint16_t command, int len, void *msg) { char block[IPC_MAXMSGSIZE], *p; pid_t pid; uid_t uid; + ssize_t ret; + p = block; memset ( p, 0 , IPC_MAXMSGSIZE); @@ -204,7 +332,11 @@ int server_ipc_write( u_int16_t command, int len, void *msg) memcpy(p, msg, len); - LOG (log_info, logtype_afpd, "ipc_write: command: %u, pid: %u, msglen: %u", command, pid, len); - return write(pipe_fd[1], block, len+IPC_HEADERLEN ); -} + LOG(log_debug, logtype_afpd, "ipc_child_write(%s)", ipc_cmd_str[command]); + + if ((ret = writet(fd, block, len+IPC_HEADERLEN, 0, 1)) != len + IPC_HEADERLEN) { + return -1; + } + return 0; +}