]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/bus-wait-for-jobs.c
Merge pull request #30284 from YHNdnzj/fstab-wantedby-defaultdeps
[thirdparty/systemd.git] / src / shared / bus-wait-for-jobs.c
index b2a9e031ce749be127e44ba869c36acdf579cbae..b3280c15dfede0e5a974c56e6565375d9bb2fdae 100644 (file)
@@ -34,13 +34,12 @@ static int match_disconnected(sd_bus_message *m, void *userdata, sd_bus_error *e
 
 static int match_job_removed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
         const char *path, *unit, *result;
-        BusWaitForJobs *d = userdata;
+        BusWaitForJobs *d = ASSERT_PTR(userdata);
         uint32_t id;
         char *found;
         int r;
 
         assert(m);
-        assert(d);
 
         r = sd_bus_message_read(m, "uoss", &id, &path, &unit, &result);
         if (r < 0) {
@@ -61,9 +60,9 @@ static int match_job_removed(sd_bus_message *m, void *userdata, sd_bus_error *er
         return 0;
 }
 
-void bus_wait_for_jobs_free(BusWaitForJobs *d) {
+BusWaitForJobs* bus_wait_for_jobs_free(BusWaitForJobs *d) {
         if (!d)
-                return;
+                return NULL;
 
         set_free(d->jobs);
 
@@ -75,7 +74,7 @@ void bus_wait_for_jobs_free(BusWaitForJobs *d) {
         free(d->name);
         free(d->result);
 
-        free(d);
+        return mfree(d);
 }
 
 int bus_wait_for_jobs_new(sd_bus *bus, BusWaitForJobs **ret) {
@@ -133,7 +132,7 @@ static int bus_process_wait(sd_bus *bus) {
                 if (r > 0)
                         return 0;
 
-                r = sd_bus_wait(bus, (uint64_t) -1);
+                r = sd_bus_wait(bus, UINT64_MAX);
                 if (r < 0)
                         return r;
         }
@@ -181,14 +180,14 @@ static void log_job_error_with_service_result(const char* service, const char *r
 
         assert(service);
 
-        service_shell_quoted = shell_maybe_quote(service, ESCAPE_BACKSLASH);
+        service_shell_quoted = shell_maybe_quote(service, 0);
 
         if (!strv_isempty((char**) extra_args)) {
-                _cleanup_free_ char *t;
+                _cleanup_free_ char *t = NULL;
 
                 t = strv_join((char**) extra_args, " ");
-                systemctl = strjoina("systemctl ", t ? : "<args>");
-                journalctl = strjoina("journalctl ", t ? : "<args>");
+                systemctl = strjoina("systemctl ", t ?: "<args>");
+                journalctl = strjoina("journalctl ", t ?: "<args>");
         }
 
         if (!isempty(result)) {
@@ -200,22 +199,24 @@ static void log_job_error_with_service_result(const char* service, const char *r
 
                 if (i < ELEMENTSOF(explanations)) {
                         log_error("Job for %s failed because %s.\n"
-                                  "See \"%s status %s\" and \"%s -xe\" for details.\n",
+                                  "See \"%s status %s\" and \"%s -xeu %s\" for details.\n",
                                   service,
                                   explanations[i].explanation,
                                   systemctl,
                                   service_shell_quoted ?: "<service>",
-                                  journalctl);
+                                  journalctl,
+                                  service_shell_quoted ?: "<service>");
                         goto finish;
                 }
         }
 
         log_error("Job for %s failed.\n"
-                  "See \"%s status %s\" and \"%s -xe\" for details.\n",
+                  "See \"%s status %s\" and \"%s -xeu %s\" for details.\n",
                   service,
                   systemctl,
                   service_shell_quoted ?: "<service>",
-                  journalctl);
+                  journalctl,
+                  service_shell_quoted ?: "<service>");
 
 finish:
         /* For some results maybe additional explanation is required */
@@ -226,12 +227,12 @@ finish:
                          service_shell_quoted ?: "<service>");
 }
 
-static int check_wait_response(BusWaitForJobs *d, bool quiet, const char* const* extra_args) {
+static int check_wait_response(BusWaitForJobs *d, WaitJobsFlags flags, const char* const* extra_args) {
         assert(d);
         assert(d->name);
         assert(d->result);
 
-        if (!quiet) {
+        if (FLAGS_SET(flags, BUS_WAIT_JOBS_LOG_ERROR)) {
                 if (streq(d->result, "canceled"))
                         log_error("Job for %s canceled.", strna(d->name));
                 else if (streq(d->result, "timeout"))
@@ -278,14 +279,21 @@ static int check_wait_response(BusWaitForJobs *d, bool quiet, const char* const*
                 return -EOPNOTSUPP;
         else if (streq(d->result, "once"))
                 return -ESTALE;
-        else if (STR_IN_SET(d->result, "done", "skipped"))
+        else if (streq(d->result, "done")) {
+                if (FLAGS_SET(flags, BUS_WAIT_JOBS_LOG_SUCCESS))
+                        log_info("Job for %s finished.", strna(d->name));
+                return 0;
+        } else if (streq(d->result, "skipped")) {
+                if (FLAGS_SET(flags, BUS_WAIT_JOBS_LOG_SUCCESS))
+                        log_info("Job for %s was skipped.", strna(d->name));
                 return 0;
+        }
 
         return log_debug_errno(SYNTHETIC_ERRNO(EIO),
                                "Unexpected job result, assuming server side newer than us: %s", d->result);
 }
 
-int bus_wait_for_jobs(BusWaitForJobs *d, bool quiet, const char* const* extra_args) {
+int bus_wait_for_jobs(BusWaitForJobs *d, WaitJobsFlags flags, const char* const* extra_args) {
         int r = 0;
 
         assert(d);
@@ -298,13 +306,14 @@ int bus_wait_for_jobs(BusWaitForJobs *d, bool quiet, const char* const* extra_ar
                         return log_error_errno(q, "Failed to wait for response: %m");
 
                 if (d->name && d->result) {
-                        q = check_wait_response(d, quiet, extra_args);
+                        q = check_wait_response(d, flags, extra_args);
                         /* Return the first error as it is most likely to be
                          * meaningful. */
                         if (q < 0 && r == 0)
                                 r = q;
 
-                        log_debug_errno(q, "Got result %s/%m for job %s", d->result, d->name);
+                        log_full_errno_zerook(LOG_DEBUG, q,
+                                              "Got result %s/%m for job %s", d->result, d->name);
                 }
 
                 d->name = mfree(d->name);
@@ -320,12 +329,12 @@ int bus_wait_for_jobs_add(BusWaitForJobs *d, const char *path) {
         return set_put_strdup(&d->jobs, path);
 }
 
-int bus_wait_for_jobs_one(BusWaitForJobs *d, const char *path, bool quiet) {
+int bus_wait_for_jobs_one(BusWaitForJobs *d, const char *path, WaitJobsFlags flags, const char* const* extra_args) {
         int r;
 
         r = bus_wait_for_jobs_add(d, path);
         if (r < 0)
                 return log_oom();
 
-        return bus_wait_for_jobs(d, quiet, NULL);
+        return bus_wait_for_jobs(d, flags, extra_args);
 }