Mailing List Archive

r19467 - projects/haf/trunk/maemo-launcher/launcher
Author: makarhun
Date: 2009-10-21 09:33:43 +0300 (Wed, 21 Oct 2009)
New Revision: 19467

Modified:
projects/haf/trunk/maemo-launcher/launcher/comm_msg.c
projects/haf/trunk/maemo-launcher/launcher/invokelib.c
projects/haf/trunk/maemo-launcher/launcher/invokelib.h
projects/haf/trunk/maemo-launcher/launcher/invoker.c
projects/haf/trunk/maemo-launcher/launcher/launcher.c
Log:
reliable_io_strings.patch from Leonid

Modified: projects/haf/trunk/maemo-launcher/launcher/comm_msg.c
===================================================================
--- projects/haf/trunk/maemo-launcher/launcher/comm_msg.c 2009-10-20 14:04:07 UTC (rev 19466)
+++ projects/haf/trunk/maemo-launcher/launcher/comm_msg.c 2009-10-21 06:33:43 UTC (rev 19467)
@@ -272,14 +272,15 @@
bool
comm_msg_send(int fd, comm_msg_t *msg)
{
- write(fd, &msg->used, sizeof(msg->used));
- write(fd, msg->buf, msg->used);
+ const bool result =
+ (sizeof(msg->used) == (uint32_t)write(fd, &msg->used, sizeof(msg->used))) &&
+ (msg->used == (uint32_t)write(fd, msg->buf, msg->used));

#if DEBUG
comm_msg_print(msg, __FUNCTION__);
#endif

- return true;
+ return result;
}

bool
@@ -287,13 +288,15 @@
{
uint32_t size;

- read(fd, &size, sizeof(size));
+ if ( invoke_raw_read(fd, &size, sizeof(size)) )
+ return false;

if (!comm_msg_grow(msg, size))
return false;

- read(fd, msg->buf, size);
msg->used = size;
+ if ( invoke_raw_read(fd, msg->buf, size) )
+ return false;

#if DEBUG
comm_msg_print(msg, __FUNCTION__);

Modified: projects/haf/trunk/maemo-launcher/launcher/invokelib.c
===================================================================
--- projects/haf/trunk/maemo-launcher/launcher/invokelib.c 2009-10-20 14:04:07 UTC (rev 19466)
+++ projects/haf/trunk/maemo-launcher/launcher/invokelib.c 2009-10-21 06:33:43 UTC (rev 19467)
@@ -18,6 +18,7 @@
*
*/

+#include <errno.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
@@ -27,24 +28,53 @@
#include "report.h"
#include "invokelib.h"

+
+int
+invoke_raw_read(int fd, void* buffer, uint32_t size)
+{
+ uint32_t cnt = size;
+ char* buf = (char*)buffer;
+
+ /* check buffer and size */
+ if (NULL == buffer || 0 == size)
+ return EINVAL;
+
+ /* load message in several iterations */
+ while (cnt > 0)
+ {
+ const ssize_t result = read(fd, buf, cnt);
+
+ if (result > 0)
+ {
+ buf += result;
+ cnt -= result;
+ }
+ else
+ {
+ /* prevent rubish in data */
+ memset(buf, 0, cnt);
+ return errno;
+ }
+ }
+
+ return 0;
+} /* invoke_raw_read */
+
+
+
bool
invoke_send_msg(int fd, uint32_t msg)
{
debug("%s: %08x\n", __FUNCTION__, msg);
-
- write(fd, &msg, sizeof(msg));
-
- return true;
+ return (sizeof(msg) == write(fd, &msg, sizeof(msg)));
}

bool
invoke_recv_msg(int fd, uint32_t *msg)
{
- read(fd, msg, sizeof(*msg));
-
- debug("%s: %08x\n", __FUNCTION__, *msg);
-
- return true;
+ const int result = invoke_raw_read(fd, msg, sizeof(*msg));
+ debug("%s: %d %08x\n", __FUNCTION__, result, *msg);
+ return (0 == result);
}

bool
@@ -53,13 +83,27 @@
uint32_t size;

/* Send size. */
- size = strlen(str) + 1;
- invoke_send_msg(fd, size);
+ size = (str && *str ? strlen(str) : 0);
+ if (size > INVOKER_MAX_STRING_SIZE)
+ {
+ error("string size is %u and larger than %u in %s\n", size, INVOKER_MAX_STRING_SIZE, __FUNCTION__);
+ return false;
+ }

+ if ( !invoke_send_msg(fd, size) )
+ {
+ error("unable to write string size is %u in %s\n", size, __FUNCTION__);
+ return false;
+ }
+
debug("%s: '%s'\n", __FUNCTION__, str);

- /* Send the string. */
- write(fd, str, size);
+ /* Send the string if size is non-zero */
+ if (size && size != (uint32_t)write(fd, str, size))
+ {
+ error("unable to write string with size %u in %s\n", size, __FUNCTION__);
+ return false;
+ }

return true;
}
@@ -67,30 +111,43 @@
char *
invoke_recv_str(int fd)
{
- uint32_t size, ret;
+ uint32_t size;
char *str;

/* Get the size. */
- invoke_recv_msg(fd, &size);
- str = malloc(size);
- if (!str)
+ if ( !invoke_recv_msg(fd, &size) )
{
- error("mallocing in %s\n", __FUNCTION__);
+ error("string size read failure in %s\n", __FUNCTION__);
return NULL;
}

- /* Get the string. */
- ret = read(fd, str, size);
- if (ret < size)
+ if (size > INVOKER_MAX_STRING_SIZE)
{
- error("getting string, got %u of %u bytes\n", ret, size);
- free(str);
+ error("string size is %u and larger than %u in %s\n", size, INVOKER_MAX_STRING_SIZE, __FUNCTION__);
return NULL;
}
- str[size - 1] = '\0';

+ str = malloc(size + 1);
+ if (!str)
+ {
+ error("mallocing in %s for %u bytes string failed\n", __FUNCTION__, size);
+ return NULL;
+ }
+
+ /* Get the string if size is non-zero */
+ if ( size )
+ {
+ const int ret = invoke_raw_read(fd, str, size);
+ if ( ret )
+ {
+ error("getting string with %u bytes got error %d\n", size, ret);
+ free(str);
+ return NULL;
+ }
+ }
+ str[size] = 0;
+
debug("%s: '%s'\n", __FUNCTION__, str);

return str;
}
-

Modified: projects/haf/trunk/maemo-launcher/launcher/invokelib.h
===================================================================
--- projects/haf/trunk/maemo-launcher/launcher/invokelib.h 2009-10-20 14:04:07 UTC (rev 19466)
+++ projects/haf/trunk/maemo-launcher/launcher/invokelib.h 2009-10-21 06:33:43 UTC (rev 19467)
@@ -30,6 +30,9 @@
bool invoke_send_str(int fd, char *str);
char *invoke_recv_str(int fd);

+/* read data from socket to pointed buffer. expected size bytes to be loaded. return 0 or errno */
+int invoke_raw_read(int fd, void* buffer, uint32_t size);
+
/* FIXME: Should be '/var/run/'. */
#define INVOKER_SOCK "/tmp/."PACKAGE

@@ -51,5 +54,8 @@
#define INVOKER_MSG_EXIT 0xe4170000
#define INVOKER_MSG_ACK 0x600d0000

+/* String length limitation */
+#define INVOKER_MAX_STRING_SIZE (64 * 1024)
+
#endif


Modified: projects/haf/trunk/maemo-launcher/launcher/invoker.c
===================================================================
--- projects/haf/trunk/maemo-launcher/launcher/invoker.c 2009-10-20 14:04:07 UTC (rev 19466)
+++ projects/haf/trunk/maemo-launcher/launcher/invoker.c 2009-10-21 06:33:43 UTC (rev 19467)
@@ -95,20 +95,34 @@
warning("could not disable self core dumping");
}

+
static bool
-invoke_recv_ack(int fd)
+__invoke_receive_marker(int fd, const uint32_t action, const char* action_name, const char* function)
{
- uint32_t action;
+ uint32_t marker;

- /* Revceive ACK. */
- invoke_recv_msg(fd, &action);
+ /* Receive action marker */
+ if ( !invoke_recv_msg(fd, &marker) )
+ die(1, "receiving %s action failed for %s and fd = %d: %s\n", action_name, function, fd, strerror(errno));

- if (action != INVOKER_MSG_ACK)
- die(1, "receiving wrong ack (%08x)\n", action);
- else
- return true;
+ /* Compare with expected */
+ if (action != marker)
+ die(1, "receiving bad marker %08x when expected %08x (%s)\n", marker, action, action_name);
+
+ return true;
+} /* __invoke_receive_marker */
+
+#define invoke_receive_marker(fd,action) __invoke_receive_marker(fd, action, #action, __FUNCTION__)
+
+static bool
+__invoke_recv_ack(int fd, const char* function)
+{
+ return __invoke_receive_marker(fd, INVOKER_MSG_ACK, "INVOKER_MSG_ACK", function);
}

+#define invoke_recv_ack(fd) __invoke_recv_ack(fd, __FUNCTION__)
+
+
static int
invoker_init(void)
{
@@ -132,63 +146,42 @@
invoker_send_magic(int fd, int options)
{
/* Send magic. */
- invoke_send_msg(fd, INVOKER_MSG_MAGIC | INVOKER_MSG_MAGIC_VERSION | options);
-
- invoke_recv_ack(fd);
-
- return true;
+ return invoke_send_msg(fd, INVOKER_MSG_MAGIC | INVOKER_MSG_MAGIC_VERSION | options) && invoke_recv_ack(fd);
}

static bool
invoker_send_name(int fd, char *name)
{
/* Send action. */
- invoke_send_msg(fd, INVOKER_MSG_NAME);
- invoke_send_str(fd, name);
-
- invoke_recv_ack(fd);
-
- return true;
+ return invoke_send_msg(fd, INVOKER_MSG_NAME) && invoke_send_str(fd, name) && invoke_recv_ack(fd);
}

static bool
invoker_send_exec(int fd, char *exec)
{
/* Send action. */
- invoke_send_msg(fd, INVOKER_MSG_EXEC);
- invoke_send_str(fd, exec);
-
- invoke_recv_ack(fd);
-
- return true;
+ return invoke_send_msg(fd, INVOKER_MSG_EXEC) && invoke_send_str(fd, exec) && invoke_recv_ack(fd);
}

static bool
invoker_send_args(int fd, int argc, char **argv)
{
int i;
+ bool succ;

/* Send action. */
- invoke_send_msg(fd, INVOKER_MSG_ARGS);
- invoke_send_msg(fd, argc);
- for (i = 0; i < argc; i++)
- invoke_send_str(fd, argv[i]);
+ succ = invoke_send_msg(fd, INVOKER_MSG_ARGS) && invoke_send_msg(fd, argc);
+ for (i = 0; succ && i < argc; i++)
+ succ = invoke_send_str(fd, argv[i]);

- invoke_recv_ack(fd);
-
- return true;
+ return (succ && invoke_recv_ack(fd));
}

static bool
invoker_send_prio(int fd, int prio)
{
/* Send action. */
- invoke_send_msg(fd, INVOKER_MSG_PRIO);
- invoke_send_msg(fd, prio);
-
- invoke_recv_ack(fd);
-
- return true;
+ return invoke_send_msg(fd, INVOKER_MSG_PRIO) && invoke_send_msg(fd, prio) && invoke_recv_ack(fd);
}

static bool
@@ -218,7 +211,12 @@

msg.msg_controllen = cmsg->cmsg_len;

- invoke_send_msg(fd, INVOKER_MSG_IO);
+ if ( !invoke_send_msg(fd, INVOKER_MSG_IO) )
+ {
+ warning("invoke_send_msg failed in invoker_send_io: %s", strerror(errno));
+ return false;
+ }
+
if (sendmsg(fd, &msg, 0) < 0)
{
warning("sendmsg failed in invoker_send_io: %s", strerror(errno));
@@ -232,63 +230,48 @@
invoker_send_env(int fd)
{
int i, n_vars;
+ bool succ;

/* Count the amount of environment variables. */
for (n_vars = 0; environ[n_vars] != NULL; n_vars++) ;

/* Send action. */
- invoke_send_msg(fd, INVOKER_MSG_ENV);
- invoke_send_msg(fd, n_vars);
- for (i = 0; i < n_vars; i++)
- invoke_send_str(fd, environ[i]);
+ succ = invoke_send_msg(fd, INVOKER_MSG_ENV) && invoke_send_msg(fd, n_vars);
+ for (i = 0; succ && i < n_vars; i++)
+ succ = invoke_send_str(fd, environ[i]);

- return true;
+ return succ;
}

static bool
invoker_send_end(int fd)
{
/* Send action. */
- invoke_send_msg(fd, INVOKER_MSG_END);
-
- invoke_recv_ack(fd);
-
- return true;
+ return invoke_send_msg(fd, INVOKER_MSG_END) && invoke_recv_ack(fd);
}

static bool
invoker_recv_pid(int fd)
{
- uint32_t action, pid;
+ uint32_t pid;

- /* Receive action. */
- invoke_recv_msg(fd, &action);
+ /* Receive action marker and pid */
+ if (invoke_receive_marker(fd, INVOKER_MSG_PID) && invoke_recv_msg(fd, &pid))
+ invoked_pid = pid;
+ else
+ die(1, "receiving pid (%08x) failed for fd %d: %s\n", INVOKER_MSG_PID, fd, strerror(errno));

- if (action != INVOKER_MSG_PID)
- die(1, "receiving bad pid (%08x)\n", action);
-
- /* Receive pid. */
- invoke_recv_msg(fd, &pid);
- invoked_pid = pid;
-
return true;
}

static int
invoker_recv_exit(int fd)
{
- uint32_t action, status;
-
- /* Receive action. */
- invoke_recv_msg(fd, &action);
-
- if (action != INVOKER_MSG_EXIT)
- die(1, "receiving bad exit status (%08x)\n", action);
-
- /* Receive status. */
- invoke_recv_msg(fd, &status);
-
- return status;
+ uint32_t status;
+ if (invoke_receive_marker(fd, INVOKER_MSG_EXIT) && invoke_recv_msg(fd, &status))
+ return status;
+ else
+ die(1, "receiving status (%08x) failed for fd %d: %s\n", INVOKER_MSG_EXIT, fd, strerror(errno));
}

static uint32_t

Modified: projects/haf/trunk/maemo-launcher/launcher/launcher.c
===================================================================
--- projects/haf/trunk/maemo-launcher/launcher/launcher.c 2009-10-20 14:04:07 UTC (rev 19466)
+++ projects/haf/trunk/maemo-launcher/launcher/launcher.c 2009-10-21 06:33:43 UTC (rev 19467)
@@ -195,8 +195,7 @@
uint32_t magic;

/* Receive the magic. */
- invoke_recv_msg(fd, &magic);
- if ((magic & INVOKER_MSG_MASK) == INVOKER_MSG_MAGIC)
+ if (invoke_recv_msg(fd, &magic) && (magic & INVOKER_MSG_MASK) == INVOKER_MSG_MAGIC)
{
if ((magic & INVOKER_MSG_MAGIC_VERSION_MASK) == INVOKER_MSG_MAGIC_VERSION)
invoke_send_msg(fd, INVOKER_MSG_ACK);
@@ -208,7 +207,7 @@
}
else
{
- error("receiving bad magic (%08x)\n", magic);
+ error("receiving bad magic (%08x) for fd = %d: %s\n", magic, fd, strerror(errno));
return false;
}

@@ -223,10 +222,9 @@
uint32_t msg;

/* Get the action. */
- invoke_recv_msg(fd, &msg);
- if (msg != INVOKER_MSG_NAME)
+ if (invoke_recv_msg(fd, &msg) && msg != INVOKER_MSG_NAME)
{
- error("receiving invalid action (%08x)\n", msg);
+ error("receiving invalid action (%08x) for fd = %d: %s\n", msg, fd, strerror(errno));
return false;
}

@@ -234,9 +232,7 @@
if (!prog->name)
return false;

- invoke_send_msg(fd, INVOKER_MSG_ACK);
-
- return true;
+ return invoke_send_msg(fd, INVOKER_MSG_ACK);
}

static bool
@@ -246,9 +242,7 @@
if (!prog->filename)
return false;

- invoke_send_msg(fd, INVOKER_MSG_ACK);
-
- return true;
+ return invoke_send_msg(fd, INVOKER_MSG_ACK);
}

static bool
@@ -259,7 +253,12 @@
size_t size;

/* Get argc. */
- invoke_recv_msg(fd, &argc);
+ if ( !invoke_recv_msg(fd, &argc) )
+ {
+ error("error in receiving number of arguments fd = %d: %s\n", fd, strerror(errno));
+ return false;
+ }
+
prog->argc = argc;
size = argc * sizeof(char *);
if (size < argc)
@@ -285,9 +284,7 @@
}
}

- invoke_send_msg(fd, INVOKER_MSG_ACK);
-
- return true;
+ return invoke_send_msg(fd, INVOKER_MSG_ACK);
}

static bool
@@ -295,12 +292,13 @@
{
uint32_t prio;

- invoke_recv_msg(fd, &prio);
- prog->prio = prio;
+ if ( invoke_recv_msg(fd, &prio) )
+ {
+ prog->prio = prio;
+ return invoke_send_msg(fd, INVOKER_MSG_ACK);
+ }

- invoke_send_msg(fd, INVOKER_MSG_ACK);
-
- return true;
+ return false;
}

static bool
@@ -359,7 +357,11 @@
uint32_t n_vars;

/* Get number of environment variables. */
- invoke_recv_msg(fd, &n_vars);
+ if ( !invoke_recv_msg(fd, &n_vars) )
+ {
+ error("receiving environ counter fd = %d: %s\n", fd, strerror(errno));
+ return false;
+ }

/* Get environ. */
for (i = 0; i < n_vars; i++)
@@ -369,7 +371,7 @@
var = invoke_recv_str(fd);
if (var == NULL)
{
- error("receiving environ[%i]\n", i);
+ error("receiving environ[%i]: %s\n", i, strerror(errno));
return false;
}

@@ -387,10 +389,7 @@
static bool
invoked_send_action(int fd, int action, int value)
{
- invoke_send_msg(fd, action);
- invoke_send_msg(fd, value);
-
- return true;
+ return invoke_send_msg(fd, action) && invoke_send_msg(fd, value);
}

static bool
@@ -407,7 +406,11 @@
uint32_t action;

/* Get the action. */
- invoke_recv_msg(fd, &action);
+ if ( !invoke_recv_msg(fd, &action) )
+ {
+ error("receiving action failed in %s using fd %d: %s\n", __FUNCTION__, fd, strerror(errno));
+ return false;
+ }

switch (action)
{
@@ -650,6 +653,7 @@
int fd;
child_t *list = childs->list;
comm_msg_t *msg;
+ bool result;

unlink(statefilename);

@@ -677,12 +681,12 @@
comm_msg_put_str(msg, list[i].name);
}

- comm_msg_send(fd, msg);
+ result = comm_msg_send(fd, msg);

comm_msg_destroy(msg);
close(fd);

- return true;
+ return result;
}

static kindergarten_t *
@@ -695,6 +699,7 @@
kindergarten_t *childs;
child_t *list;
comm_msg_t *msg;
+ bool result;

fd = open(statefilename, O_RDONLY);
if (fd < 0)
@@ -705,12 +710,12 @@
}

msg = comm_msg_new(512, 0);
- comm_msg_recv(fd, msg);
+ result = comm_msg_recv(fd, msg);

close(fd);

comm_msg_get_magic(msg, &magic);
- if (LAUNCHER_STATE_SIG != magic)
+ if (result && LAUNCHER_STATE_SIG != magic)
{
error("wrong signature on persistence file '%s'\n", statefilename);
comm_msg_destroy(msg);

_______________________________________________
maemo-commits mailing list
maemo-commits@maemo.org
https://lists.maemo.org/mailman/listinfo/maemo-commits