]> granicus.if.org Git - transmission/commitdiff
Refactor daemon startup a bit
authorMike Gelfand <mikedld@mikedld.com>
Fri, 15 Mar 2019 18:53:53 +0000 (21:53 +0300)
committerMike Gelfand <mikedld@mikedld.com>
Fri, 15 Mar 2019 18:55:45 +0000 (21:55 +0300)
Fix exit code to be zero when dumping settings along the way.

Closes: #487
daemon/daemon.c

index 64a5b4ccc14bf9111621c0ea332c23138bf8dd0c..3237eea6a80a3a3e2756479a09d3b266ad60a903 100644 (file)
@@ -796,38 +796,23 @@ cleanup:
     return 0;
 }
 
-int tr_main(int argc, char* argv[])
+static bool init_daemon_data(int argc, char* argv[], struct daemon_data* data, bool* foreground, int* ret)
 {
-    dtr_callbacks const cb =
-    {
-        .on_start = &daemon_start,
-        .on_stop = &daemon_stop,
-        .on_reconfigure = &daemon_reconfigure,
-    };
+    data->configDir = getConfigDir(argc, (char const* const*)argv);
 
-    int ret;
-    bool loaded;
-    bool dumpSettings;
-    bool foreground;
-    tr_error* error = NULL;
+    /* load settings from defaults + config file */
+    tr_variantInitDict(&data->settings, 0);
+    tr_variantDictAddBool(&data->settings, TR_KEY_rpc_enabled, true);
+    bool const loaded = tr_sessionLoadSettings(&data->settings, data->configDir, MY_NAME);
 
-    struct daemon_data arg;
-    tr_variant* const settings = &arg.settings;
-    char const** const configDir = &arg.configDir;
+    bool dumpSettings;
 
-    key_pidfile = tr_quark_new("pidfile", 7);
-    key_watch_dir_force_generic = tr_quark_new("watch-dir-force-generic", 23);
+    *ret = 0;
 
-    /* load settings from defaults + config file */
-    tr_variantInitDict(settings, 0);
-    tr_variantDictAddBool(settings, TR_KEY_rpc_enabled, true);
-    *configDir = getConfigDir(argc, (char const* const*)argv);
-    loaded = tr_sessionLoadSettings(settings, *configDir, MY_NAME);
-
-    /* overwrite settings from the comamndline */
-    if (!parse_args(argc, (char const**)argv, settings, &arg.paused, &dumpSettings, &foreground, &ret))
+    /* overwrite settings from the command line */
+    if (!parse_args(argc, (char const**)argv, &data->settings, &data->paused, &dumpSettings, foreground, ret))
     {
-        goto cleanup;
+        goto exit_early;
     }
 
     if (foreground && logfile == TR_BAD_SYS_FILE)
@@ -838,19 +823,49 @@ int tr_main(int argc, char* argv[])
     if (!loaded)
     {
         printMessage(logfile, TR_LOG_ERROR, MY_NAME, "Error loading config file -- exiting.", __FILE__, __LINE__);
-        ret = 1;
-        goto cleanup;
+        *ret = 1;
+        goto exit_early;
     }
 
     if (dumpSettings)
     {
-        char* str = tr_variantToStr(settings, TR_VARIANT_FMT_JSON, NULL);
+        char* str = tr_variantToStr(&data->settings, TR_VARIANT_FMT_JSON, NULL);
         fprintf(stderr, "%s", str);
         tr_free(str);
-        goto cleanup;
+        goto exit_early;
+    }
+
+    return true;
+
+exit_early:
+    tr_variantFree(&data->settings);
+    return false;
+}
+
+int tr_main(int argc, char* argv[])
+{
+    key_pidfile = tr_quark_new("pidfile", TR_BAD_SIZE);
+    key_watch_dir_force_generic = tr_quark_new("watch-dir-force-generic", TR_BAD_SIZE);
+
+    struct daemon_data data;
+    bool foreground;
+    int ret;
+
+    if (!init_daemon_data(argc, argv, &data, &foreground, &ret))
+    {
+        return ret;
     }
 
-    if (!dtr_daemon(&cb, &arg, foreground, &ret, &error))
+    dtr_callbacks const cb =
+    {
+        .on_start = &daemon_start,
+        .on_stop = &daemon_stop,
+        .on_reconfigure = &daemon_reconfigure,
+    };
+
+    tr_error* error = NULL;
+
+    if (!dtr_daemon(&cb, &data, foreground, &ret, &error))
     {
         char buf[256];
         tr_snprintf(buf, sizeof(buf), "Failed to daemonize: %s", error->message);
@@ -858,8 +873,6 @@ int tr_main(int argc, char* argv[])
         tr_error_free(error);
     }
 
-cleanup:
-    tr_variantFree(settings);
-
+    tr_variantFree(&data.settings);
     return ret;
 }