X Tutup
Skip to content

Commit 8389fd1

Browse files
authored
Merge pull request systemd#20138 from keszybz/coding-style-variable-decls
A coding style tweak and checking of sd_notify() calls and voidification of pager_open()
2 parents 5f035b1 + 384c2c3 commit 8389fd1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+191
-169
lines changed

docs/CODING_STYLE.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,25 +153,34 @@ SPDX-License-Identifier: LGPL-2.1-or-later
153153
## Using C Constructs
154154
155155
- Allocate local variables where it makes sense: at the top of the block, or at
156-
the point where they can be initialized. `r` is typically used for a local
157-
state variable, but should almost always be declared at the top of the
158-
function.
156+
the point where they can be initialized. Avoid huge variable declaration
157+
lists at the top of the function.
158+
159+
As an exception, `r` is typically used for a local state variable, but should
160+
almost always be declared as the last variable at the top of the function.
159161
160162
```c
161163
{
162-
uint64_t a, b;
164+
uint64_t a;
163165
int r;
164166
165-
a = frobnicate();
166-
b = a + 5;
167+
r = frobnicate(&a);
168+
if (r < 0)
169+
170+
171+
uint64_t b = a + 1, c;
167172
168-
r = do_something();
173+
r = foobarify(a, b, &c);
169174
if (r < 0)
170175
176+
177+
const char *pretty = prettify(a, b, c);
178+
171179
}
172180
```
173181

174-
- Do not mix function invocations with variable definitions in one line.
182+
- Do not mix multiple variable definitions with function invocations or
183+
complicated expressions:
175184

176185
```c
177186
{
@@ -225,7 +234,7 @@ SPDX-License-Identifier: LGPL-2.1-or-later
225234
- To determine the length of a constant string `"foo"`, don't bother with
226235
`sizeof("foo")-1`, please use `strlen()` instead (both gcc and clang optimize
227236
the call away for fixed strings). The only exception is when declaring an
228-
array. In that case use STRLEN, which evaluates to a static constant and
237+
array. In that case use `STRLEN()`, which evaluates to a static constant and
229238
doesn't force the compiler to create a VLA.
230239

231240
- Please use C's downgrade-to-bool feature only for expressions that are

src/analyze/analyze.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ static int analyze_critical_chain(int argc, char *argv[], void *userdata) {
10511051
}
10521052
unit_times_hashmap = h;
10531053

1054-
(void) pager_open(arg_pager_flags);
1054+
pager_open(arg_pager_flags);
10551055

10561056
puts("The time when unit became active or started is printed after the \"@\" character.\n"
10571057
"The time the unit took to start is printed after the \"+\" character.\n");
@@ -1121,7 +1121,7 @@ static int analyze_blame(int argc, char *argv[], void *userdata) {
11211121
return table_log_add_error(r);
11221122
}
11231123

1124-
(void) pager_open(arg_pager_flags);
1124+
pager_open(arg_pager_flags);
11251125

11261126
return table_print(table, NULL);
11271127
}
@@ -1349,7 +1349,7 @@ static int dump(int argc, char *argv[], void *userdata) {
13491349
if (r < 0)
13501350
return bus_log_connect_error(r, arg_transport);
13511351

1352-
(void) pager_open(arg_pager_flags);
1352+
pager_open(arg_pager_flags);
13531353

13541354
if (!sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD))
13551355
return dump_fallback(bus);
@@ -1376,7 +1376,7 @@ static int cat_config(int argc, char *argv[], void *userdata) {
13761376
char **arg, **list;
13771377
int r;
13781378

1379-
(void) pager_open(arg_pager_flags);
1379+
pager_open(arg_pager_flags);
13801380

13811381
list = strv_skip(argv, 1);
13821382
STRV_FOREACH(arg, list) {
@@ -1523,7 +1523,7 @@ static int dump_exit_status(int argc, char *argv[], void *userdata) {
15231523
return table_log_add_error(r);
15241524
}
15251525

1526-
(void) pager_open(arg_pager_flags);
1526+
pager_open(arg_pager_flags);
15271527

15281528
return table_print(table, NULL);
15291529
}
@@ -1568,7 +1568,7 @@ static int dump_capabilities(int argc, char *argv[], void *userdata) {
15681568
(void) table_set_sort(table, (size_t) 1);
15691569
}
15701570

1571-
(void) pager_open(arg_pager_flags);
1571+
pager_open(arg_pager_flags);
15721572

15731573
return table_print(table, NULL);
15741574
}
@@ -1652,7 +1652,7 @@ static void dump_syscall_filter(const SyscallFilterSet *set) {
16521652
static int dump_syscall_filters(int argc, char *argv[], void *userdata) {
16531653
bool first = true;
16541654

1655-
(void) pager_open(arg_pager_flags);
1655+
pager_open(arg_pager_flags);
16561656

16571657
if (strv_isempty(strv_skip(argv, 1))) {
16581658
_cleanup_set_free_ Set *kernel = NULL, *known = NULL;
@@ -1824,7 +1824,7 @@ static int dump_filesystems(int argc, char *argv[], void *userdata) {
18241824
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Not compiled with libbpf support, sorry.");
18251825
#endif
18261826

1827-
(void) pager_open(arg_pager_flags);
1827+
pager_open(arg_pager_flags);
18281828

18291829
if (strv_isempty(strv_skip(argv, 1))) {
18301830
_cleanup_set_free_ Set *kernel = NULL, *known = NULL;
@@ -2270,7 +2270,7 @@ static int do_security(int argc, char *argv[], void *userdata) {
22702270
if (r < 0)
22712271
return bus_log_connect_error(r, arg_transport);
22722272

2273-
(void) pager_open(arg_pager_flags);
2273+
pager_open(arg_pager_flags);
22742274

22752275
if (arg_security_policy) {
22762276
r = json_parse_file(/*f=*/ NULL, arg_security_policy, /*flags=*/ 0, &policy, &line, &column);
@@ -2309,7 +2309,7 @@ static int help(int argc, char *argv[], void *userdata) {
23092309
_cleanup_free_ char *link = NULL, *dot_link = NULL;
23102310
int r;
23112311

2312-
(void) pager_open(arg_pager_flags);
2312+
pager_open(arg_pager_flags);
23132313

23142314
r = terminal_urlify_man("systemd-analyze", "1", &link);
23152315
if (r < 0)

src/binfmt/binfmt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ static int run(int argc, char *argv[]) {
217217
return log_error_errno(r, "Failed to enumerate binfmt.d files: %m");
218218

219219
if (arg_cat_config) {
220-
(void) pager_open(arg_pager_flags);
220+
pager_open(arg_pager_flags);
221221

222222
return cat_files(NULL, files, 0);
223223
}

src/boot/bootctl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,7 @@ static int verb_status(int argc, char *argv[], void *userdata) {
12861286
r = 0; /* If we couldn't determine the path, then don't consider that a problem from here on, just show what we
12871287
* can show */
12881288

1289-
(void) pager_open(arg_pager_flags);
1289+
pager_open(arg_pager_flags);
12901290

12911291
if (is_efi_boot()) {
12921292
static const struct {
@@ -1444,7 +1444,7 @@ static int verb_list(int argc, char *argv[], void *userdata) {
14441444
if (config.n_entries == 0)
14451445
log_info("No boot loader entries found.");
14461446
else {
1447-
(void) pager_open(arg_pager_flags);
1447+
pager_open(arg_pager_flags);
14481448

14491449
printf("Boot Loader Entries:\n");
14501450

src/busctl/busctl.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ static int tree_one(sd_bus *bus, const char *service) {
486486
return log_oom();
487487
}
488488

489-
(void) pager_open(arg_pager_flags);
489+
pager_open(arg_pager_flags);
490490

491491
l = set_get_strv(done);
492492
if (!l)
@@ -526,7 +526,7 @@ static int tree(int argc, char **argv, void *userdata) {
526526
if (r < 0)
527527
return log_error_errno(r, "Failed to get name list: %m");
528528

529-
(void) pager_open(arg_pager_flags);
529+
pager_open(arg_pager_flags);
530530

531531
STRV_FOREACH(i, names) {
532532
int q;
@@ -556,7 +556,7 @@ static int tree(int argc, char **argv, void *userdata) {
556556
printf("\n");
557557

558558
if (argv[2]) {
559-
(void) pager_open(arg_pager_flags);
559+
pager_open(arg_pager_flags);
560560
printf("Service %s%s%s:\n", ansi_highlight(), *i, ansi_normal());
561561
}
562562

@@ -979,7 +979,7 @@ static int introspect(int argc, char **argv, void *userdata) {
979979

980980
if (arg_xml_interface) {
981981
/* Just dump the received XML and finish */
982-
(void) pager_open(arg_pager_flags);
982+
pager_open(arg_pager_flags);
983983
puts(xml);
984984
return 0;
985985
}
@@ -1098,7 +1098,7 @@ static int introspect(int argc, char **argv, void *userdata) {
10981098

10991099
typesafe_qsort(sorted, k, member_compare_funcp);
11001100

1101-
(void) pager_open(arg_pager_flags);
1101+
pager_open(arg_pager_flags);
11021102

11031103
if (arg_legend)
11041104
printf("%-*s %-*s %-*s %-*s %s\n",
@@ -1358,7 +1358,7 @@ static int status(int argc, char **argv, void *userdata) {
13581358
if (r < 0)
13591359
return r;
13601360

1361-
(void) pager_open(arg_pager_flags);
1361+
pager_open(arg_pager_flags);
13621362

13631363
if (!isempty(argv[1])) {
13641364
r = parse_pid(argv[1], &pid);
@@ -2038,7 +2038,7 @@ static int call(int argc, char **argv, void *userdata) {
20382038
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
20392039

20402040
if (arg_json_format_flags & (JSON_FORMAT_PRETTY|JSON_FORMAT_PRETTY_AUTO))
2041-
(void) pager_open(arg_pager_flags);
2041+
pager_open(arg_pager_flags);
20422042

20432043
r = json_transform_message(reply, &v);
20442044
if (r < 0)
@@ -2047,7 +2047,7 @@ static int call(int argc, char **argv, void *userdata) {
20472047
json_variant_dump(v, arg_json_format_flags, NULL, NULL);
20482048

20492049
} else if (arg_verbose) {
2050-
(void) pager_open(arg_pager_flags);
2050+
pager_open(arg_pager_flags);
20512051

20522052
r = sd_bus_message_dump(reply, stdout, 0);
20532053
if (r < 0)
@@ -2147,7 +2147,7 @@ static int get_property(int argc, char **argv, void *userdata) {
21472147
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
21482148

21492149
if (arg_json_format_flags & (JSON_FORMAT_PRETTY|JSON_FORMAT_PRETTY_AUTO))
2150-
(void) pager_open(arg_pager_flags);
2150+
pager_open(arg_pager_flags);
21512151

21522152
r = json_transform_variant(reply, contents, &v);
21532153
if (r < 0)
@@ -2156,7 +2156,7 @@ static int get_property(int argc, char **argv, void *userdata) {
21562156
json_variant_dump(v, arg_json_format_flags, NULL, NULL);
21572157

21582158
} else if (arg_verbose) {
2159-
(void) pager_open(arg_pager_flags);
2159+
pager_open(arg_pager_flags);
21602160

21612161
r = sd_bus_message_dump(reply, stdout, SD_BUS_MESSAGE_DUMP_SUBTREE_ONLY);
21622162
if (r < 0)
@@ -2233,7 +2233,7 @@ static int help(void) {
22332233
if (r < 0)
22342234
return log_oom();
22352235

2236-
(void) pager_open(arg_pager_flags);
2236+
pager_open(arg_pager_flags);
22372237

22382238
printf("%s [OPTIONS...] COMMAND ...\n\n"
22392239
"%sIntrospect the D-Bus IPC bus.%s\n"

src/cgls/cgls.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ static int run(int argc, char *argv[]) {
191191
if (r <= 0)
192192
return r;
193193

194-
r = pager_open(arg_pager_flags);
195-
if (r > 0 && arg_full < 0)
194+
pager_open(arg_pager_flags);
195+
if (arg_full < 0 && pager_have())
196196
arg_full = true;
197197

198198
if (arg_full > 0)

src/core/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2902,7 +2902,7 @@ int main(int argc, char *argv[]) {
29022902
goto finish;
29032903

29042904
if (IN_SET(arg_action, ACTION_TEST, ACTION_HELP, ACTION_DUMP_CONFIGURATION_ITEMS, ACTION_DUMP_BUS_PROPERTIES, ACTION_BUS_INTROSPECT))
2905-
(void) pager_open(arg_pager_flags);
2905+
pager_open(arg_pager_flags);
29062906

29072907
if (arg_action != ACTION_RUN)
29082908
skip_setup = true;

src/core/manager.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3457,27 +3457,38 @@ static void manager_notify_finished(Manager *m) {
34573457
}
34583458

34593459
static void user_manager_send_ready(Manager *m) {
3460+
int r;
3461+
34603462
assert(m);
34613463

34623464
/* We send READY=1 on reaching basic.target only when running in --user mode. */
34633465
if (!MANAGER_IS_USER(m) || m->ready_sent)
34643466
return;
34653467

3466-
sd_notifyf(false,
3467-
"READY=1\n"
3468-
"STATUS=Reached " SPECIAL_BASIC_TARGET ".");
3468+
r = sd_notify(false,
3469+
"READY=1\n"
3470+
"STATUS=Reached " SPECIAL_BASIC_TARGET ".");
3471+
if (r < 0)
3472+
log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
3473+
34693474
m->ready_sent = true;
34703475
m->status_ready = false;
34713476
}
34723477

34733478
static void manager_send_ready(Manager *m) {
3479+
int r;
3480+
34743481
if (m->ready_sent && m->status_ready)
34753482
/* Skip the notification if nothing changed. */
34763483
return;
34773484

3478-
sd_notifyf(false,
3479-
"%sSTATUS=Ready.",
3480-
m->ready_sent ? "READY=1\n" : "");
3485+
r = sd_notify(false,
3486+
"READY=1\n"
3487+
"STATUS=Ready.");
3488+
if (r < 0)
3489+
log_full_errno(m->ready_sent ? LOG_DEBUG : LOG_WARNING, r,
3490+
"Failed to send readiness notification, ignoring: %m");
3491+
34813492
m->ready_sent = m->status_ready = true;
34823493
}
34833494

src/coredump/coredumpctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ static int dump_list(int argc, char **argv, void *userdata) {
824824

825825
(void) table_set_empty_string(t, "-");
826826
} else
827-
(void) pager_open(arg_pager_flags);
827+
pager_open(arg_pager_flags);
828828

829829
/* "info" without pattern implies "-1" */
830830
if ((arg_rows_max == 1 && arg_reverse) || (verb_is_info && argc == 1)) {

src/delta/delta.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ static int run(int argc, char *argv[]) {
649649
else if (arg_diff)
650650
arg_flags |= SHOW_OVERRIDDEN;
651651

652-
(void) pager_open(arg_pager_flags);
652+
pager_open(arg_pager_flags);
653653

654654
if (optind < argc) {
655655
int i;

0 commit comments

Comments
 (0)
X Tutup