William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 1 | From dc40487e0ac26e3ca317429b9c3e8f01772de60a Mon Sep 17 00:00:00 2001 |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 2 | From: Chen Qi <Qi.Chen@windriver.com> |
| 3 | Date: Mon, 25 Feb 2019 15:12:41 +0800 |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 4 | Subject: [PATCH] Use uintmax_t for handling rlim_t |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 5 | |
| 6 | PRIu{32,64} is not right format to represent rlim_t type |
| 7 | therefore use %ju and typecast the rlim_t variables to |
| 8 | uintmax_t. |
| 9 | |
| 10 | Fixes portablility errors like |
| 11 | |
| 12 | execute.c:3446:36: error: format '%lu' expects argument of type 'long unsigned int', but argument 5 has type 'rlim_t {aka long long unsigned int}' [-Werror=format=] |
| 13 | | fprintf(f, "%s%s: " RLIM_FMT "\n", |
| 14 | | ^~~~~~~~ |
| 15 | | prefix, rlimit_to_string(i), c->rlimit[i]->rlim_max); |
| 16 | | ~~~~~~~~~~~~~~~~~~~~~~ |
| 17 | |
| 18 | Upstream-Status: Denied [https://github.com/systemd/systemd/pull/7199] |
| 19 | |
| 20 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 21 | [Rebased for v241] |
| 22 | Signed-off-by: Chen Qi <Qi.Chen@windriver.com> |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 23 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 24 | --- |
| 25 | src/basic/format-util.h | 8 +------- |
| 26 | src/basic/rlimit-util.c | 10 +++++----- |
| 27 | src/core/execute.c | 4 ++-- |
| 28 | 3 files changed, 8 insertions(+), 14 deletions(-) |
| 29 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 30 | diff --git a/src/basic/format-util.h b/src/basic/format-util.h |
| 31 | index b7e18768e3..3195ab205d 100644 |
| 32 | --- a/src/basic/format-util.h |
| 33 | +++ b/src/basic/format-util.h |
| 34 | @@ -32,13 +32,7 @@ assert_cc(sizeof(gid_t) == sizeof(uint32_t)); |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 35 | # define PRI_TIMEX "li" |
| 36 | #endif |
| 37 | |
| 38 | -#if SIZEOF_RLIM_T == 8 |
| 39 | -# define RLIM_FMT "%" PRIu64 |
| 40 | -#elif SIZEOF_RLIM_T == 4 |
| 41 | -# define RLIM_FMT "%" PRIu32 |
| 42 | -#else |
| 43 | -# error Unknown rlim_t size |
| 44 | -#endif |
| 45 | +#define RLIM_FMT "%ju" |
| 46 | |
| 47 | #if SIZEOF_DEV_T == 8 |
| 48 | # define DEV_FMT "%" PRIu64 |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 49 | diff --git a/src/basic/rlimit-util.c b/src/basic/rlimit-util.c |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 50 | index 23d108d5df..b037734ee3 100644 |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 51 | --- a/src/basic/rlimit-util.c |
| 52 | +++ b/src/basic/rlimit-util.c |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 53 | @@ -43,7 +43,7 @@ int setrlimit_closest(int resource, const struct rlimit *rlim) { |
| 54 | fixed.rlim_max == highest.rlim_max) |
| 55 | return 0; |
| 56 | |
| 57 | - log_debug("Failed at setting rlimit " RLIM_FMT " for resource RLIMIT_%s. Will attempt setting value " RLIM_FMT " instead.", rlim->rlim_max, rlimit_to_string(resource), fixed.rlim_max); |
| 58 | + log_debug("Failed at setting rlimit " RLIM_FMT " for resource RLIMIT_%s. Will attempt setting value " RLIM_FMT " instead.", (uintmax_t)rlim->rlim_max, rlimit_to_string(resource), (uintmax_t)fixed.rlim_max); |
| 59 | |
| 60 | if (setrlimit(resource, &fixed) < 0) |
| 61 | return -errno; |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 62 | @@ -308,13 +308,13 @@ int rlimit_format(const struct rlimit *rl, char **ret) { |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 63 | if (rl->rlim_cur >= RLIM_INFINITY && rl->rlim_max >= RLIM_INFINITY) |
| 64 | s = strdup("infinity"); |
| 65 | else if (rl->rlim_cur >= RLIM_INFINITY) |
| 66 | - (void) asprintf(&s, "infinity:" RLIM_FMT, rl->rlim_max); |
| 67 | + (void) asprintf(&s, "infinity:" RLIM_FMT, (uintmax_t)rl->rlim_max); |
| 68 | else if (rl->rlim_max >= RLIM_INFINITY) |
| 69 | - (void) asprintf(&s, RLIM_FMT ":infinity", rl->rlim_cur); |
| 70 | + (void) asprintf(&s, RLIM_FMT ":infinity", (uintmax_t)rl->rlim_cur); |
| 71 | else if (rl->rlim_cur == rl->rlim_max) |
| 72 | - (void) asprintf(&s, RLIM_FMT, rl->rlim_cur); |
| 73 | + (void) asprintf(&s, RLIM_FMT, (uintmax_t)rl->rlim_cur); |
| 74 | else |
| 75 | - (void) asprintf(&s, RLIM_FMT ":" RLIM_FMT, rl->rlim_cur, rl->rlim_max); |
| 76 | + (void) asprintf(&s, RLIM_FMT ":" RLIM_FMT, (uintmax_t)rl->rlim_cur, (uintmax_t)rl->rlim_max); |
| 77 | |
| 78 | if (!s) |
| 79 | return -ENOMEM; |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 80 | @@ -405,7 +405,7 @@ int rlimit_nofile_safe(void) { |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 81 | |
| 82 | rl.rlim_cur = FD_SETSIZE; |
| 83 | if (setrlimit(RLIMIT_NOFILE, &rl) < 0) |
| 84 | - return log_debug_errno(errno, "Failed to lower RLIMIT_NOFILE's soft limit to " RLIM_FMT ": %m", rl.rlim_cur); |
| 85 | + return log_debug_errno(errno, "Failed to lower RLIMIT_NOFILE's soft limit to " RLIM_FMT ": %m", (uintmax_t)rl.rlim_cur); |
| 86 | |
| 87 | return 1; |
| 88 | } |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 89 | diff --git a/src/core/execute.c b/src/core/execute.c |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 90 | index f82fc294c0..4696d055a8 100644 |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 91 | --- a/src/core/execute.c |
| 92 | +++ b/src/core/execute.c |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 93 | @@ -5370,9 +5370,9 @@ void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix) { |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 94 | for (unsigned i = 0; i < RLIM_NLIMITS; i++) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 95 | if (c->rlimit[i]) { |
| 96 | fprintf(f, "%sLimit%s: " RLIM_FMT "\n", |
| 97 | - prefix, rlimit_to_string(i), c->rlimit[i]->rlim_max); |
| 98 | + prefix, rlimit_to_string(i), (uintmax_t)c->rlimit[i]->rlim_max); |
| 99 | fprintf(f, "%sLimit%sSoft: " RLIM_FMT "\n", |
| 100 | - prefix, rlimit_to_string(i), c->rlimit[i]->rlim_cur); |
| 101 | + prefix, rlimit_to_string(i), (uintmax_t)c->rlimit[i]->rlim_cur); |
| 102 | } |
| 103 | |
| 104 | if (c->ioprio_set) { |