]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
_dbus_spawn_program(): Add additional parameter to return error
authorRalf Habacker <ralf.habacker@freenet.de>
Wed, 24 Nov 2021 08:49:47 +0000 (09:49 +0100)
committerRalf Habacker <ralf.habacker@freenet.de>
Mon, 29 Nov 2021 12:07:04 +0000 (13:07 +0100)
By specifying an error instance via the additional parameter, errors that
occur in it are transported to the caller in a coordinated manner.

This eliminates the need to query GetLastError() outside the function,
which can return an incorrect value if the implementation changes.

dbus/dbus-spawn-win.c
dbus/dbus-sysdeps-win.h
tools/dbus-run-session.c

index f0773aa7fa8232e76cc83b1c6001cdb68c9674e1..5d444b5803de4ed67abcbf3f46de6086b8f0fb97 100644 (file)
@@ -494,11 +494,23 @@ build_env_string (char** envp)
   return compose_string (envp, '\0');
 }
 
+/**
+ * Creates a process with arguments and environment variables
+ *
+ * @param name name of the program
+ * @param argv list of char* pointers for the arguments
+ * @param envp list of char pointers for the environment
+ * @param inherit_handles specifies whether handles should be inherited by the child process
+ * @param error the error to set, if NULL no error will be set
+ * @return #NULL if an error occurred, the reason is returned in \p error
+ * @note The call to GetLastError() after this function may not return the expected value.
+ */
 HANDLE
 _dbus_spawn_program (const char *name,
                      char      **argv,
                      char      **envp,
-                     dbus_bool_t inherit_handles)
+                     dbus_bool_t inherit_handles,
+                     DBusError  *error)
 {
   PROCESS_INFORMATION pi = { NULL, 0, 0, 0 };
   STARTUPINFOA si;
@@ -514,7 +526,10 @@ _dbus_spawn_program (const char *name,
   arg_string = build_commandline (argv);
 #endif
   if (!arg_string)
-    return NULL;
+    {
+      dbus_set_error (error, DBUS_ERROR_FAILED, "No arguments given to start '%s' or out of memory", name);
+      goto out;
+    }
 
   env_string = build_env_string(envp);
 
@@ -545,7 +560,13 @@ _dbus_spawn_program (const char *name,
     free (env_string);
 
   if (!result)
-    return NULL;
+    {
+      _dbus_win_set_error_from_last_error (error, "Unable to start '%s' with arguments '%s'", name, arg_string);
+      goto out;
+    }
+
+out:
+  _DBUS_ASSERT_ERROR_XOR_BOOL (error, result);
 
   CloseHandle (pi.hThread);
   return pi.hProcess;
@@ -672,7 +693,7 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter           **sitter_p,
   _dbus_verbose ("babysitter: spawn child '%s'\n", my_argv[0]);
 
   PING();
-  handle = _dbus_spawn_program (sitter->log_name, my_argv, (char **) envp, FALSE);
+  handle = _dbus_spawn_program (sitter->log_name, my_argv, (char **) envp, FALSE, NULL);
 
   if (my_argv != NULL)
     {
index d63fb4f18f6ccc8906341a829e570df2e6726285..52db915bffd4b922d429151d343b8ae9f30f3329 100644 (file)
@@ -93,7 +93,8 @@ dbus_bool_t _dbus_getsid(char **sid, dbus_pid_t process_id);
 HANDLE      _dbus_spawn_program (const char *name,
                                  char **argv,
                                  char **envp,
-                                 dbus_bool_t inherit_handles);
+                                 dbus_bool_t inherit_handles,
+                                 DBusError *error);
 
 DBUS_PRIVATE_EXPORT
 void _dbus_win_set_error_from_last_error (DBusError  *error,
index f481d7cefa3a45ea82928be98d545e01f5ed42e3..a622fb3db1cb4e3cd139e32729c97d87430302a1 100644 (file)
@@ -441,12 +441,9 @@ run_session (const char *dbus_daemon,
   dbus_daemon_argv[3] = _dbus_string_get_data (&argv_strings[3]);
   dbus_daemon_argv[4] = NULL;
 
-  server_handle = _dbus_spawn_program (dbus_daemon, dbus_daemon_argv, NULL, TRUE);
+  server_handle = _dbus_spawn_program (dbus_daemon, dbus_daemon_argv, NULL, TRUE, &error);
   if (server_handle == NULL)
-    {
-      _dbus_win_set_error_from_last_error (&error, "Could not start dbus daemon");
-      goto out;
-    }
+    goto out;
 
   /* wait until dbus-daemon is ready for connections */
   if (ready_event_handle != NULL)
@@ -529,12 +526,9 @@ run_session (const char *dbus_daemon,
   if (!env)
     goto out;
 
-  app_handle = _dbus_spawn_program (argv[prog_arg], argv + prog_arg, env, FALSE);
+  app_handle = _dbus_spawn_program (argv[prog_arg], argv + prog_arg, env, FALSE, &error);
   if (app_handle == NULL)
-    {
-      _dbus_win_set_error_from_last_error (&error, "Unable to start child process");
-      goto out;
-    }
+    goto out;
 
   WaitForSingleObject (app_handle, INFINITE);
   if (!GetExitCodeProcess (app_handle, &exit_code))