sdbus: kdbus tests: improve mt-safety
authorMaciej Slodczyk <m.slodczyk2@partner.samsung.com>
Tue, 14 Apr 2020 10:54:12 +0000 (12:54 +0200)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Thu, 25 Jan 2024 14:15:26 +0000 (15:15 +0100)
Change-Id: I09d0eea89920d3af549f2e10b2a1dbdd7a9b4cf2
Signed-off-by: Maciej Slodczyk <m.slodczyk2@partner.samsung.com>
src/libsystemd/sd-bus/test-bus-async-match.c

index a95197a..00f837e 100644 (file)
@@ -104,6 +104,7 @@ static int install_match_callback(sd_bus_message *m, void *userdata,
         int r;
         sd_bus_message *msg;
         struct app_data *app = (struct app_data *)userdata;
+        char err[512];
 
         assert(app);
         assert(app->bus);
@@ -117,11 +118,12 @@ static int install_match_callback(sd_bus_message *m, void *userdata,
         }
 
         if (NULL != ret_error && 0 != sd_bus_error_is_set(ret_error)) {
-                fprintf(stderr,
-                        "Failed to add match callback: %s/%s (%s)\n",
-                        ret_error->name,
-                        ret_error->message,
-                        strerror(sd_bus_error_get_errno(ret_error)));
+                if (strerror_r(abs(sd_bus_error_get_errno(ret_error)), err, sizeof err))
+                        fprintf(stderr,
+                                "Failed to add match callback: %s/%s (%s)\n",
+                                ret_error->name,
+                                ret_error->message,
+                                err);
                 app->state = APP_STATE_FINISHED_ERROR;
                 sd_event_exit(app->event, sd_bus_error_get_errno(ret_error));
                 return 0;
@@ -133,7 +135,8 @@ static int install_match_callback(sd_bus_message *m, void *userdata,
                         "SystemdAsyncMatchTestSignal");
 
         if (r < 0) {
-                fprintf(stderr, "Failed to add signal: %s\n", strerror(-r));
+                if (strerror_r(abs(r), err, sizeof err))
+                        fprintf(stderr, "Failed to add signal: %s\n", err);
                 app->state = APP_STATE_FINISHED_ERROR;
                 sd_event_exit(app->event, r);
                 return r;
@@ -141,7 +144,8 @@ static int install_match_callback(sd_bus_message *m, void *userdata,
 
         r = sd_bus_send(app->bus, msg, NULL);
         if (r < 0) {
-                fprintf(stderr, "Failed to emit signal: %s\n", strerror(-r));
+                if (strerror_r(abs(r), err, sizeof err))
+                        fprintf(stderr, "Failed to emit signal: %s\n", err);
                 app->state = APP_STATE_FINISHED_ERROR;
                 sd_event_exit(app->event, r);
                 return r;
@@ -181,6 +185,7 @@ int main(int argc, char *argv[]) {
         int r;
         sigset_t ss;
         uint64_t now;
+        char err[512];
         const char *match = "type='signal',interface='org.freedesktop.systemd1.Manager',path='/org/freedesktop/systemd1',member='SystemdAsyncMatchTestSignal'";
 
         r = sd_event_default(&app.event);
@@ -216,7 +221,8 @@ int main(int argc, char *argv[]) {
         /* Connect to the system bus */
         r = sd_bus_open_system(&app.bus);
         if (r < 0) {
-                fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r));
+                if (strerror_r(abs(r), err, sizeof err))
+                        fprintf(stderr, "Failed to connect to system bus: %s\n", err);
                 app.state = APP_STATE_FINISHED_ERROR;
                 goto finish;
         }
@@ -224,7 +230,8 @@ int main(int argc, char *argv[]) {
         /* Add global test timeout callback */
         r = sd_event_now(app.event, CLOCK_MONOTONIC, &now);
         if (r < 0) {
-                fprintf(stderr, "Failed to get current timestamp: %s\n", strerror(-r));
+                if (strerror_r(abs(r), err, sizeof err))
+                        fprintf(stderr, "Failed to get current timestamp: %s\n", err);
                 app.state = APP_STATE_FINISHED_ERROR;
                 goto finish;
         }
@@ -238,7 +245,8 @@ int main(int argc, char *argv[]) {
                 &app);
 
         if (r < 0) {
-                fprintf(stderr, "Failed to add global timeout: %s\n", strerror(-r));
+                if (strerror_r(abs(r), err, sizeof err))
+                        fprintf(stderr, "Failed to add global timeout: %s\n", err);
                 app.state = APP_STATE_FINISHED_ERROR;
                 goto finish;
         }
@@ -247,7 +255,8 @@ int main(int argc, char *argv[]) {
         r = sd_bus_add_match_async(app.bus, &slot, match, signal_match_callback,
                         install_match_callback, &app);
         if (r < 0) {
-                fprintf(stderr, "Failed to add match: %s\n", strerror(-r));
+                if (strerror_r(abs(r), err, sizeof err))
+                        fprintf(stderr, "Failed to add match: %s\n", err);
                 app.state = APP_STATE_FINISHED_ERROR;
                 goto finish;
         }
@@ -256,7 +265,8 @@ int main(int argc, char *argv[]) {
         /* Attach system bus events to manloop */
         r = sd_bus_attach_event(app.bus, app.event, SD_EVENT_PRIORITY_NORMAL);
         if (r < 0) {
-                fprintf(stderr, "Failed to attach system bus to mainloop: %s\n", strerror(-r));
+                if (strerror_r(abs(r), err, sizeof err))
+                        fprintf(stderr, "Failed to attach system bus to mainloop: %s\n", err);
                 app.state = APP_STATE_FINISHED_ERROR;
                 goto finish;
         }