From: 2xsec Date: Fri, 13 Jul 2018 12:33:09 +0000 (+0900) Subject: lxclock: change error log using strerror to SYSERROR X-Git-Tag: lxc-2.0.10~51 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2e264c8572f4b71eaef1c999c8b7c292f9e7ace3;p=thirdparty%2Flxc.git lxclock: change error log using strerror to SYSERROR Signed-off-by: 2xsec --- diff --git a/src/lxc/lxclock.c b/src/lxc/lxclock.c index 08c96e0b1..2e2b54538 100644 --- a/src/lxc/lxclock.c +++ b/src/lxc/lxclock.c @@ -75,7 +75,7 @@ static void lock_mutex(pthread_mutex_t *l) ret = pthread_mutex_lock(l); if (ret != 0) { - fprintf(stderr, "%s - Failed acquire mutex", strerror(ret)); + SYSERROR("Failed to acquire mutex"); dump_stacktrace(); _exit(EXIT_FAILURE); } @@ -87,7 +87,7 @@ static void unlock_mutex(pthread_mutex_t *l) ret = pthread_mutex_unlock(l); if (ret != 0) { - fprintf(stderr, "%s - Failed to release mutex", strerror(ret)); + SYSERROR("Failed to release mutex"); dump_stacktrace(); _exit(EXIT_FAILURE); } @@ -111,6 +111,7 @@ static char *lxclock_name(const char *p, const char *n) rundir = get_rundir(); if (!rundir) return NULL; + len += strlen(rundir); if ((dest = malloc(len)) == NULL) { @@ -124,6 +125,7 @@ static char *lxclock_name(const char *p, const char *n) free(rundir); return NULL; } + ret = mkdir_p(dest, 0755); if (ret < 0) { free(dest); @@ -137,6 +139,7 @@ static char *lxclock_name(const char *p, const char *n) free(dest); return NULL; } + return dest; } @@ -148,11 +151,13 @@ static sem_t *lxc_new_unnamed_sem(void) s = malloc(sizeof(*s)); if (!s) return NULL; + ret = sem_init(s, 0, 1); if (ret) { free(s); return NULL; } + return s; } @@ -171,6 +176,7 @@ struct lxc_lock *lxc_newlock(const char *lxcpath, const char *name) free(l); l = NULL; } + goto out; } @@ -181,6 +187,7 @@ struct lxc_lock *lxc_newlock(const char *lxcpath, const char *name) l = NULL; goto out; } + l->u.f.fd = -1; out: @@ -200,10 +207,12 @@ int lxclock(struct lxc_lock *l, int timeout) saved_errno = errno; } else { struct timespec ts; + if (clock_gettime(CLOCK_REALTIME, &ts) == -1) { ret = -2; goto out; } + ts.tv_sec += timeout; ret = sem_timedwait(l->u.sem, &ts); if (ret < 0) @@ -216,10 +225,12 @@ int lxclock(struct lxc_lock *l, int timeout) ERROR("Error: timeout not supported with flock"); goto out; } + if (!l->u.f.fname) { ERROR("Error: filename not set for flock"); goto out; } + if (l->u.f.fd == -1) { l->u.f.fd = open(l->u.f.fname, O_CREAT | O_RDWR | O_NOFOLLOW | O_CLOEXEC | O_NOCTTY, S_IWUSR | S_IRUSR); if (l->u.f.fd == -1) { @@ -228,9 +239,12 @@ int lxclock(struct lxc_lock *l, int timeout) goto out; } } + memset(&lk, 0, sizeof(struct flock)); + lk.l_type = F_WRLCK; lk.l_whence = SEEK_SET; + ret = fcntl(l->u.f.fd, F_OFD_SETLKW, &lk); if (ret < 0) { if (errno == EINVAL) @@ -262,14 +276,17 @@ int lxcunlock(struct lxc_lock *l) case LXC_LOCK_FLOCK: if (l->u.f.fd != -1) { memset(&lk, 0, sizeof(struct flock)); + lk.l_type = F_UNLCK; lk.l_whence = SEEK_SET; + ret = fcntl(l->u.f.fd, F_OFD_SETLK, &lk); if (ret < 0) { if (errno == EINVAL) ret = flock(l->u.f.fd, LOCK_EX | LOCK_NB); saved_errno = errno; } + close(l->u.f.fd); l->u.f.fd = -1; } else @@ -292,6 +309,7 @@ void lxc_putlock(struct lxc_lock *l) { if (!l) return; + switch(l->type) { case LXC_LOCK_ANON_SEM: if (l->u.sem) { @@ -305,6 +323,7 @@ void lxc_putlock(struct lxc_lock *l) close(l->u.f.fd); l->u.f.fd = -1; } + free(l->u.f.fname); l->u.f.fname = NULL; break; @@ -338,10 +357,12 @@ int container_disk_lock(struct lxc_container *c) if ((ret = lxclock(c->privlock, 0))) return ret; + if ((ret = lxclock(c->slock, 0))) { lxcunlock(c->privlock); return ret; } + return 0; }