Handle cleanup of locks properly

This commit is contained in:
Dan Walsh 2013-10-09 17:37:42 -04:00 committed by Stephen Smalley
parent 53ccfb3b4d
commit f2051b20fa

View File

@ -54,6 +54,7 @@ static const char *PATH="/org/selinux/Restorecond";
static const char *INTERFACE="org.selinux.RestorecondIface";
static const char *RULE="type='signal',interface='org.selinux.RestorecondIface'";
static int local_lock_fd = -1;
static DBusHandlerResult
signal_filter (DBusConnection *connection __attribute__ ((__unused__)), DBusMessage *message, void *user_data)
@ -201,17 +202,18 @@ static int local_server() {
perror("asprintf");
return -1;
}
int fd = open(ptr, O_CREAT | O_WRONLY | O_NOFOLLOW | O_CLOEXEC, S_IRUSR | S_IWUSR);
local_lock_fd = open(ptr, O_CREAT | O_WRONLY | O_NOFOLLOW | O_CLOEXEC, S_IRUSR | S_IWUSR);
if (debug_mode)
g_warning ("Lock file: %s", ptr);
free(ptr);
if (fd < 0) {
if (local_lock_fd < 0) {
if (debug_mode)
perror("open");
return -1;
}
if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
if (flock(local_lock_fd, LOCK_EX | LOCK_NB) < 0) {
close(local_lock_fd);
if (debug_mode)
perror("flock");
return -1;
@ -226,6 +228,12 @@ static int local_server() {
return 0;
}
static void end_local_server(void) {
if (local_lock_fd >= 0)
close(local_lock_fd);
local_lock_fd = -1;
}
int server(int master_fd, const char *watch_file) {
GMainLoop *loop;
@ -253,6 +261,7 @@ int server(int master_fd, const char *watch_file) {
g_main_loop_run (loop);
end:
end_local_server();
g_main_loop_unref (loop);
return 0;
}