X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=libatalk%2Futil%2Fserver_ipc.c;h=ed4e1261ef0a0fb08c4f7fb371888efee13ad231;hb=80fe6e63eb8ec9a396b4383cb8210fd5ded6ad35;hp=76d3bc3b8a1f492433b77084ab6d207e0e5179ec;hpb=d28f176330075b2789ce4d50a64fc3fe5d6bbe9b;p=netatalk.git diff --git a/libatalk/util/server_ipc.c b/libatalk/util/server_ipc.c index 76d3bc3b..ed4e1261 100644 --- a/libatalk/util/server_ipc.c +++ b/libatalk/util/server_ipc.c @@ -9,20 +9,27 @@ #endif #include -#ifdef HAVE_UNISTD_H #include -#endif #include #include #include #include +#include #include #include +#include #include #include #include #include +#include +#include +#include +#include + +#define IPC_HEADERLEN 14 +#define IPC_MAXMSGSIZE 90 typedef struct ipc_header { uint16_t command; @@ -62,8 +69,8 @@ static int ipc_kill_token(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; + uint32_t boottime; + uint32_t idlen; char *clientid, *p; @@ -85,8 +92,8 @@ static int ipc_get_session(struct ipc_header *ipc, server_child *children) return -1; memcpy (clientid, p, idlen); - LOG (log_debug, logtype_afpd, "ipc_get_session(pid: %u, uid: %u, time %x)", - ipc->child_pid, ipc->uid, 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, @@ -99,8 +106,100 @@ static int ipc_get_session(struct ipc_header *ipc, server_child *children) 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 @@ -108,7 +207,7 @@ static int ipc_get_session(struct ipc_header *ipc, server_child *children) * pid * uid * -*/ + */ /*! * Read a IPC message from a child @@ -180,8 +279,9 @@ int ipc_server_read(server_child *children, int fd) /* 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, SIGKILL); + kill(ipc.child_pid, SIGTERM); } + close(ipc.afp_socket); break; case IPC_GETSESSION: @@ -203,6 +303,8 @@ 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); @@ -232,6 +334,9 @@ int ipc_child_write(int fd, uint16_t command, int len, void *msg) LOG(log_debug, logtype_afpd, "ipc_child_write(%s)", ipc_cmd_str[command]); - return write(fd, block, len+IPC_HEADERLEN ); -} + if ((ret = writet(fd, block, len+IPC_HEADERLEN, 0, 1)) != len + IPC_HEADERLEN) { + return -1; + } + return 0; +}