Skip to content

Commit

Permalink
first attempt in implementinging execution logic
Browse files Browse the repository at this point in the history
  • Loading branch information
poettering committed Jan 23, 2010
1 parent cd2dbd7 commit 5cb5a6f
Show file tree
Hide file tree
Showing 33 changed files with 1,775 additions and 542 deletions.
26 changes: 24 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
CFLAGS=-Wall -Wextra -O0 -g -pipe -D_GNU_SOURCE -fdiagnostics-show-option -Wno-unused-parameter
LIBS=-lrt
LIBS=-lrt -lcap

COMMON=name.o util.o set.o hashmap.o strv.o job.o manager.o conf-parser.o load-fragment.o socket-util.o log.o
COMMON= \
name.o \
util.o \
set.o \
hashmap.o \
strv.o \
job.o \
manager.o \
conf-parser.o \
load-fragment.o \
socket-util.o \
log.o \
service.o \
automount.o \
mount.o \
device.o \
milestone.o \
snapshot.o \
socket.o \
timer.o \
load-fstab.o \
load-dropin.o \
execute.o

all: systemd test-engine test-job-type

Expand Down
111 changes: 111 additions & 0 deletions automount.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*-*- Mode: C; c-basic-offset: 8 -*-*/

#include <errno.h>

#include "name.h"
#include "automount.h"
#include "load-fragment.h"
#include "load-fstab.h"
#include "load-dropin.h"

static int automount_load(Name *n) {
int r;
Automount *a = AUTOMOUNT(n);

assert(a);

exec_context_defaults(&a->exec_context);

/* Load a .automount file */
if ((r = name_load_fragment(n)) < 0 && errno != -ENOENT)
return r;

/* Load entry from /etc/fstab */
if ((r = name_load_fstab(n)) < 0)
return r;

/* Load drop-in directory data */
if ((r = name_load_dropin(n)) < 0)
return r;

return 0;
}

static void automount_dump(Name *n, FILE *f, const char *prefix) {

static const char* const state_table[_AUTOMOUNT_STATE_MAX] = {
[AUTOMOUNT_DEAD] = "dead",
[AUTOMOUNT_START_PRE] = "start-pre",
[AUTOMOUNT_START_POST] = "start-post",
[AUTOMOUNT_WAITING] = "waiting",
[AUTOMOUNT_RUNNING] = "running",
[AUTOMOUNT_STOP_PRE] = "stop-pre",
[AUTOMOUNT_STOP_POST] = "stop-post",
[AUTOMOUNT_MAINTAINANCE] = "maintainance"
};

static const char* const command_table[_AUTOMOUNT_EXEC_MAX] = {
[AUTOMOUNT_EXEC_START_PRE] = "StartPre",
[AUTOMOUNT_EXEC_START_POST] = "StartPost",
[AUTOMOUNT_EXEC_STOP_PRE] = "StopPre",
[AUTOMOUNT_EXEC_STOP_POST] = "StopPost"
};

AutomountExecCommand c;
Automount *s = AUTOMOUNT(n);

assert(s);

fprintf(f,
"%sAutomount State: %s\n"
"%sPath: %s\n",
prefix, state_table[s->state],
prefix, s->path);

exec_context_dump(&s->exec_context, f, prefix);

for (c = 0; c < _AUTOMOUNT_EXEC_MAX; c++) {
ExecCommand *i;

LIST_FOREACH(i, s->exec_command[c])
fprintf(f, "%s%s: %s\n", prefix, command_table[c], i->path);
}
}

static NameActiveState automount_active_state(Name *n) {

static const NameActiveState table[_AUTOMOUNT_STATE_MAX] = {
[AUTOMOUNT_DEAD] = NAME_INACTIVE,
[AUTOMOUNT_START_PRE] = NAME_ACTIVATING,
[AUTOMOUNT_START_POST] = NAME_ACTIVATING,
[AUTOMOUNT_WAITING] = NAME_ACTIVE,
[AUTOMOUNT_RUNNING] = NAME_ACTIVE,
[AUTOMOUNT_STOP_PRE] = NAME_DEACTIVATING,
[AUTOMOUNT_STOP_POST] = NAME_DEACTIVATING,
[AUTOMOUNT_MAINTAINANCE] = NAME_INACTIVE,
};

return table[AUTOMOUNT(n)->state];
}

static void automount_free_hook(Name *n) {
Automount *d = AUTOMOUNT(n);

assert(d);
free(d->path);
}

const NameVTable automount_vtable = {
.suffix = ".mount",

.load = automount_load,
.dump = automount_dump,

.start = NULL,
.stop = NULL,
.reload = NULL,

.active_state = automount_active_state,

.free_hook = automount_free_hook
};
46 changes: 46 additions & 0 deletions automount.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*-*- Mode: C; c-basic-offset: 8 -*-*/

#ifndef fooautomounthfoo
#define fooautomounthfoo

typedef struct Automount Automount;

#include "name.h"

typedef enum AutomountState {
AUTOMOUNT_DEAD,
AUTOMOUNT_START_PRE,
AUTOMOUNT_START_POST,
AUTOMOUNT_WAITING,
AUTOMOUNT_RUNNING,
AUTOMOUNT_STOP_PRE,
AUTOMOUNT_STOP_POST,
AUTOMOUNT_MAINTAINANCE,
_AUTOMOUNT_STATE_MAX
} AutomountState;

typedef enum AutomountExecCommand {
AUTOMOUNT_EXEC_START_PRE,
AUTOMOUNT_EXEC_START_POST,
AUTOMOUNT_EXEC_STOP_PRE,
AUTOMOUNT_EXEC_STOP_POST,
_AUTOMOUNT_EXEC_MAX
} AutomountExecCommand;

struct Automount {
Meta meta;

AutomountState state;
char *path;

ExecCommand* exec_command[_AUTOMOUNT_EXEC_MAX];
ExecContext exec_context;

pid_t contol_pid;

Mount *mount;
};

extern const NameVTable automount_vtable;

#endif
47 changes: 47 additions & 0 deletions device.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*-*- Mode: C; c-basic-offset: 8 -*-*/

#include "name.h"
#include "device.h"
#include "strv.h"

static void device_dump(Name *n, FILE *f, const char *prefix) {

static const char* const state_table[_DEVICE_STATE_MAX] = {
[DEVICE_DEAD] = "dead",
[DEVICE_AVAILABLE] = "available"
};

Device *s = DEVICE(n);

assert(s);

fprintf(f,
"%sDevice State: %s\n",
prefix, state_table[s->state]);
}

static NameActiveState device_active_state(Name *n) {
return DEVICE(n)->state == DEVICE_DEAD ? NAME_INACTIVE : NAME_ACTIVE;
}

static void device_free_hook(Name *n) {
Device *d = DEVICE(n);

assert(d);
strv_free(d->sysfs);
}

const NameVTable device_vtable = {
.suffix = ".device",

.load = name_load_fragment_and_dropin,
.dump = device_dump,

.start = NULL,
.stop = NULL,
.reload = NULL,

.active_state = device_active_state,

.free_hook = device_free_hook
};
29 changes: 29 additions & 0 deletions device.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*-*- Mode: C; c-basic-offset: 8 -*-*/

#ifndef foodevicehfoo
#define foodevicehfoo

typedef struct Device Device;

#include "name.h"

/* We simply watch devices, we cannot plug/unplug them. That
* simplifies the state engine greatly */
typedef enum DeviceState {
DEVICE_DEAD,
DEVICE_AVAILABLE,
_DEVICE_STATE_MAX
} DeviceState;

struct Device {
Meta meta;

DeviceState state;

/* A single device can be created by multiple sysfs objects */
char **sysfs;
};

extern const NameVTable device_vtable;

#endif
68 changes: 68 additions & 0 deletions execute.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*-*- Mode: C; c-basic-offset: 8 -*-*/

#include <assert.h>

#include "execute.h"
#include "strv.h"
#include "macro.h"
#include "util.h"

int exec_spawn(const ExecCommand *command, const ExecContext *context, pid_t *ret) {
assert(command);
assert(context);
assert(ret);

return 0;
}

void exec_context_free(ExecContext *c) {
unsigned l;

assert(c);

strv_free(c->environment);

for (l = 0; l < ELEMENTSOF(c->rlimit); l++)
free(c->rlimit[l]);

free(c->chdir);
free(c->user);
free(c->group);
free(c->supplementary_groups);
}

void exec_command_free_list(ExecCommand *c) {
ExecCommand *i;

while ((i = c)) {
LIST_REMOVE(ExecCommand, c, i);

free(i->path);
free(i->argv);
free(i);
}
}

void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
assert(c);
assert(f);

if (!prefix)
prefix = "";

fprintf(f,
"%sUmask: %04o\n"
"%sDumpable: %s\n"
"%sDirectory: %s\n",
prefix, c->umask,
prefix, yes_no(c->dumpable),
prefix, c->chdir ? c->chdir : "/");
}

void exec_context_defaults(ExecContext *c) {
assert(c);

c->umask = 0002;
cap_clear(c->capabilities);
c->dumpable = true;
}
59 changes: 59 additions & 0 deletions execute.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*-*- Mode: C; c-basic-offset: 8 -*-*/

#ifndef fooexecutehfoo
#define fooexecutehfoo

typedef struct ExecStatus ExecStatus;
typedef struct ExecCommand ExecCommand;
typedef struct ExecContext ExecContext;

#include <sys/time.h>
#include <sys/resource.h>
#include <sys/capability.h>
#include <stdbool.h>
#include <stdio.h>

#include "list.h"

struct ExecStatus {
pid_t pid;
time_t timestamp;
int status; /* as in wait() */
};

struct ExecCommand {
char *path;
char **argv;
ExecStatus last_exec_status;
LIST_FIELDS(ExecCommand);
};

struct ExecContext {
char **environment;
mode_t umask;
struct rlimit *rlimit[RLIMIT_NLIMITS];
cap_t capabilities;
bool capabilities_set:1;
bool dumpable:1;
int oom_adjust;
int nice;
char *chdir;

/* since resolving these names might might involve socket
* connections and we don't want to deadlock ourselves these
* names are resolved on execution only. */
char *user;
char *group;
char **supplementary_groups;
};

int exec_spawn(const ExecCommand *command, const ExecContext *context, pid_t *ret);

void exec_context_free(ExecContext *c);
void exec_command_free_list(ExecCommand *c);

void exec_context_dump(ExecContext *c, FILE* f, const char *prefix);

void exec_context_defaults(ExecContext *c);

#endif
Loading

0 comments on commit 5cb5a6f

Please sign in to comment.