X Tutup
Skip to content

Commit baaa35a

Browse files
committed
coccinelle: make use of SYNTHETIC_ERRNO
Ideally, coccinelle would strip unnecessary braces too. But I do not see any option in coccinelle for this, so instead, I edited the patch text using search&replace to remove the braces. Unfortunately this is not fully automatic, in particular it didn't deal well with if-else-if-else blocks and ifdefs, so there is an increased likelikehood be some bugs in such spots. I also removed part of the patch that coccinelle generated for udev, where we returns -1 for failure. This should be fixed independently.
1 parent 52d8669 commit baaa35a

File tree

129 files changed

+1946
-2325
lines changed

Some content is hidden

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

129 files changed

+1946
-2325
lines changed

coccinelle/synthetic-errno.cocci

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@@
2+
expression e;
3+
expression list args;
4+
@@
5+
- log_debug(args);
6+
- return -e;
7+
+ return log_debug_errno(SYNTHETIC_ERRNO(e), args);
8+
@@
9+
expression e;
10+
expression list args;
11+
@@
12+
- log_info(args);
13+
- return -e;
14+
+ return log_info_errno(SYNTHETIC_ERRNO(e), args);
15+
@@
16+
expression e;
17+
expression list args;
18+
@@
19+
- log_notice(args);
20+
- return -e;
21+
+ return log_notice_errno(SYNTHETIC_ERRNO(e), args);
22+
@@
23+
expression e;
24+
expression list args;
25+
@@
26+
- log_error(args);
27+
- return -e;
28+
+ return log_error_errno(SYNTHETIC_ERRNO(e), args);
29+
@@
30+
expression e;
31+
expression list args;
32+
@@
33+
- log_emergency(args);
34+
- return -e;
35+
+ return log_emergency_errno(SYNTHETIC_ERRNO(e), args);

src/ac-power/ac-power.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ static int parse_argv(int argc, char *argv[]) {
5656
assert_not_reached("Unhandled option");
5757
}
5858

59-
if (optind < argc) {
60-
log_error("%s takes no arguments.", program_invocation_short_name);
61-
return -EINVAL;
62-
}
59+
if (optind < argc)
60+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
61+
"%s takes no arguments.",
62+
program_invocation_short_name);
6363

6464
return 1;
6565
}

src/activate/activate.c

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,9 @@ static int exec_process(const char* name, char **argv, char **env, int start_fd,
122122
char **s;
123123
int r;
124124

125-
if (arg_inetd && n_fds != 1) {
126-
log_error("--inetd only supported for single file descriptors.");
127-
return -EINVAL;
128-
}
125+
if (arg_inetd && n_fds != 1)
126+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
127+
"--inetd only supported for single file descriptors.");
129128

130129
length = strv_length(arg_setenv);
131130

@@ -384,19 +383,17 @@ static int parse_argv(int argc, char *argv[]) {
384383
break;
385384

386385
case 'd':
387-
if (arg_socket_type == SOCK_SEQPACKET) {
388-
log_error("--datagram may not be combined with --seqpacket.");
389-
return -EINVAL;
390-
}
386+
if (arg_socket_type == SOCK_SEQPACKET)
387+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
388+
"--datagram may not be combined with --seqpacket.");
391389

392390
arg_socket_type = SOCK_DGRAM;
393391
break;
394392

395393
case ARG_SEQPACKET:
396-
if (arg_socket_type == SOCK_DGRAM) {
397-
log_error("--seqpacket may not be combined with --datagram.");
398-
return -EINVAL;
399-
}
394+
if (arg_socket_type == SOCK_DGRAM)
395+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
396+
"--seqpacket may not be combined with --datagram.");
400397

401398
arg_socket_type = SOCK_SEQPACKET;
402399
break;
@@ -448,17 +445,15 @@ static int parse_argv(int argc, char *argv[]) {
448445
assert_not_reached("Unhandled option");
449446
}
450447

451-
if (optind == argc) {
452-
log_error("%s: command to execute is missing.",
453-
program_invocation_short_name);
454-
return -EINVAL;
455-
}
448+
if (optind == argc)
449+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
450+
"%s: command to execute is missing.",
451+
program_invocation_short_name);
456452

457-
if (arg_socket_type == SOCK_DGRAM && arg_accept) {
458-
log_error("Datagram sockets do not accept connections. "
459-
"The --datagram and --accept options may not be combined.");
460-
return -EINVAL;
461-
}
453+
if (arg_socket_type == SOCK_DGRAM && arg_accept)
454+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
455+
"Datagram sockets do not accept connections. "
456+
"The --datagram and --accept options may not be combined.");
462457

463458
arg_args = argv + optind;
464459

src/analyze/analyze.c

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,10 +1360,10 @@ static int cat_config(int argc, char *argv[], void *userdata) {
13601360
break;
13611361
}
13621362

1363-
if (!t) {
1364-
log_error("Path %s does not start with any known prefix.", *arg);
1365-
return -EINVAL;
1366-
}
1363+
if (!t)
1364+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1365+
"Path %s does not start with any known prefix.",
1366+
*arg);
13671367
} else
13681368
t = *arg;
13691369

@@ -1638,10 +1638,9 @@ static int dump_syscall_filters(int argc, char *argv[], void *userdata) {
16381638
}
16391639

16401640
#else
1641-
static int dump_syscall_filters(int argc, char *argv[], void *userdata) {
1642-
log_error("Not compiled with syscall filters, sorry.");
1643-
return -EOPNOTSUPP;
1644-
}
1641+
static int dump_syscall_filters(int argc, char *argv[], void *userdata)
1642+
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1643+
"Not compiled with syscall filters, sorry.");
16451644
#endif
16461645

16471646
static int dump_timespan(int argc, char *argv[], void *userdata) {
@@ -1948,10 +1947,9 @@ static int parse_argv(int argc, char *argv[]) {
19481947
case ARG_MAN:
19491948
if (optarg) {
19501949
r = parse_boolean(optarg);
1951-
if (r < 0) {
1952-
log_error("Failed to parse --man= argument.");
1953-
return -EINVAL;
1954-
}
1950+
if (r < 0)
1951+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1952+
"Failed to parse --man= argument.");
19551953

19561954
arg_man = r;
19571955
} else
@@ -1962,10 +1960,9 @@ static int parse_argv(int argc, char *argv[]) {
19621960
case ARG_GENERATORS:
19631961
if (optarg) {
19641962
r = parse_boolean(optarg);
1965-
if (r < 0) {
1966-
log_error("Failed to parse --generators= argument.");
1967-
return -EINVAL;
1968-
}
1963+
if (r < 0)
1964+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1965+
"Failed to parse --generators= argument.");
19691966

19701967
arg_generators = r;
19711968
} else
@@ -1981,15 +1978,13 @@ static int parse_argv(int argc, char *argv[]) {
19811978
}
19821979

19831980
if (arg_scope == UNIT_FILE_GLOBAL &&
1984-
!STR_IN_SET(argv[optind] ?: "time", "dot", "unit-paths", "verify")) {
1985-
log_error("Option --global only makes sense with verbs dot, unit-paths, verify.");
1986-
return -EINVAL;
1987-
}
1981+
!STR_IN_SET(argv[optind] ?: "time", "dot", "unit-paths", "verify"))
1982+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1983+
"Option --global only makes sense with verbs dot, unit-paths, verify.");
19881984

1989-
if (arg_root && !streq_ptr(argv[optind], "cat-config")) {
1990-
log_error("Option --root is only supported for cat-config right now.");
1991-
return -EINVAL;
1992-
}
1985+
if (arg_root && !streq_ptr(argv[optind], "cat-config"))
1986+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1987+
"Option --root is only supported for cat-config right now.");
19931988

19941989
return 1; /* work to do */
19951990
}

src/ask-password/ask-password.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ static int parse_argv(int argc, char *argv[]) {
102102
break;
103103

104104
case ARG_TIMEOUT:
105-
if (parse_sec(optarg, &arg_timeout) < 0) {
106-
log_error("Failed to parse --timeout parameter %s", optarg);
107-
return -EINVAL;
108-
}
105+
if (parse_sec(optarg, &arg_timeout) < 0)
106+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
107+
"Failed to parse --timeout parameter %s",
108+
optarg);
109109
break;
110110

111111
case ARG_ECHO:

src/basic/cgroup-util.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,11 +2545,10 @@ static int cg_unified_update(void) {
25452545
unified_cache = CGROUP_UNIFIED_NONE;
25462546
}
25472547
}
2548-
} else {
2549-
log_debug("Unknown filesystem type %llx mounted on /sys/fs/cgroup.",
2550-
(unsigned long long) fs.f_type);
2551-
return -ENOMEDIUM;
2552-
}
2548+
} else
2549+
return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM),
2550+
"Unknown filesystem type %llx mounted on /sys/fs/cgroup.",
2551+
(unsigned long long)fs.f_type);
25532552

25542553
return 0;
25552554
}

src/basic/fileio.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -638,16 +638,18 @@ static int check_utf8ness_and_warn(
638638
_cleanup_free_ char *p = NULL;
639639

640640
p = utf8_escape_invalid(key);
641-
log_error("%s:%u: invalid UTF-8 in key '%s', ignoring.", strna(filename), line, p);
642-
return -EINVAL;
641+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
642+
"%s:%u: invalid UTF-8 in key '%s', ignoring.",
643+
strna(filename), line, p);
643644
}
644645

645646
if (value && !utf8_is_valid(value)) {
646647
_cleanup_free_ char *p = NULL;
647648

648649
p = utf8_escape_invalid(value);
649-
log_error("%s:%u: invalid UTF-8 value for key %s: '%s', ignoring.", strna(filename), line, key, p);
650-
return -EINVAL;
650+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
651+
"%s:%u: invalid UTF-8 value for key %s: '%s', ignoring.",
652+
strna(filename), line, key, p);
651653
}
652654

653655
return 0;

src/basic/log.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ void log_assert_failed_return_realm(
202202
#define log_full_errno_realm(realm, level, error, ...) \
203203
({ \
204204
int _level = (level), _e = (error), _realm = (realm); \
205-
(log_get_max_level_realm(_realm) >= LOG_PRI(_level)) \
205+
(log_get_max_level_realm(_realm) >= LOG_PRI(_level)) \
206206
? log_internal_realm(LOG_REALM_PLUS_LEVEL(_realm, _level), _e, \
207207
__FILE__, __LINE__, __func__, __VA_ARGS__) \
208-
: -abs(_e); \
208+
: -ERRNO_VALUE(_e); \
209209
})
210210

211211
#define log_full_errno(level, error, ...) \

src/basic/process-util.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,8 +1227,7 @@ int must_be_root(void) {
12271227
if (geteuid() == 0)
12281228
return 0;
12291229

1230-
log_error("Need to be root.");
1231-
return -EPERM;
1230+
return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Need to be root.");
12321231
}
12331232

12341233
int safe_fork_full(

src/basic/rm-rf.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,10 @@ int rm_rf(const char *path, RemoveFlags flags) {
168168
/* We refuse to clean the root file system with this
169169
* call. This is extra paranoia to never cause a really
170170
* seriously broken system. */
171-
if (path_equal_or_files_same(path, "/", AT_SYMLINK_NOFOLLOW)) {
172-
log_error("Attempted to remove entire root file system (\"%s\"), and we can't allow that.", path);
173-
return -EPERM;
174-
}
171+
if (path_equal_or_files_same(path, "/", AT_SYMLINK_NOFOLLOW))
172+
return log_error_errno(SYNTHETIC_ERRNO(EPERM),
173+
"Attempted to remove entire root file system (\"%s\"), and we can't allow that.",
174+
path);
175175

176176
if (FLAGS_SET(flags, REMOVE_SUBVOLUME | REMOVE_ROOT | REMOVE_PHYSICAL)) {
177177
/* Try to remove as subvolume first */
@@ -194,10 +194,10 @@ int rm_rf(const char *path, RemoveFlags flags) {
194194
if (statfs(path, &s) < 0)
195195
return -errno;
196196

197-
if (is_physical_fs(&s)) {
198-
log_error("Attempted to remove files from a disk file system under \"%s\", refusing.", path);
199-
return -EPERM;
200-
}
197+
if (is_physical_fs(&s))
198+
return log_error_errno(SYNTHETIC_ERRNO(EPERM),
199+
"Attempted to remove files from a disk file system under \"%s\", refusing.",
200+
path);
201201
}
202202

203203
if ((flags & REMOVE_ROOT) && !(flags & REMOVE_ONLY_DIRECTORIES))

0 commit comments

Comments
 (0)
X Tutup