#endif
#include <sys/types.h>
-#ifdef HAVE_UNISTD_H
#include <unistd.h>
-#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
+#include <sys/un.h>
#include <errno.h>
#include <signal.h>
+#include <time.h>
+#include <pthread.h>
#include <atalk/server_child.h>
#include <atalk/server_ipc.h>
#include <atalk/logger.h>
#include <atalk/util.h>
+#include <atalk/errchk.h>
+#include <atalk/paths.h>
+#include <atalk/globals.h>
+#include <atalk/dsi.h>
+
+#define IPC_HEADERLEN 14
+#define IPC_MAXMSGSIZE 90
typedef struct ipc_header {
uint16_t command;
* 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)
+static int ipc_kill_token(struct ipc_header *ipc, server_child_t *children)
{
pid_t pid;
memcpy (&pid, ipc->msg, sizeof(pid_t));
return server_child_transfer_session(children,
- CHILD_DSIFORK,
pid,
ipc->uid,
ipc->afp_socket,
}
/* ----------------- */
-static int ipc_get_session(struct ipc_header *ipc, server_child *children)
+static int ipc_get_session(struct ipc_header *ipc, server_child_t *children)
{
- u_int32_t boottime;
- u_int32_t idlen;
+ uint32_t boottime;
+ uint32_t idlen;
char *clientid, *p;
ipc->child_pid, ipc->uid, boottime);
server_child_kill_one_by_id(children,
- CHILD_DSIFORK,
ipc->child_pid,
ipc->uid,
idlen,
return 0;
}
-#define IPC_HEADERLEN 14
-#define IPC_MAXMSGSIZE 90
+static int ipc_set_state(struct ipc_header *ipc, server_child_t *children)
+{
+ EC_INIT;
+ afp_child_t *child;
+
+ pthread_mutex_lock(&children->servch_lock);
+
+ if ((child = server_child_resolve(children, ipc->child_pid)) == NULL)
+ EC_FAIL;
+
+ memcpy(&child->afpch_state, ipc->msg, sizeof(uint16_t));
+
+EC_CLEANUP:
+ pthread_mutex_unlock(&children->servch_lock);
+ EC_EXIT;
+}
+
+static int ipc_set_volumes(struct ipc_header *ipc, server_child_t *children)
+{
+ EC_INIT;
+ afp_child_t *child;
+
+ pthread_mutex_lock(&children->servch_lock);
+
+ if ((child = server_child_resolve(children, ipc->child_pid)) == NULL)
+ EC_FAIL;
+
+ if (child->afpch_volumes) {
+ free(child->afpch_volumes);
+ child->afpch_volumes = NULL;
+ }
+ if (ipc->len)
+ child->afpch_volumes = strdup(ipc->msg);
+
+EC_CLEANUP:
+ pthread_mutex_unlock(&children->servch_lock);
+ EC_EXIT;
+}
+
+/***********************************************************************************
+ * Public functions
+ ***********************************************************************************/
/* -----------------
* Ipc format
* pid
* uid
*
-*/
+ */
/*!
* Read a IPC message from a child
*
+ * This is using an fd with non-blocking IO, so EAGAIN is not an error
+ *
* @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
+ * @returns -1 on error, 0 on success
*/
-int ipc_server_read(server_child *children, int fd)
+int ipc_server_read(server_child_t *children, int fd)
{
- int ret = 0;
+ int ret;
struct ipc_header ipc;
char buf[IPC_MAXMSGSIZE], *p;
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;
+ if (ret != 0) {
+ if (errno == EAGAIN)
+ return 0;
+ LOG(log_error, logtype_afpd, "Reading IPC header failed (%i of %u bytes read): %s",
+ ret, IPC_HEADERLEN, strerror(errno));
+ }
+ return -1;
}
p = buf;
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;
+ return -1;
}
}
ipc.msg = buf;
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));
+ return -1;
}
if ((ipc.afp_socket = recv_fd(fd, 1)) == -1) {
LOG (log_error, logtype_afpd, "ipc_read(%s:child[%u]): recv_fd: %s",
return -1;
break;
+ case IPC_STATE:
+ if (ipc_set_state(&ipc, children) != 0)
+ return -1;
+ break;
+
+ case IPC_VOLUMES:
+ if (ipc_set_volumes(&ipc, children) != 0)
+ return -1;
+ break;
+
default:
LOG (log_info, logtype_afpd, "ipc_read: unknown command: %d", ipc.command);
return -1;
}
- return ret;
+ return 0;
}
/* ----------------- */
char block[IPC_MAXMSGSIZE], *p;
pid_t pid;
uid_t uid;
+ ssize_t ret;
+
p = block;
memset ( p, 0 , IPC_MAXMSGSIZE);
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;
}
+int ipc_child_state(AFPObj *obj, uint16_t state)
+{
+ return ipc_child_write(obj->ipc_fd, IPC_STATE, sizeof(uint16_t), &state);
+}