Skip to content

Commit

Permalink
Merge pull request #232 from mtomaschewski/wait-device-ready
Browse files Browse the repository at this point in the history
netlink: initial uevent monitor
  • Loading branch information
mtomaschewski committed May 12, 2014
2 parents ac435bb + 88cc1f5 commit 2771722
Show file tree
Hide file tree
Showing 7 changed files with 739 additions and 56 deletions.
4 changes: 4 additions & 0 deletions include/wicked/util.h
Expand Up @@ -40,6 +40,7 @@ struct ni_variable {

typedef struct ni_var_array ni_var_array_t;
struct ni_var_array {
ni_var_array_t *next;
unsigned int count;
ni_var_t * data;
};
Expand Down Expand Up @@ -136,6 +137,9 @@ extern void ni_var_array_set_long(ni_var_array_t *, const char *name, unsigned
extern void ni_var_array_set_double(ni_var_array_t *, const char *name, double);
extern void ni_var_array_set_boolean(ni_var_array_t *, const char *name, int);

extern void ni_var_array_list_append(ni_var_array_t **, ni_var_array_t *);
extern void ni_var_array_list_destroy(ni_var_array_t **);

extern void ni_stringbuf_set(ni_stringbuf_t *, const char *);
extern void ni_stringbuf_init(ni_stringbuf_t *);
extern void ni_stringbuf_grow(ni_stringbuf_t *, size_t);
Expand Down
2 changes: 2 additions & 0 deletions src/Makefile.am
Expand Up @@ -94,6 +94,7 @@ libwicked_la_SOURCES = \
sysfs.c \
timer.c \
tuntap.c \
uevent.c \
update.c \
util.c \
vlan.c \
Expand Down Expand Up @@ -178,6 +179,7 @@ wicked_headers = \
process.h \
socket_priv.h \
sysfs.h \
uevent.h \
util_priv.h \
wireless_priv.h \
wpa-supplicant.h \
Expand Down
119 changes: 81 additions & 38 deletions src/process.c
Expand Up @@ -38,96 +38,139 @@ __ni_shellcmd_format(char **cmd, const ni_string_array_t *argv)
}

static void
__ni_shellcmd_free(ni_shellcmd_t *proc)
__ni_shellcmd_free(ni_shellcmd_t *cmd)
{
ni_string_array_destroy(&proc->environ);
ni_string_free(&proc->command);
free(proc);
ni_string_free(&cmd->command);
ni_string_array_destroy(&cmd->argv);
ni_string_array_destroy(&cmd->environ);
free(cmd);
}


/*
* Create a process description
*/
ni_shellcmd_t *
ni_shellcmd_new(const ni_string_array_t *argv)
{
ni_shellcmd_t *proc;
ni_shellcmd_t *cmd;
unsigned int i;

ni_assert(argv != NULL);

proc = xcalloc(1, sizeof(*proc));
cmd = xcalloc(1, sizeof(*cmd));
cmd->refcount = 1;
if (!argv)
return cmd;

for (i = 0; i < argv->count; ++i) {
const char *arg = argv->data[i];

if (ni_string_len(arg) == 0)
continue; /* fail ?! */
if (ni_string_empty(arg)) {
__ni_shellcmd_free(cmd);
return NULL;
}

if (ni_string_array_append(&proc->argv, arg) < 0) {
__ni_shellcmd_free(proc);
if (ni_string_array_append(&cmd->argv, arg) < 0) {
__ni_shellcmd_free(cmd);
return NULL;
}
}
if (__ni_shellcmd_format(&proc->command, &proc->argv) == NULL) {
__ni_shellcmd_free(proc);

if (__ni_shellcmd_format(&cmd->command, &cmd->argv) == NULL) {
__ni_shellcmd_free(cmd);
return NULL;
}
if (ni_string_array_copy(&proc->environ, __ni_default_environment()) < 0) {
__ni_shellcmd_free(proc);
if (ni_string_array_copy(&cmd->environ, __ni_default_environment()) < 0) {
__ni_shellcmd_free(cmd);
return NULL;
}

proc->refcount = 1;
return proc;
return cmd;
}

ni_shellcmd_t *
ni_shellcmd_parse(const char *command)
{
ni_shellcmd_t *proc;
ni_shellcmd_t *cmd;

ni_assert(command != NULL);
if (ni_string_empty(command))
return NULL;

proc = xcalloc(1, sizeof(*proc));
cmd = xcalloc(1, sizeof(*cmd));
cmd->refcount = 1;

ni_string_dup(&proc->command, command);
if (!__ni_shellcmd_parse(&proc->argv, proc->command)) {
__ni_shellcmd_free(proc);
ni_string_dup(&cmd->command, command);
if (!__ni_shellcmd_parse(&cmd->argv, cmd->command)) {
__ni_shellcmd_free(cmd);
return NULL;
}
if (ni_string_array_copy(&proc->environ, __ni_default_environment()) < 0) {
__ni_shellcmd_free(proc);
if (ni_string_array_copy(&cmd->environ, __ni_default_environment()) < 0) {
__ni_shellcmd_free(cmd);
return NULL;
}

proc->refcount = 1;
return proc;
return cmd;
}

void
ni_shellcmd_free(ni_shellcmd_t *proc)
ni_bool_t
ni_shellcmd_fmt_arg(ni_shellcmd_t *cmd, const char *fmt, ...)
{
ni_assert(proc->refcount == 0);
__ni_shellcmd_free(proc);
char *arg = NULL;
va_list ap;
int ret;

if (!cmd || ni_string_empty(fmt))
return FALSE;

va_start(ap, fmt);
ret = vasprintf(&arg, fmt, ap);
va_end(ap);
if (ret < 0)
return FALSE;

if (!ni_shellcmd_add_arg(cmd, arg)) {
ni_string_free(&arg);
return FALSE;
}
ni_string_free(&arg);
return TRUE;
}

ni_bool_t
ni_shellcmd_add_arg(ni_shellcmd_t *proc, const char *arg)
ni_shellcmd_add_arg(ni_shellcmd_t *cmd, const char *arg)
{
if (proc == NULL || ni_string_len(arg) == 0)
if (!cmd || ni_string_empty(arg))
return FALSE;

if (ni_string_array_append(&proc->argv, arg) < 0)
if (ni_string_array_append(&cmd->argv, arg) < 0)
return FALSE;

if (__ni_shellcmd_format(&proc->command, &proc->argv) == NULL)
if (__ni_shellcmd_format(&cmd->command, &cmd->argv) == NULL)
return FALSE;

return TRUE;
}

ni_shellcmd_t *
ni_shellcmd_hold(ni_shellcmd_t *cmd)
{
if (cmd) {
ni_assert(cmd->refcount);
cmd->refcount++;
return cmd;
}
return NULL;
}

void
ni_shellcmd_free(ni_shellcmd_t *cmd)
{
if (cmd) {
ni_assert(cmd->refcount);
cmd->refcount--;
if (cmd->refcount == 0)
__ni_shellcmd_free(cmd);
}
}


ni_process_t *
ni_process_new(ni_shellcmd_t *proc)
{
Expand Down
26 changes: 8 additions & 18 deletions src/process.h
Expand Up @@ -38,9 +38,16 @@ struct ni_process {
void * user_data;
};

extern ni_shellcmd_t * ni_shellcmd_new(const ni_string_array_t *argv);
extern ni_shellcmd_t * ni_shellcmd_new(const ni_string_array_t *args);
extern ni_shellcmd_t * ni_shellcmd_parse(const char *command);
extern ni_bool_t ni_shellcmd_add_arg(ni_shellcmd_t *, const char *);
extern ni_bool_t ni_shellcmd_fmt_arg(ni_shellcmd_t *, const char *, ...);
extern ni_shellcmd_t * ni_shellcmd_hold(ni_shellcmd_t *);
extern void ni_shellcmd_free(ni_shellcmd_t *);
static inline void ni_shellcmd_release(ni_shellcmd_t *cmd)
{
ni_shellcmd_free(cmd);
}

extern ni_process_t * ni_process_new(ni_shellcmd_t *);
extern int ni_process_run(ni_process_t *);
Expand All @@ -51,22 +58,5 @@ extern const char * ni_process_getenv(const ni_process_t *, const char *);
extern ni_tempstate_t * ni_process_tempstate(ni_process_t *);
extern void ni_process_free(ni_process_t *);
extern int ni_process_exit_status_okay(const ni_process_t *);
extern void ni_shellcmd_free(ni_shellcmd_t *);

static inline ni_shellcmd_t *
ni_shellcmd_hold(ni_shellcmd_t *proc)
{
ni_assert(proc->refcount);
proc->refcount++;
return proc;
}

static inline void
ni_shellcmd_release(ni_shellcmd_t *proc)
{
ni_assert(proc->refcount);
if (--(proc->refcount) == 0)
ni_shellcmd_free(proc);
}

#endif /* __WICKED_PROCESS_H__ */

0 comments on commit 2771722

Please sign in to comment.