blob: 3a47d09e8a27e91171aef445c9c069308c7949f3 [file] [log] [blame]
Andrew Geisslerd5838332022-05-27 11:33:10 -05001From 48a791aae7a47a2a08e9e60c18054071a43b8cda Mon Sep 17 00:00:00 2001
Brad Bishop19323692019-04-05 15:28:33 -04002From: Chen Qi <Qi.Chen@windriver.com>
3Date: Mon, 25 Feb 2019 15:12:41 +0800
William A. Kennington IIIac69b482021-06-02 12:28:27 -07004Subject: [PATCH] Use uintmax_t for handling rlim_t
Brad Bishop19323692019-04-05 15:28:33 -04005
6PRIu{32,64} is not right format to represent rlim_t type
7therefore use %ju and typecast the rlim_t variables to
8uintmax_t.
9
10Fixes portablility errors like
11
12execute.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
18Upstream-Status: Denied [https://github.com/systemd/systemd/pull/7199]
19
20Signed-off-by: Khem Raj <raj.khem@gmail.com>
21[Rebased for v241]
22Signed-off-by: Chen Qi <Qi.Chen@windriver.com>
William A. Kennington IIIac69b482021-06-02 12:28:27 -070023
Brad Bishop19323692019-04-05 15:28:33 -040024---
25 src/basic/format-util.h | 8 +-------
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050026 src/basic/rlimit-util.c | 12 ++++++------
Brad Bishop19323692019-04-05 15:28:33 -040027 src/core/execute.c | 4 ++--
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050028 3 files changed, 9 insertions(+), 15 deletions(-)
Brad Bishop19323692019-04-05 15:28:33 -040029
Andrew Geisslerd5838332022-05-27 11:33:10 -050030diff --git a/src/basic/format-util.h b/src/basic/format-util.h
31index 8719df3e29..9becc96066 100644
Andrew Geisslerd1e89492021-02-12 15:35:20 -060032--- a/src/basic/format-util.h
33+++ b/src/basic/format-util.h
Andrew Geisslerd5838332022-05-27 11:33:10 -050034@@ -34,13 +34,7 @@ assert_cc(sizeof(gid_t) == sizeof(uint32_t));
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000035 # error Unknown timex member size
Brad Bishop19323692019-04-05 15:28:33 -040036 #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 Geisslerd5838332022-05-27 11:33:10 -050049diff --git a/src/basic/rlimit-util.c b/src/basic/rlimit-util.c
50index 33dfde9d6c..e018fd81fd 100644
Andrew Geisslerd1e89492021-02-12 15:35:20 -060051--- a/src/basic/rlimit-util.c
52+++ b/src/basic/rlimit-util.c
Andrew Geisslerd5838332022-05-27 11:33:10 -050053@@ -44,7 +44,7 @@ int setrlimit_closest(int resource, const struct rlimit *rlim) {
Andrew Geissler09036742021-06-25 14:25:14 -050054 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
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000060 return RET_NERRNO(setrlimit(resource, &fixed));
61 }
Andrew Geisslerd5838332022-05-27 11:33:10 -050062@@ -307,13 +307,13 @@ int rlimit_format(const struct rlimit *rl, char **ret) {
Brad Bishop19323692019-04-05 15:28:33 -040063 if (rl->rlim_cur >= RLIM_INFINITY && rl->rlim_max >= RLIM_INFINITY)
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000064 r = free_and_strdup(&s, "infinity");
Brad Bishop19323692019-04-05 15:28:33 -040065 else if (rl->rlim_cur >= RLIM_INFINITY)
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000066- r = asprintf(&s, "infinity:" RLIM_FMT, rl->rlim_max);
67+ r = asprintf(&s, "infinity:" RLIM_FMT, (uintmax_t)rl->rlim_max);
Brad Bishop19323692019-04-05 15:28:33 -040068 else if (rl->rlim_max >= RLIM_INFINITY)
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000069- r = asprintf(&s, RLIM_FMT ":infinity", rl->rlim_cur);
70+ r = asprintf(&s, RLIM_FMT ":infinity", (uintmax_t)rl->rlim_cur);
Brad Bishop19323692019-04-05 15:28:33 -040071 else if (rl->rlim_cur == rl->rlim_max)
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000072- r = asprintf(&s, RLIM_FMT, rl->rlim_cur);
73+ r = asprintf(&s, RLIM_FMT, (uintmax_t)rl->rlim_cur);
Brad Bishop19323692019-04-05 15:28:33 -040074 else
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000075- r = asprintf(&s, RLIM_FMT ":" RLIM_FMT, rl->rlim_cur, rl->rlim_max);
76+ r = asprintf(&s, RLIM_FMT ":" RLIM_FMT, (uintmax_t)rl->rlim_cur, (uintmax_t)rl->rlim_max);
77 if (r < 0)
Brad Bishop19323692019-04-05 15:28:33 -040078 return -ENOMEM;
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000079
80@@ -403,7 +403,7 @@ int rlimit_nofile_safe(void) {
Brad Bishop19323692019-04-05 15:28:33 -040081
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 Geisslerd5838332022-05-27 11:33:10 -050089diff --git a/src/core/execute.c b/src/core/execute.c
90index fccfb9268c..90f00e10a5 100644
Andrew Geisslerd1e89492021-02-12 15:35:20 -060091--- a/src/core/execute.c
92+++ b/src/core/execute.c
Andrew Geisslerd5838332022-05-27 11:33:10 -050093@@ -5633,9 +5633,9 @@ void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix) {
Andrew Geisslerd1e89492021-02-12 15:35:20 -060094 for (unsigned i = 0; i < RLIM_NLIMITS; i++)
Brad Bishop19323692019-04-05 15:28:33 -040095 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) {