From 70d784a0a179307399445c3847bf60f06782adb3 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 21 Apr 2015 03:12:20 +0200 Subject: fix bus creating bugs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/bus.c | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/bus.c b/src/bus.c index 5607dbf..5875b2b 100644 --- a/src/bus.c +++ b/src/bus.c @@ -353,6 +353,38 @@ randomchar(void) } +/** + * Basically, this is `mkdir -p -m $mode $pathname` + * + * @param pathname The pathname of the directory to create if missing + * @param mode The permission bits of any created directory + * @return 0 on sucess, -1 on error + */ +static int +mkdirs(char *pathname, mode_t mode) +{ + size_t i, n = strlen(pathname); + char c; + for (i = 0; i < n; i++) + if (pathname[i] != '/') + break; + for (; i < n; i++) { + if (pathname[i] == '/') { + c = pathname[i]; + if (access(pathname, F_OK)) + if (mkdir(pathname, mode) < 0) + return -1; + pathname[i] = c; + break; + } + } + if (access(pathname, F_OK)) + if (mkdir(pathname, mode) < 0) + return -1; + return 0; +} + + /** * Create a new bus @@ -387,7 +419,7 @@ bus_create(const char *file, int flags, char **out_file) srand((unsigned int)time(NULL) + (unsigned int)rand()); if (file) { - fd = open(file, O_CREAT | O_EXCL); + fd = open(file, O_WRONLY | O_CREAT | O_EXCL, 0600); if (fd == -1) { if ((errno != EEXIST) || (flags & BUS_EXCL)) return -1; @@ -402,13 +434,15 @@ bus_create(const char *file, int flags, char **out_file) goto fail; if (out_file) *out_file = genfile; - sprintf(genfile, "%s/run/random.", env); + sprintf(genfile, "%s/bus", env); + t(mkdirs(genfile, 0755)); + sprintf(genfile, "%s/bus/random.", env); len = strlen(genfile); genfile[len + 30] = '\0'; retry: for (ptr = 0; ptr < 30; ptr++) genfile[len + ptr] = randomchar(); - fd = open(genfile, O_CREAT | O_EXCL); + fd = open(genfile, O_WRONLY | O_CREAT | O_EXCL, 0600); if (fd == -1) { if (errno == EEXIST) goto retry; @@ -423,7 +457,7 @@ bus_create(const char *file, int flags, char **out_file) for (len = strlen(buf), ptr = 0; ptr < len;) { wrote = write(fd, buf + ptr, len - ptr); if (wrote < 0) { - if ((errno == EINTR) && (flags & BUS_INTR)) + if ((errno != EINTR) || (flags & BUS_INTR)) goto fail; } else { ptr += (size_t)wrote; -- cgit v1.2.3-70-g09d2