Skip to contentSkip to conversation

restorecond: Add -F for run in foreground

PATCH In review Petr Lautrbach 3 participants 5 messages Apr 7 – May 13 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 restorecond: Add -F for run in foreground

---
 restorecond/restorecond.8 |  5 ++++-
 restorecond/restorecond.c | 13 ++++++++-----
 2 files changed, 12 insertions(+), 6 deletions(-)
restorecond/restorecond.8 +4 -1
index bf8ec87562f8..bb99b24a2d69 100644
@@ -3,7 +3,7 @@
 restorecond \- daemon that watches for file creation and then sets the default SELinux file context
 
 .SH "SYNOPSIS"
-.B restorecond  [\-d] [-h] [\-f restorecond_file ] [\-u] [\-v]
+.B restorecond  [\-d] [-h] [\-f restorecond_file ] [\-F] [\-u] [\-v]
 .P
 
 .SH "DESCRIPTION"
@@ -26,6 +26,9 @@ Print usage statement.
 .B \-f restorecond_file
 Use alternative restorecond.conf file.
 .TP
+.B \-F
+Run in foreground, do not become a daemon.
+.TP
 .B \-u
 Turns on user mode.  Runs restorecond in the user session and reads /etc/selinux/restorecond_user.conf.  Uses dbus to make sure only one restorecond is running per user session.
 .TP
restorecond/restorecond.c +8 -5
index d5f70fc2e2c1..36f82ae5e9cb 100644
@@ -76,6 +76,7 @@ int debug_mode = 0;
 int terminate = 0;
 int master_wd = -1;
 int run_as_user = 0;
+int foreground_mode = 0;
 
 static void done(void) {
 	watch_list_free(master_fd);
@@ -124,7 +125,7 @@ static void term_handler(int s __attribute__ ((unused)))
 
 static void usage(char *program)
 {
-	printf("%s [-d] [-f restorecond_file ] [-u] [-v] \n", program);
+	printf("%s [-d] [-f restorecond_file ] [-F] [-n] [-u] [-v] \n", program);
 }
 
 void exitApp(const char *msg)
@@ -165,7 +166,7 @@ int main(int argc, char **argv)
 	sigaction(SIGTERM, &sa, NULL);
 
 	atexit( done );
-	while ((opt = getopt(argc, argv, "hdf:uv")) > 0) {
+	while ((opt = getopt(argc, argv, "hdf:Fuv")) > 0) {
 		switch (opt) {
 		case 'd':
 			debug_mode = 1;
@@ -173,6 +174,9 @@ int main(int argc, char **argv)
 		case 'f':
 			watch_file = optarg;
 			break;
+		case 'F':
+			foreground_mode = 1;
+			break;
 		case 'u':
 			run_as_user = 1;
 			break;
@@ -209,13 +213,12 @@ int main(int argc, char **argv)
 
 	read_config(master_fd, watch_file);
 
-	if (!debug_mode) {
+	if (!debug_mode && !foreground_mode) {
 		if (daemon(0, 0) < 0)
 			exitApp("daemon");
+		write_pid_file();
 	}
 
-	write_pid_file();
-
 	while (watch(master_fd, watch_file) == 0) {
 	}
 
Signature · 1 line
2.53.0
#1 lore raw

PATCH 2/2 restorecond.service: Use Type=simple

There's a race condition when daemon() in parent process immediately exits while pid file is not created or updated. Using Type=forking it can confuse systemd which tries to open non-existing pid file. It's better to run restocond in foreground and use Type=simple

Fixes:

    openat(80</run>, "restorecond.pid", O_RDONLY|O_NOFOLLOW|O_CLOEXEC|O_PATH) = -1 ENOENT (No such file or directory) <0.000006>
---
 restorecond/restorecond.service | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
restorecond/restorecond.service +2 -3
index 0e4ea72d08ae..56f970707d93 100644
@@ -5,9 +5,8 @@ ConditionPathExists=/etc/selinux/restorecond.conf
 ConditionSecurity=selinux
 
 [Service]
-Type=forking
-ExecStart=/usr/sbin/restorecond
-PIDFile=/run/restorecond.pid
+Type=simple
+ExecStart=/usr/sbin/restorecond -F
 
 [Install]
 WantedBy=multi-user.target
Signature · 1 line
2.53.0
maintainer Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] restorecond.service: Use Type=simple

Quoted 11 lines · Petr Lautrbach

There's a race condition when daemon() in parent process immediately exits while pid file is not created or updated. Using Type=forking it can confuse systemd which tries to open non-existing pid file. It's better to run restocond in foreground and use Type=simple

Fixes:

    openat(80</run>, "restorecond.pid", O_RDONLY|O_NOFOLLOW|O_CLOEXEC|O_PATH) = -1 ENOENT (No such file or directory) <0.000006>
Quoted 24 lines
---
 restorecond/restorecond.service | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
restorecond/restorecond.service +2 -4
index 0e4ea72d08ae..56f970707d93 100644
@@ -5,9 +5,8 @@ ConditionPathExists=/etc/selinux/restorecond.conf
 ConditionSecurity=selinux
 
 [Service]
-Type=forking
-ExecStart=/usr/sbin/restorecond
-PIDFile=/run/restorecond.pid
+Type=simple
+ExecStart=/usr/sbin/restorecond -F
 
 [Install]
 WantedBy=multi-user.target
--

2.53.0

maintainer Acked-by on v1 1/2
#3 lore raw
In reply to Petr Lautrbach
Quoted 2 lines · Petr Lautrbach
Quoted 88 lines
---
 restorecond/restorecond.8 |  5 ++++-
 restorecond/restorecond.c | 13 ++++++++-----
 2 files changed, 12 insertions(+), 6 deletions(-)
restorecond/restorecond.8 +4 -1
index bf8ec87562f8..bb99b24a2d69 100644
@@ -3,7 +3,7 @@
 restorecond \- daemon that watches for file creation and then sets the default SELinux file context
 
 .SH "SYNOPSIS"
-.B restorecond  [\-d] [-h] [\-f restorecond_file ] [\-u] [\-v]
+.B restorecond  [\-d] [-h] [\-f restorecond_file ] [\-F] [\-u] [\-v]
 .P
 
 .SH "DESCRIPTION"
@@ -26,6 +26,9 @@ Print usage statement.
 .B \-f restorecond_file
 Use alternative restorecond.conf file.
 .TP
+.B \-F
+Run in foreground, do not become a daemon.
+.TP
 .B \-u
 Turns on user mode.  Runs restorecond in the user session and reads /etc/selinux/restorecond_user.conf.  Uses dbus to make sure only one restorecond is running per user session.
 .TP
restorecond/restorecond.c +8 -6
index d5f70fc2e2c1..36f82ae5e9cb 100644
@@ -76,6 +76,7 @@ int debug_mode = 0;
 int terminate = 0;
 int master_wd = -1;
 int run_as_user = 0;
+int foreground_mode = 0;
 
 static void done(void) {
        watch_list_free(master_fd);
@@ -124,7 +125,7 @@ static void term_handler(int s __attribute__ ((unused)))
 
 static void usage(char *program)
 {
-       printf("%s [-d] [-f restorecond_file ] [-u] [-v] \n", program);
+       printf("%s [-d] [-f restorecond_file ] [-F] [-n] [-u] [-v] \n", program);
 }
 
 void exitApp(const char *msg)
@@ -165,7 +166,7 @@ int main(int argc, char **argv)
        sigaction(SIGTERM, &sa, NULL);
 
        atexit( done );
-       while ((opt = getopt(argc, argv, "hdf:uv")) > 0) {
+       while ((opt = getopt(argc, argv, "hdf:Fuv")) > 0) {
                switch (opt) {
                case 'd':
                        debug_mode = 1;
@@ -173,6 +174,9 @@ int main(int argc, char **argv)
                case 'f':
                        watch_file = optarg;
                        break;
+               case 'F':
+                       foreground_mode = 1;
+                       break;
                case 'u':
                        run_as_user = 1;
                        break;
@@ -209,13 +213,12 @@ int main(int argc, char **argv)
 
        read_config(master_fd, watch_file);
 
-       if (!debug_mode) {
+       if (!debug_mode && !foreground_mode) {
                if (daemon(0, 0) < 0)
                        exitApp("daemon");
+               write_pid_file();
        }
 
-       write_pid_file();
-
        while (watch(master_fd, watch_file) == 0) {
        }
 
--

2.53.0

#4 lore raw
Quoted 7 lines · Stephen Smalley
Quoted 2 lines · Petr Lautrbach

These two patches have been merged.
Thanks,
Jim

Quoted 89 lines
Quoted 88 lines
---
 restorecond/restorecond.8 |  5 ++++-
 restorecond/restorecond.c | 13 ++++++++-----
 2 files changed, 12 insertions(+), 6 deletions(-)
restorecond/restorecond.8 +4 -1
index bf8ec87562f8..bb99b24a2d69 100644
@@ -3,7 +3,7 @@
 restorecond \- daemon that watches for file creation and then sets the default SELinux file context
 
 .SH "SYNOPSIS"
-.B restorecond  [\-d] [-h] [\-f restorecond_file ] [\-u] [\-v]
+.B restorecond  [\-d] [-h] [\-f restorecond_file ] [\-F] [\-u] [\-v]
 .P
 
 .SH "DESCRIPTION"
@@ -26,6 +26,9 @@ Print usage statement.
 .B \-f restorecond_file
 Use alternative restorecond.conf file.
 .TP
+.B \-F
+Run in foreground, do not become a daemon.
+.TP
 .B \-u
 Turns on user mode.  Runs restorecond in the user session and reads /etc/selinux/restorecond_user.conf.  Uses dbus to make sure only one restorecond is running per user session.
 .TP
restorecond/restorecond.c +8 -6
index d5f70fc2e2c1..36f82ae5e9cb 100644
@@ -76,6 +76,7 @@ int debug_mode = 0;
 int terminate = 0;
 int master_wd = -1;
 int run_as_user = 0;
+int foreground_mode = 0;
 
 static void done(void) {
        watch_list_free(master_fd);
@@ -124,7 +125,7 @@ static void term_handler(int s __attribute__ ((unused)))
 
 static void usage(char *program)
 {
-       printf("%s [-d] [-f restorecond_file ] [-u] [-v] \n", program);
+       printf("%s [-d] [-f restorecond_file ] [-F] [-n] [-u] [-v] \n", program);
 }
 
 void exitApp(const char *msg)
@@ -165,7 +166,7 @@ int main(int argc, char **argv)
        sigaction(SIGTERM, &sa, NULL);
 
        atexit( done );
-       while ((opt = getopt(argc, argv, "hdf:uv")) > 0) {
+       while ((opt = getopt(argc, argv, "hdf:Fuv")) > 0) {
                switch (opt) {
                case 'd':
                        debug_mode = 1;
@@ -173,6 +174,9 @@ int main(int argc, char **argv)
                case 'f':
                        watch_file = optarg;
                        break;
+               case 'F':
+                       foreground_mode = 1;
+                       break;
                case 'u':
                        run_as_user = 1;
                        break;
@@ -209,13 +213,12 @@ int main(int argc, char **argv)
 
        read_config(master_fd, watch_file);
 
-       if (!debug_mode) {
+       if (!debug_mode && !foreground_mode) {
                if (daemon(0, 0) < 0)
                        exitApp("daemon");
+               write_pid_file();
        }
 
-       write_pid_file();
-
        while (watch(master_fd, watch_file) == 0) {
        }
 
--

2.53.0