mirror of
https://github.com/troglobit/finit.git
synced 2026-11-22 20:01:46 +00:00
4 files changed
+76
-18
No files matched your search
+47
-4
@@ -25,16 +25,51 @@
|
|||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
#include <lite/lite.h>
|
#include <lite/lite.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
|
|
||||||
#include "finit.h"
|
#include "finit.h"
|
||||||
#include "cond.h"
|
#include "cond.h"
|
||||||
#include "service.h"
|
#include "service.h"
|
||||||
|
|
||||||
|
static int cond_set_gen(const char *path, unsigned int gen)
|
||||||
|
{
|
||||||
|
FILE *fp;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
fp = fopen(path, "w");
|
||||||
|
if (!fp)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
ret = fprintf(fp, "%u", gen);
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
return (ret > 0) ? 0 : ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void cond_bump_reconf(void)
|
||||||
|
{
|
||||||
|
unsigned int rgen;
|
||||||
|
|
||||||
|
rgen = cond_get_gen(COND_RECONF);
|
||||||
|
/* COND_RECONF does not exist, cond_get_gen will return 0
|
||||||
|
* meaning that rgen++ is always what we want. */
|
||||||
|
rgen++;
|
||||||
|
|
||||||
|
cond_set_gen(COND_RECONF, rgen);
|
||||||
|
}
|
||||||
|
|
||||||
int cond_set_path(const char *path, enum cond_state new)
|
int cond_set_path(const char *path, enum cond_state new)
|
||||||
{
|
{
|
||||||
char buf[MAX_ARG_LEN], *dir;
|
char buf[MAX_ARG_LEN], *dir;
|
||||||
enum cond_state old;
|
enum cond_state old;
|
||||||
|
unsigned int rgen;
|
||||||
|
|
||||||
|
_d("%s", path);
|
||||||
|
|
||||||
|
rgen = cond_get_gen(COND_RECONF);
|
||||||
|
if (!rgen) {
|
||||||
|
_e("Unable to read configuration generation (%s)", path);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
old = cond_get_path(path);
|
old = cond_get_path(path);
|
||||||
|
|
||||||
@@ -50,8 +85,7 @@ int cond_set_path(const char *path, enum cond_state new)
|
|||||||
_pe("Failed creating dir '%s' for condition '%s'", dir, path);
|
_pe("Failed creating dir '%s' for condition '%s'", dir, path);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
touch(path);
|
cond_set_gen(path, rgen);
|
||||||
utimensat(0, path, NULL, 0);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case COND_OFF:
|
case COND_OFF:
|
||||||
@@ -121,8 +155,8 @@ void cond_clear(const char *name)
|
|||||||
void cond_reload(void)
|
void cond_reload(void)
|
||||||
{
|
{
|
||||||
_d("");
|
_d("");
|
||||||
cond_set_path(COND_RECONF, COND_ON);
|
|
||||||
|
|
||||||
|
cond_bump_reconf();
|
||||||
cond_update(NULL);
|
cond_update(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,6 +187,15 @@ void cond_reassert(const char *type)
|
|||||||
nftw(cond_path(type), reassert, 20, FTW_DEPTH);
|
nftw(cond_path(type), reassert, 20, FTW_DEPTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void cond_init(void)
|
||||||
|
{
|
||||||
|
if (makepath(COND_PATH) && errno != EEXIST) {
|
||||||
|
_pe("Failed creating condition base directory '%s'", COND_PATH);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cond_bump_reconf();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
|
|||||||
+23
-14
@@ -28,14 +28,6 @@
|
|||||||
#include "cond.h"
|
#include "cond.h"
|
||||||
#include "service.h"
|
#include "service.h"
|
||||||
|
|
||||||
static inline int timespec_newer(const struct timespec *a,
|
|
||||||
const struct timespec *b)
|
|
||||||
{
|
|
||||||
if (a->tv_sec != b->tv_sec)
|
|
||||||
return a->tv_sec > b->tv_sec;
|
|
||||||
|
|
||||||
return a->tv_nsec >= b->tv_nsec;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *condstr(enum cond_state s)
|
const char *condstr(enum cond_state s)
|
||||||
{
|
{
|
||||||
@@ -57,18 +49,35 @@ const char *cond_path(const char *name)
|
|||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned int cond_get_gen(const char *path)
|
||||||
|
{
|
||||||
|
unsigned int gen;
|
||||||
|
FILE *fp;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
fp = fopen(path, "r");
|
||||||
|
if (!fp)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
ret = fscanf(fp, "%u", &gen);
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
return (ret == 1) ? gen : 0;
|
||||||
|
}
|
||||||
|
|
||||||
enum cond_state cond_get_path(const char *path)
|
enum cond_state cond_get_path(const char *path)
|
||||||
{
|
{
|
||||||
struct stat st, st_reconf;
|
int cgen, rgen;
|
||||||
|
|
||||||
if (stat(path, &st))
|
rgen = cond_get_gen(COND_RECONF);
|
||||||
|
if (!rgen)
|
||||||
return COND_OFF;
|
return COND_OFF;
|
||||||
|
|
||||||
if (stat(COND_RECONF, &st_reconf) ||
|
cgen = cond_get_gen(path);
|
||||||
timespec_newer(&st.st_mtim, &st_reconf.st_mtim))
|
if (!cgen)
|
||||||
return COND_ON;
|
return COND_OFF;
|
||||||
|
|
||||||
return COND_FLUX;
|
return (cgen == rgen) ? COND_ON : COND_FLUX;
|
||||||
}
|
}
|
||||||
|
|
||||||
enum cond_state cond_get(const char *name)
|
enum cond_state cond_get(const char *name)
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ typedef enum cond_state {
|
|||||||
|
|
||||||
const char *condstr (enum cond_state s);
|
const char *condstr (enum cond_state s);
|
||||||
const char *cond_path (const char *name);
|
const char *cond_path (const char *name);
|
||||||
|
unsigned int cond_get_gen (const char *path);
|
||||||
enum cond_state cond_get_path(const char *path);
|
enum cond_state cond_get_path(const char *path);
|
||||||
enum cond_state cond_get (const char *name);
|
enum cond_state cond_get (const char *name);
|
||||||
enum cond_state cond_get_agg (const char *names);
|
enum cond_state cond_get_agg (const char *names);
|
||||||
@@ -25,6 +26,7 @@ void cond_set (const char *name);
|
|||||||
void cond_clear (const char *name);
|
void cond_clear (const char *name);
|
||||||
void cond_reload (void);
|
void cond_reload (void);
|
||||||
void cond_reassert(const char *pat);
|
void cond_reassert(const char *pat);
|
||||||
|
void cond_init (void);
|
||||||
|
|
||||||
#endif /* FINIT_COND_H_ */
|
#endif /* FINIT_COND_H_ */
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,7 @@
|
|||||||
#include <lite/lite.h>
|
#include <lite/lite.h>
|
||||||
|
|
||||||
#include "finit.h"
|
#include "finit.h"
|
||||||
|
#include "cond.h"
|
||||||
#include "conf.h"
|
#include "conf.h"
|
||||||
#include "helpers.h"
|
#include "helpers.h"
|
||||||
#include "private.h"
|
#include "private.h"
|
||||||
@@ -486,6 +487,9 @@ int main(int argc, char* argv[])
|
|||||||
/* Base FS up, enable standard SysV init signals */
|
/* Base FS up, enable standard SysV init signals */
|
||||||
sig_setup(&loop);
|
sig_setup(&loop);
|
||||||
|
|
||||||
|
/* Bootstrap conditions */
|
||||||
|
cond_init();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Set up inotify watcher for /etc/finit.d and read all .conf
|
* Set up inotify watcher for /etc/finit.d and read all .conf
|
||||||
* files to figure out how to bootstrap the system.
|
* files to figure out how to bootstrap the system.
|
||||||
|
|||||||
Reference in new issue
Block a user