1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

Use format patterns for usec_t, pid_t, nsec_t, usec_t

It is nicer to predefine patterns using configure time check instead of
using casts everywhere.

Since we do not need to use any flags, include "%" in the format instead
of excluding it like PRI* macros.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2013-12-30 17:22:26 -05:00
parent ab9001a1e3
commit ccd06097c7
14 changed files with 95 additions and 73 deletions

1
.gitignore vendored
View File

@ -112,6 +112,7 @@
/test-bus-creds
/test-bus-error
/test-bus-gvariant
/test-bus-cleanup
/test-bus-introspect
/test-bus-kernel
/test-bus-kernel-benchmark

View File

@ -178,6 +178,9 @@ CC_CHECK_FLAGS_APPEND([with_ldflags], [LDFLAGS], [\
-Wl,-z,now])
AC_SUBST([OUR_LDFLAGS], "$with_ldflags $address_sanitizer_ldflags")
AC_CHECK_SIZEOF(pid_t)
AC_CHECK_SIZEOF(uid_t)
# ------------------------------------------------------------------------------
# we use python to build the man page index, and for systemd-python
have_python=no

View File

@ -157,9 +157,9 @@ static int rename_service(sd_bus *a, sd_bus *b) {
/* The status string gets the full command line ... */
sd_notifyf(false,
"STATUS=Processing requests from client PID %lu (%s); UID %lu (%s)",
(unsigned long) pid, p,
(unsigned long) uid, name);
"STATUS=Processing requests from client PID "PID_FMT" (%s); UID "UID_FMT" (%s)",
pid, p,
uid, name);
/* ... and the argv line only the short comm */
if (arg_command_line_buffer) {
@ -167,17 +167,17 @@ static int rename_service(sd_bus *a, sd_bus *b) {
m = strlen(arg_command_line_buffer);
w = snprintf(arg_command_line_buffer, m,
"[PID %lu/%s; UID %lu/%s]",
(unsigned long) pid, comm,
(unsigned long) uid, name);
"[PID "PID_FMT"/%s; UID "UID_FMT"/%s]",
pid, comm,
uid, name);
if (m > w)
memset(arg_command_line_buffer + w, 0, m - w);
}
log_debug("Running on behalf of PID %lu (%s), UID %lu (%s), %s",
(unsigned long) pid, p,
(unsigned long) uid, name,
log_debug("Running on behalf of PID "PID_FMT" (%s), UID "UID_FMT" (%s), %s",
pid, p,
uid, name,
a->unique_name);
;
return 0;

View File

@ -774,8 +774,8 @@ static int automount_dispatch_io(sd_event_source *s, int fd, uint32_t events, vo
get_process_comm(packet.v5_packet.pid, &p);
log_info_unit(UNIT(a)->id,
"Got automount request for %s, triggered by %lu (%s)",
a->where, (unsigned long) packet.v5_packet.pid, strna(p));
"Got automount request for %s, triggered by "PID_FMT" (%s)",
a->where, packet.v5_packet.pid, strna(p));
} else
log_debug_unit(UNIT(a)->id, "Got direct mount request on %s", a->where);

View File

@ -321,10 +321,9 @@ int bus_cgroup_set_property(
if (r < 0)
return r;
while (( r = sd_bus_message_read(message, "(st)", &path, &u64)) > 0) {
unsigned long ul;
while ((r = sd_bus_message_read(message, "(st)", &path, &u64)) > 0) {
unsigned long ul = u64;
ul = (unsigned long) u64;
if (ul < 10 || ul > 1000)
return sd_bus_error_set_errnof(error, EINVAL, "BlockIODeviceWeight out of range");

View File

@ -1038,7 +1038,7 @@ static int build_environment(
return -ENOMEM;
if (n_fds > 0) {
if (asprintf(&x, "LISTEN_PID=%lu", (unsigned long) getpid()) < 0)
if (asprintf(&x, "LISTEN_PID="PID_FMT, getpid()) < 0)
return -ENOMEM;
our_env[n_env++] = x;
@ -1048,7 +1048,7 @@ static int build_environment(
}
if (watchdog_usec > 0) {
if (asprintf(&x, "WATCHDOG_PID=%lu", (unsigned long) getpid()) < 0)
if (asprintf(&x, "WATCHDOG_PID="PID_FMT, getpid()) < 0)
return -ENOMEM;
our_env[n_env++] = x;
@ -1636,8 +1636,8 @@ int exec_spawn(ExecCommand *command,
log_struct_unit(LOG_DEBUG,
unit_id,
"MESSAGE=Forked %s as %lu",
command->path, (unsigned long) pid,
"MESSAGE=Forked %s as "PID_FMT,
command->path, pid,
NULL);
/* We add the new process to the cgroup both in the child (so
@ -1979,7 +1979,7 @@ void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
}
if (c->timer_slack_nsec != (nsec_t) -1)
fprintf(f, "%sTimerSlackNSec: %lu\n", prefix, (unsigned long)c->timer_slack_nsec);
fprintf(f, "%sTimerSlackNSec: "NSEC_FMT "\n", prefix, c->timer_slack_nsec);
fprintf(f,
"%sStandardInput: %s\n"
@ -2139,8 +2139,8 @@ void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix) {
return;
fprintf(f,
"%sPID: %lu\n",
prefix, (unsigned long) s->pid);
"%sPID: "PID_FMT"\n",
prefix, s->pid);
if (s->start_timestamp.realtime > 0)
fprintf(f,

View File

@ -906,7 +906,7 @@ char *job_dbus_path(Job *j) {
assert(j);
if (asprintf(&p, "/org/freedesktop/systemd1/job/%lu", (unsigned long) j->id) < 0)
if (asprintf(&p, "/org/freedesktop/systemd1/job/%"PRIu32, j->id) < 0)
return NULL;
return p;
@ -922,7 +922,7 @@ int job_serialize(Job *j, FILE *f, FDSet *fds) {
fprintf(f, "job-ignore-order=%s\n", yes_no(j->ignore_order));
if (j->begin_usec > 0)
fprintf(f, "job-begin=%llu\n", (unsigned long long) j->begin_usec);
fprintf(f, "job-begin="USEC_FMT"\n", j->begin_usec);
bus_client_track_serialize(j->manager, f, j->subscribed);

View File

@ -170,12 +170,12 @@ static int killall(int sig, Set *pids, bool send_sighup) {
_cleanup_free_ char *s;
get_process_comm(pid, &s);
log_notice("Sending SIGKILL to PID %lu (%s).", (unsigned long) pid, strna(s));
log_notice("Sending SIGKILL to PID "PID_FMT" (%s).", pid, strna(s));
}
if (kill(pid, sig) >= 0) {
if (pids)
set_put(pids, ULONG_TO_PTR((unsigned long) pid));
set_put(pids, ULONG_TO_PTR(pid));
} else if (errno != ENOENT)
log_warning("Could not kill %d: %m", pid);

View File

@ -166,7 +166,7 @@ noreturn static void crash(int sig) {
else if (status.si_code != CLD_DUMPED)
log_error("Caught <%s>, core dump failed.", signal_to_string(sig));
else
log_error("Caught <%s>, dumped core as pid %lu.", signal_to_string(sig), (unsigned long) pid);
log_error("Caught <%s>, dumped core as pid "PID_FMT".", signal_to_string(sig), pid);
}
}
@ -197,7 +197,7 @@ noreturn static void crash(int sig) {
_exit(1);
}
log_info("Successfully spawned crash shell as pid %lu.", (unsigned long) pid);
log_info("Successfully spawned crash shell as pid "PID_FMT".", pid);
}
log_info("Freezing execution.");
@ -1865,7 +1865,7 @@ finish:
watchdog_close(false);
/* Tell the binary how often to ping */
snprintf(e, sizeof(e), "WATCHDOG_USEC=%llu", (unsigned long long) arg_shutdown_watchdog);
snprintf(e, sizeof(e), "WATCHDOG_USEC="USEC_FMT, arg_shutdown_watchdog);
char_array_0(e);
env_block = strv_append(environ, e);

View File

@ -1334,7 +1334,7 @@ static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t
if (!u) {
u = manager_get_unit_by_pid(m, ucred->pid);
if (!u) {
log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred->pid);
log_warning("Cannot find unit for notify message of PID "PID_FMT".", ucred->pid);
continue;
}
}
@ -1382,7 +1382,7 @@ static int manager_dispatch_sigchld(Manager *m) {
_cleanup_free_ char *name = NULL;
get_process_comm(si.si_pid, &name);
log_debug("Got SIGCHLD for process %lu (%s)", (unsigned long) si.si_pid, strna(name));
log_debug("Got SIGCHLD for process "PID_FMT" (%s)", si.si_pid, strna(name));
}
/* And now figure out the unit this belongs to */
@ -1470,9 +1470,9 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
log_full(sfsi.ssi_signo == SIGCHLD ||
(sfsi.ssi_signo == SIGTERM && m->running_as == SYSTEMD_USER)
? LOG_DEBUG : LOG_INFO,
"Received SIG%s from PID %lu (%s).",
"Received SIG%s from PID "PID_FMT" (%s).",
signal_to_string(sfsi.ssi_signo),
(unsigned long) sfsi.ssi_pid, strna(p));
sfsi.ssi_pid, strna(p));
} else
log_full(sfsi.ssi_signo == SIGCHLD ||
(sfsi.ssi_signo == SIGTERM && m->running_as == SYSTEMD_USER)
@ -1974,9 +1974,9 @@ int manager_open_serialization(Manager *m, FILE **_f) {
assert(_f);
if (m->running_as == SYSTEMD_SYSTEM)
asprintf(&path, "/run/systemd/dump-%lu-XXXXXX", (unsigned long) getpid());
asprintf(&path, "/run/systemd/dump-"PID_FMT"-XXXXXX", getpid());
else
asprintf(&path, "/tmp/systemd-dump-%lu-XXXXXX", (unsigned long) getpid());
asprintf(&path, "/tmp/systemd-dump-"PID_FMT"-XXXXXX", getpid());
if (!path)
return -ENOMEM;
@ -2454,9 +2454,9 @@ void manager_check_finished(Manager *m) {
if (!log_on_console())
log_struct(LOG_INFO,
MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED),
"KERNEL_USEC=%llu", (unsigned long long) kernel_usec,
"INITRD_USEC=%llu", (unsigned long long) initrd_usec,
"USERSPACE_USEC=%llu", (unsigned long long) userspace_usec,
"KERNEL_USEC="USEC_FMT, kernel_usec,
"INITRD_USEC="USEC_FMT, initrd_usec,
"USERSPACE_USEC="USEC_FMT, userspace_usec,
"MESSAGE=Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.",
format_timespan(kernel, sizeof(kernel), kernel_usec, USEC_PER_MSEC),
format_timespan(initrd, sizeof(initrd), initrd_usec, USEC_PER_MSEC),
@ -2470,8 +2470,8 @@ void manager_check_finished(Manager *m) {
if (!log_on_console())
log_struct(LOG_INFO,
MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED),
"KERNEL_USEC=%llu", (unsigned long long) kernel_usec,
"USERSPACE_USEC=%llu", (unsigned long long) userspace_usec,
"KERNEL_USEC="USEC_FMT, kernel_usec,
"USERSPACE_USEC="USEC_FMT, userspace_usec,
"MESSAGE=Startup finished in %s (kernel) + %s (userspace) = %s.",
format_timespan(kernel, sizeof(kernel), kernel_usec, USEC_PER_MSEC),
format_timespan(userspace, sizeof(userspace), userspace_usec, USEC_PER_MSEC),
@ -2485,7 +2485,7 @@ void manager_check_finished(Manager *m) {
if (!log_on_console())
log_struct(LOG_INFO,
MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED),
"USERSPACE_USEC=%llu", (unsigned long long) userspace_usec,
"USERSPACE_USEC="USEC_FMT, userspace_usec,
"MESSAGE=Startup finished in %s.",
format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC),
NULL);

View File

@ -750,8 +750,8 @@ static void mount_dump(Unit *u, FILE *f, const char *prefix) {
if (m->control_pid > 0)
fprintf(f,
"%sControl PID: %lu\n",
prefix, (unsigned long) m->control_pid);
"%sControl PID: "PID_FMT"\n",
prefix, m->control_pid);
exec_context_dump(&m->exec_context, f, prefix);
kill_context_dump(&m->kill_context, f, prefix);
@ -1093,7 +1093,7 @@ static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
unit_serialize_item(u, f, "reload-result", mount_result_to_string(m->reload_result));
if (m->control_pid > 0)
unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) m->control_pid);
unit_serialize_item_format(u, f, "control-pid", PID_FMT, m->control_pid);
if (m->control_command_id >= 0)
unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));

View File

@ -210,8 +210,8 @@ static int service_set_main_pid(Service *s, pid_t pid) {
if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
log_warning_unit(UNIT(s)->id,
"%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
UNIT(s)->id, (unsigned long) pid);
"%s: Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.",
UNIT(s)->id, pid);
s->main_pid_alien = true;
} else
@ -1309,15 +1309,15 @@ static void service_dump(Unit *u, FILE *f, const char *prefix) {
if (s->control_pid > 0)
fprintf(f,
"%sControl PID: %lu\n",
prefix, (unsigned long) s->control_pid);
"%sControl PID: "PID_FMT"\n",
prefix, s->control_pid);
if (s->main_pid > 0)
fprintf(f,
"%sMain PID: %lu\n"
"%sMain PID: "PID_FMT"\n"
"%sMain PID Known: %s\n"
"%sMain PID Alien: %s\n",
prefix, (unsigned long) s->main_pid,
prefix, s->main_pid,
prefix, yes_no(s->main_pid_known),
prefix, yes_no(s->main_pid_alien));
@ -1401,8 +1401,8 @@ static int service_load_pid_file(Service *s, bool may_warn) {
if (kill(pid, 0) < 0 && errno != EPERM) {
if (may_warn)
log_info_unit(UNIT(s)->id,
"PID %lu read from file %s does not exist.",
(unsigned long) pid, s->pid_file);
"PID "PID_FMT" read from file %s does not exist.",
pid, s->pid_file);
return -ESRCH;
}
@ -1411,13 +1411,13 @@ static int service_load_pid_file(Service *s, bool may_warn) {
return 0;
log_debug_unit(UNIT(s)->id,
"Main PID changing: %lu -> %lu",
(unsigned long) s->main_pid, (unsigned long) pid);
"Main PID changing: "PID_FMT" -> "PID_FMT,
s->main_pid, pid);
service_unwatch_main_pid(s);
s->main_pid_known = false;
} else
log_debug_unit(UNIT(s)->id,
"Main PID loaded: %lu", (unsigned long) pid);
"Main PID loaded: "PID_FMT, pid);
r = service_set_main_pid(s, pid);
if (r < 0)
@ -1427,8 +1427,8 @@ static int service_load_pid_file(Service *s, bool may_warn) {
if (r < 0) {
/* FIXME: we need to do something here */
log_warning_unit(UNIT(s)->id,
"Failed to watch PID %lu from service %s",
(unsigned long) pid, UNIT(s)->id);
"Failed to watch PID "PID_FMT" from service %s",
pid, UNIT(s)->id);
return r;
}
@ -1456,7 +1456,7 @@ static int service_search_main_pid(Service *s) {
return -ENOENT;
log_debug_unit(UNIT(s)->id,
"Main PID guessed: %lu", (unsigned long) pid);
"Main PID guessed: "PID_FMT, pid);
r = service_set_main_pid(s, pid);
if (r < 0)
return r;
@ -1465,8 +1465,8 @@ static int service_search_main_pid(Service *s) {
if (r < 0)
/* FIXME: we need to do something here */
log_warning_unit(UNIT(s)->id,
"Failed to watch PID %lu from service %s",
(unsigned long) pid, UNIT(s)->id);
"Failed to watch PID "PID_FMT" from service %s",
pid, UNIT(s)->id);
return r;
return 0;
@ -1763,13 +1763,13 @@ static int service_spawn(
}
if (s->main_pid > 0)
if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) {
r = -ENOMEM;
goto fail;
}
if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid()) < 0) {
r = -ENOMEM;
goto fail;
}
@ -2562,11 +2562,11 @@ static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
if (s->control_pid > 0)
unit_serialize_item_format(u, f, "control-pid", "%lu",
(unsigned long) s->control_pid);
unit_serialize_item_format(u, f, "control-pid", PID_FMT,
s->control_pid);
if (s->main_pid_known && s->main_pid > 0)
unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
unit_serialize_item_format(u, f, "main-pid", PID_FMT, s->main_pid);
unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
@ -2590,8 +2590,8 @@ static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
}
if (s->main_exec_status.pid > 0) {
unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu",
(unsigned long) s->main_exec_status.pid);
unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT,
s->main_exec_status.pid);
dual_timestamp_serialize(f, "main-exec-status-start",
&s->main_exec_status.start_timestamp);
dual_timestamp_serialize(f, "main-exec-status-exit",
@ -3343,20 +3343,20 @@ static void service_notify_message(Unit *u, pid_t pid, char **tags) {
assert(u);
log_debug_unit(u->id, "%s: Got notification message from PID %lu (%s...)",
u->id, (unsigned long) pid, tags && *tags ? tags[0] : "(empty)");
log_debug_unit(u->id, "%s: Got notification message from PID "PID_FMT" (%s...)",
u->id, pid, tags && *tags ? tags[0] : "(empty)");
if (s->notify_access == NOTIFY_NONE) {
log_warning_unit(u->id,
"%s: Got notification message from PID %lu, but reception is disabled.",
u->id, (unsigned long) pid);
"%s: Got notification message from PID "PID_FMT", but reception is disabled.",
u->id, pid);
return;
}
if (s->notify_access == NOTIFY_MAIN && s->main_pid != 0 && pid != s->main_pid) {
log_warning_unit(u->id,
"%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
u->id, (unsigned long) pid, (unsigned long) s->main_pid);
"%s: Got notification message from PID "PID_FMT", but reception only permitted for PID "PID_FMT,
u->id, pid, s->main_pid);
return;
}

View File

@ -27,6 +27,9 @@
typedef uint64_t usec_t;
typedef uint64_t nsec_t;
#define NSEC_FMT "%" PRIu64
#define USEC_FMT "%" PRIu64
#include "macro.h"
typedef struct dual_timestamp {

View File

@ -42,6 +42,22 @@
#include <mntent.h>
#include <sys/socket.h>
#if SIZEOF_PID_T == 4
# define PID_FMT "%" PRIu32
#elif SIZEOF_PID_T == 2
# define PID_FMT "%" PRIu16
#else
# error Unknown pid_t size
#endif
#if SIZEOF_UID_T == 4
# define UID_FMT "%" PRIu32
#elif SIZEOF_UID_T == 2
# define UID_FMT "%" PRIu16
#else
# error Unknown uid_t size
#endif
#include "macro.h"
#include "time-util.h"
@ -763,7 +779,7 @@ int unlink_noerrno(const char *path);
pid_t _pid_ = (pid); \
char *_r_; \
_r_ = alloca(sizeof("/proc/") -1 + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \
sprintf(_r_, "/proc/%lu/" field, (unsigned long) _pid_); \
sprintf(_r_, "/proc/"PID_FMT"/" field, _pid_); \
_r_; \
})