summaryrefslogtreecommitdiffstats
path: root/src/lock.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lock.c')
-rw-r--r--src/lock.c22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/lock.c b/src/lock.c
index 42ad213..efce0bb 100644
--- a/src/lock.c
+++ b/src/lock.c
@@ -8,6 +8,7 @@
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
+#include <pwd.h>
#include <libdaemon/dlog.h>
@@ -35,6 +36,7 @@ int device_lock(const char *dev) {
int fd;
const char *path, *temp;
char buf[100];
+ char uidbuf[32];
if (stat(LOCK_PATH, &st) != 0 || !S_ISDIR(st.st_mode)) {
daemon_log(LOG_ERR, "Failed to lock device, directory "LOCK_PATH" not existent.");
@@ -45,6 +47,9 @@ int device_lock(const char *dev) {
temp = tempfile(path);
for (;;) {
+ mode_t u;
+ struct passwd* pw;
+ char *username;
if ((fd = open(path, O_RDONLY)) < 0) {
if (errno != ENOENT) {
@@ -87,18 +92,33 @@ int device_lock(const char *dev) {
}
}
}
+
+ u = umask(0033);
+ fd = open(temp, O_WRONLY | O_CREAT | O_EXCL, 0666);
+ umask(u);
- if ((fd = open(temp, O_WRONLY | O_CREAT | O_EXCL, 0666)) < 0) {
+ if (fd < 0) {
daemon_log(LOG_ERR, "Failed to create temporary lock file: %s", strerror(errno));
return -1;
}
+ if ((pw = getpwuid(target_uid)))
+ username = pw->pw_name;
+ else
+ snprintf(username = uidbuf, sizeof(uidbuf), "%lu", (unsigned long) target_uid);
+
snprintf(buf, sizeof(buf), "%10lu %s %.20s\n", (unsigned long) getpid(), appname, username);
if (loop_write(fd, buf, strlen(buf)) < 0) {
daemon_log(LOG_ERR, "Failed to write to temporary lock file: %s", strerror(errno));
close(fd);
return -1;
}
+
+ if (fchown(fd, target_uid, target_gid) < 0) {
+ daemon_log(LOG_ERR, "Failed to change owners of temporary lock file: %s", strerror(errno));
+ close(fd);
+ return -1;
+ }
close(fd);