diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2017-12-04 14:59:44 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2017-12-04 14:59:44 +0000 |
commit | 1f72ed3900db6b43ecbde097c83c4c5ddf9c9e38 (patch) | |
tree | 628a75700f4a7d82d11d51b7963d986deb13af29 /src/libs6rc | |
parent | 2c44eb5822e7ab6281123290184df4542da5cdb1 (diff) | |
download | s6-rc-1f72ed3900db6b43ecbde097c83c4c5ddf9c9e38.tar.xz |
Refactor s6-rc-init and s6-rc-upgrade around atomic_symlink and s6rc_livedir_create
Diffstat (limited to 'src/libs6rc')
-rw-r--r-- | src/libs6rc/deps-lib/s6rc | 1 | ||||
-rw-r--r-- | src/libs6rc/s6rc_livedir_create.c | 50 |
2 files changed, 51 insertions, 0 deletions
diff --git a/src/libs6rc/deps-lib/s6rc b/src/libs6rc/deps-lib/s6rc index f42783b..52c220f 100644 --- a/src/libs6rc/deps-lib/s6rc +++ b/src/libs6rc/deps-lib/s6rc @@ -5,6 +5,7 @@ s6rc_db_read.o s6rc_db_read_sizes.o s6rc_db_read_uint32.o s6rc_graph_closure.o +s6rc_livedir_create.o s6rc_livedir_prefix.o s6rc_livedir_prefixsize.o s6rc_lock.o diff --git a/src/libs6rc/s6rc_livedir_create.c b/src/libs6rc/s6rc_livedir_create.c new file mode 100644 index 0000000..1da0ac3 --- /dev/null +++ b/src/libs6rc/s6rc_livedir_create.c @@ -0,0 +1,50 @@ +/* ISC license. */ + +#include <sys/stat.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <stdlib.h> /* mkdtemp */ +#include <skalibs/stralloc.h> +#include <skalibs/djbunix.h> +#include <s6-rc/s6rc-utils.h> + +int s6rc_livedir_create (stralloc *sa, char const *live, char const *suffix, char const *scandir, char const *prefix, char const *compiled, unsigned char const *state, unsigned int statelen, size_t *dirlen) +{ + size_t newlen, ddirlen ; + size_t sabase = sa->len ; + int wasnull = !sa->s ; + if (!s6rc_sanitize_dir(sa, live, &ddirlen)) return 0 ; + if (!stralloc_cats(sa, ":")) goto err ; + if (!stralloc_cats(sa, suffix)) goto err ; + if (!stralloc_cats(sa, ":XXXXXX")) goto err ; + if (!mkdtemp(sa->s + sabase)) goto err ; + newlen = sa->len-- ; + if (chmod(sa->s + sabase, 0755) < 0) goto delerr ; + if (!stralloc_catb(sa, "/servicedirs", 13)) goto delerr ; /* allocates enough for the next strcpys */ + if (mkdir(sa->s + sabase, 0755) < 0) goto delerr ; + strcpy(sa->s + newlen, "compiled") ; + if (symlink(compiled, sa->s + sabase) < 0) goto delerr ; + strcpy(sa->s + newlen, "scandir") ; + if (symlink(scandir, sa->s + sabase) < 0) goto delerr ; + strcpy(sa->s + newlen, "prefix") ; + if (!openwritenclose_unsafe(sa->s + sabase, prefix, strlen(prefix))) goto delerr ; + strcpy(sa->s + newlen, "state") ; + if (!openwritenclose_unsafe(sa->s + sabase, (char const *)state, statelen)) goto delerr ; + sa->len = newlen ; + sa->s[newlen] = 0 ; + *dirlen = ddirlen ; + return 1 ; + + delerr: + { + int e = errno ; + sa->s[newlen] = 0 ; + rm_rf_in_tmp(sa, sabase) ; + errno = e ; + } + err: + if (wasnull) stralloc_free(sa) ; + else sa->len = sabase ; + return 0 ; +} |