diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2017-07-12 18:22:55 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2017-07-12 18:22:55 +0000 |
commit | f7c2e436ed0144d09cff0e3ac90f510a47f0aeac (patch) | |
tree | 2306f029b468623ed316ac3df40a4df8d13c5c5e /src | |
parent | f80dbef73f98ae1a801078e5f69a071651e6b52d (diff) | |
download | s6-f7c2e436ed0144d09cff0e3ac90f510a47f0aeac.tar.xz |
Add s6_svc_lock_take() and s6_svc_lock_release()
Allows fixing a race condition in s6-rc-init.
s6-supervise also creates event/ before supervise/ so that
locking the servicedir ensures event/ can be used.
Diffstat (limited to 'src')
-rw-r--r-- | src/include/s6/s6-supervise.h | 4 | ||||
-rw-r--r-- | src/libs6/deps-lib/s6 | 1 | ||||
-rw-r--r-- | src/libs6/s6_svc_lock_take.c | 28 | ||||
-rw-r--r-- | src/supervision/s6-supervise.c | 2 |
4 files changed, 34 insertions, 1 deletions
diff --git a/src/include/s6/s6-supervise.h b/src/include/s6/s6-supervise.h index f0b08e7..541c30d 100644 --- a/src/include/s6/s6-supervise.h +++ b/src/include/s6/s6-supervise.h @@ -5,6 +5,7 @@ #include <sys/types.h> #include <skalibs/tai.h> +#include <skalibs/djbunix.h> #define S6_SUPERVISE_CTLDIR "supervise" #define S6_SUPERVISE_EVENTDIR "event" @@ -17,6 +18,9 @@ extern int s6_svc_write (char const *, char const *, size_t) ; extern int s6_svc_writectl (char const *, char const *, char const *, size_t) ; extern int s6_svc_main (int, char const *const *, char const *, char const *, char const *) ; +extern int s6_svc_lock_take (char const *) ; +#define s6_svc_lock_release(fdlock) lock_un(fdlock) + typedef struct s6_svstatus_s s6_svstatus_t, *s6_svstatus_t_ref ; struct s6_svstatus_s { diff --git a/src/libs6/deps-lib/s6 b/src/libs6/deps-lib/s6 index ace5a05..5acb672 100644 --- a/src/libs6/deps-lib/s6 +++ b/src/libs6/deps-lib/s6 @@ -26,6 +26,7 @@ s6_accessrules_uidgid_cdb.o s6_accessrules_uidgid_fs.o s6_supervise_lock.o s6_supervise_lock_mode.o +s6_svc_lock_take.o s6_svc_ok.o s6_svc_write.o s6_svc_writectl.o diff --git a/src/libs6/s6_svc_lock_take.c b/src/libs6/s6_svc_lock_take.c new file mode 100644 index 0000000..57830c0 --- /dev/null +++ b/src/libs6/s6_svc_lock_take.c @@ -0,0 +1,28 @@ +/* ISC license. */ + +#include <sys/stat.h> +#include <string.h> +#include <errno.h> +#include <skalibs/djbunix.h> +#include <s6/s6-supervise.h> + +int s6_svc_lock_take (char const *dir) +{ + size_t dirlen = strlen(dir) ; + int fdlock ; + char lock[dirlen + sizeof(S6_SUPERVISE_CTLDIR) + 6] ; + memcpy(lock, dir, dirlen) ; + memcpy(lock + dirlen, "/" S6_SUPERVISE_CTLDIR, sizeof(S6_SUPERVISE_CTLDIR) + 1) ; + if ((mkdir(lock, S_IRWXU) < 0) && (errno != EEXIST)) return -1 ; + memcpy(lock + dirlen + sizeof(S6_SUPERVISE_CTLDIR), "/lock", 6) ; + fdlock = open_create(lock) ; + if (fdlock < 0) return -1 ; + if (coe(fdlock) < 0 || lock_ex(fdlock) < 0) + { + int e = errno ; + fd_close(fdlock) ; + errno = e ; + return -1 ; + } + return fdlock ; +} diff --git a/src/supervision/s6-supervise.c b/src/supervision/s6-supervise.c index 7527668..71f247e 100644 --- a/src/supervision/s6-supervise.c +++ b/src/supervision/s6-supervise.c @@ -599,9 +599,9 @@ int main (int argc, char const *const *argv) memcpy(progname + proglen + 1, argv[1], namelen + 1) ; PROG = progname ; if (!fd_sanitize()) strerr_diefu1sys(111, "sanitize stdin and stdout") ; - x[1].fd = s6_supervise_lock(S6_SUPERVISE_CTLDIR) ; if (!ftrigw_fifodir_make(S6_SUPERVISE_EVENTDIR, getegid(), 0)) strerr_diefu2sys(111, "mkfifodir ", S6_SUPERVISE_EVENTDIR) ; + x[1].fd = s6_supervise_lock(S6_SUPERVISE_CTLDIR) ; x[0].fd = selfpipe_init() ; if (x[0].fd == -1) strerr_diefu1sys(111, "init selfpipe") ; if (sig_ignore(SIGPIPE) < 0) strerr_diefu1sys(111, "ignore SIGPIPE") ; |