diff mbox series

[3/3] RFC: delete PID file on exit

Message ID 20180831145314.14736-4-marcandre.lureau@redhat.com (mailing list archive)
State New, archived
Headers show
Series util: add qemu_write_pidfile() | expand

Commit Message

Marc-André Lureau Aug. 31, 2018, 2:53 p.m. UTC
Register an exit handler to remove the PID file. By the time atexit()
is called, qemu_write_pidfile() guarantees QEMU owns the PID file,
thus we could safely remove it when exiting.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 vl.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

Comments

Stefan Weil Aug. 31, 2018, 4:29 p.m. UTC | #1
Am 31.08.2018 um 16:53 schrieb Marc-André Lureau:
[...]
> +static const char *pid_file;
> +
> +static void qemu_unlink_pidfile(void)
> +{
> +    if (pid_file) {
> +        unlink(pid_file);
> +    }
> +}
> +
>  bool machine_init_done;
>  
>  void qemu_add_machine_init_done_notifier(Notifier *notify)
> @@ -2927,7 +2936,6 @@ int main(int argc, char **argv, char **envp)
>      const char *vga_model = NULL;
>      const char *qtest_chrdev = NULL;
>      const char *qtest_log = NULL;
> -    const char *pid_file = NULL;
>      const char *incoming = NULL;
>      bool userconfig = true;
>      bool nographic = false;
> @@ -4000,6 +4008,7 @@ int main(int argc, char **argv, char **envp)
>          error_reportf_err(err, "cannot create PID file: ");
>          exit(1);
>      }
> +    atexit(qemu_unlink_pidfile);


Could qemu_unlink_pidfile be combined with qemu_run_exit_notifiers to
avoid an additional call of atexit from the same function (main)?

Even if there is support for 32 or more atexit calls, it might be good
to be a little bit more restrictive.

Regards
Stefan
Marc-André Lureau Aug. 31, 2018, 4:35 p.m. UTC | #2
Hi

On Fri, Aug 31, 2018 at 6:29 PM, Stefan Weil <sw@weilnetz.de> wrote:
> Am 31.08.2018 um 16:53 schrieb Marc-André Lureau:
> [...]
>> +static const char *pid_file;
>> +
>> +static void qemu_unlink_pidfile(void)
>> +{
>> +    if (pid_file) {
>> +        unlink(pid_file);
>> +    }
>> +}
>> +
>>  bool machine_init_done;
>>
>>  void qemu_add_machine_init_done_notifier(Notifier *notify)
>> @@ -2927,7 +2936,6 @@ int main(int argc, char **argv, char **envp)
>>      const char *vga_model = NULL;
>>      const char *qtest_chrdev = NULL;
>>      const char *qtest_log = NULL;
>> -    const char *pid_file = NULL;
>>      const char *incoming = NULL;
>>      bool userconfig = true;
>>      bool nographic = false;
>> @@ -4000,6 +4008,7 @@ int main(int argc, char **argv, char **envp)
>>          error_reportf_err(err, "cannot create PID file: ");
>>          exit(1);
>>      }
>> +    atexit(qemu_unlink_pidfile);
>
>
> Could qemu_unlink_pidfile be combined with qemu_run_exit_notifiers to
> avoid an additional call of atexit from the same function (main)?
>
> Even if there is support for 32 or more atexit calls, it might be good
> to be a little bit more restrictive.

Sure, that would require an additional Notifier, but that's not much anyway.
I'll update the patch.
diff mbox series

Patch

diff --git a/vl.c b/vl.c
index 0eaf948d32..743679ddb7 100644
--- a/vl.c
+++ b/vl.c
@@ -2603,6 +2603,15 @@  static void qemu_run_exit_notifiers(void)
     notifier_list_notify(&exit_notifiers, NULL);
 }
 
+static const char *pid_file;
+
+static void qemu_unlink_pidfile(void)
+{
+    if (pid_file) {
+        unlink(pid_file);
+    }
+}
+
 bool machine_init_done;
 
 void qemu_add_machine_init_done_notifier(Notifier *notify)
@@ -2927,7 +2936,6 @@  int main(int argc, char **argv, char **envp)
     const char *vga_model = NULL;
     const char *qtest_chrdev = NULL;
     const char *qtest_log = NULL;
-    const char *pid_file = NULL;
     const char *incoming = NULL;
     bool userconfig = true;
     bool nographic = false;
@@ -4000,6 +4008,7 @@  int main(int argc, char **argv, char **envp)
         error_reportf_err(err, "cannot create PID file: ");
         exit(1);
     }
+    atexit(qemu_unlink_pidfile);
 
     if (qemu_init_main_loop(&main_loop_err)) {
         error_report_err(main_loop_err);