Skip to content

Commit c4e4bb0

Browse files
Merge pull request #1699 from AkihiroSuda/indent-c
make: validate C format
2 parents 9f9c962 + dd5eb3b commit c4e4bb0

File tree

3 files changed

+96
-54
lines changed

3 files changed

+96
-54
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ clean:
112112

113113
validate:
114114
script/validate-gofmt
115+
script/validate-c
115116
$(GO) vet $(allpackages)
116117

117118
ci: validate test release

libcontainer/nsenter/nsexec.c

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <sys/types.h>
2323
#include <sys/wait.h>
2424

25-
2625
#include <linux/limits.h>
2726
#include <linux/netlink.h>
2827
#include <linux/types.h>
@@ -32,15 +31,15 @@
3231

3332
/* Synchronisation values. */
3433
enum sync_t {
35-
SYNC_USERMAP_PLS = 0x40, /* Request parent to map our users. */
36-
SYNC_USERMAP_ACK = 0x41, /* Mapping finished by the parent. */
37-
SYNC_RECVPID_PLS = 0x42, /* Tell parent we're sending the PID. */
38-
SYNC_RECVPID_ACK = 0x43, /* PID was correctly received by parent. */
39-
SYNC_GRANDCHILD = 0x44, /* The grandchild is ready to run. */
40-
SYNC_CHILD_READY = 0x45, /* The child or grandchild is ready to return. */
34+
SYNC_USERMAP_PLS = 0x40, /* Request parent to map our users. */
35+
SYNC_USERMAP_ACK = 0x41, /* Mapping finished by the parent. */
36+
SYNC_RECVPID_PLS = 0x42, /* Tell parent we're sending the PID. */
37+
SYNC_RECVPID_ACK = 0x43, /* PID was correctly received by parent. */
38+
SYNC_GRANDCHILD = 0x44, /* The grandchild is ready to run. */
39+
SYNC_CHILD_READY = 0x45, /* The child or grandchild is ready to return. */
4140

4241
/* XXX: This doesn't help with segfaults and other such issues. */
43-
SYNC_ERR = 0xFF, /* Fatal error, no turning back. The error code follows. */
42+
SYNC_ERR = 0xFF, /* Fatal error, no turning back. The error code follows. */
4443
};
4544

4645
/* longjmp() arguments. */
@@ -73,7 +72,7 @@ struct nlconfig_t {
7372
char *oom_score_adj;
7473
size_t oom_score_adj_len;
7574

76-
/* User namespace settings.*/
75+
/* User namespace settings. */
7776
char *uidmap;
7877
size_t uidmap_len;
7978
char *gidmap;
@@ -82,7 +81,7 @@ struct nlconfig_t {
8281
size_t namespaces_len;
8382
uint8_t is_setgroup;
8483

85-
/* Rootless container settings.*/
84+
/* Rootless container settings. */
8685
uint8_t is_rootless;
8786
char *uidmappath;
8887
size_t uidmappath_len;
@@ -167,7 +166,7 @@ static int write_file(char *data, size_t data_len, char *pathfmt, ...)
167166
goto out;
168167
}
169168

170-
out:
169+
out:
171170
close(fd);
172171
return ret;
173172
}
@@ -184,16 +183,16 @@ static void update_setgroups(int pid, enum policy_t setgroup)
184183
char *policy;
185184

186185
switch (setgroup) {
187-
case SETGROUPS_ALLOW:
188-
policy = "allow";
189-
break;
190-
case SETGROUPS_DENY:
191-
policy = "deny";
192-
break;
193-
case SETGROUPS_DEFAULT:
194-
default:
195-
/* Nothing to do. */
196-
return;
186+
case SETGROUPS_ALLOW:
187+
policy = "allow";
188+
break;
189+
case SETGROUPS_DENY:
190+
policy = "deny";
191+
break;
192+
case SETGROUPS_DEFAULT:
193+
default:
194+
/* Nothing to do. */
195+
return;
197196
}
198197

199198
if (write_file(policy, strlen(policy), "/proc/%d/setgroups", pid) < 0) {
@@ -226,14 +225,14 @@ static int try_mapping_tool(const char *app, int pid, char *map, size_t map_len)
226225
if (!child) {
227226
#define MAX_ARGV 20
228227
char *argv[MAX_ARGV];
229-
char *envp[] = {NULL};
228+
char *envp[] = { NULL };
230229
char pid_fmt[16];
231230
int argc = 0;
232231
char *next;
233232

234233
snprintf(pid_fmt, 16, "%d", pid);
235234

236-
argv[argc++] = (char *) app;
235+
argv[argc++] = (char *)app;
237236
argv[argc++] = pid_fmt;
238237
/*
239238
* Convert the map string into a list of argument that
@@ -319,7 +318,7 @@ static int clone_parent(jmp_buf *env, int jmpval) __attribute__ ((noinline));
319318
static int clone_parent(jmp_buf *env, int jmpval)
320319
{
321320
struct clone_t ca = {
322-
.env = env,
321+
.env = env,
323322
.jmpval = jmpval,
324323
};
325324

@@ -533,7 +532,7 @@ void nsexec(void)
533532
int pipenum;
534533
jmp_buf env;
535534
int sync_child_pipe[2], sync_grandchild_pipe[2];
536-
struct nlconfig_t config = {0};
535+
struct nlconfig_t config = { 0 };
537536

538537
/*
539538
* If we don't have an init pipe, just return to the go routine.
@@ -630,21 +629,21 @@ void nsexec(void)
630629
*/
631630

632631
switch (setjmp(env)) {
633-
/*
634-
* Stage 0: We're in the parent. Our job is just to create a new child
635-
* (stage 1: JUMP_CHILD) process and write its uid_map and
636-
* gid_map. That process will go on to create a new process, then
637-
* it will send us its PID which we will send to the bootstrap
638-
* process.
639-
*/
640-
case JUMP_PARENT: {
632+
/*
633+
* Stage 0: We're in the parent. Our job is just to create a new child
634+
* (stage 1: JUMP_CHILD) process and write its uid_map and
635+
* gid_map. That process will go on to create a new process, then
636+
* it will send us its PID which we will send to the bootstrap
637+
* process.
638+
*/
639+
case JUMP_PARENT:{
641640
int len;
642641
pid_t child, first_child = -1;
643642
char buf[JSON_MAX];
644643
bool ready = false;
645644

646645
/* For debugging. */
647-
prctl(PR_SET_NAME, (unsigned long) "runc:[0:PARENT]", 0, 0, 0);
646+
prctl(PR_SET_NAME, (unsigned long)"runc:[0:PARENT]", 0, 0, 0);
648647

649648
/* Start the process of getting a container. */
650649
child = clone_parent(&env, JUMP_CHILD);
@@ -702,7 +701,7 @@ void nsexec(void)
702701
bail("failed to sync with child: write(SYNC_USERMAP_ACK)");
703702
}
704703
break;
705-
case SYNC_RECVPID_PLS: {
704+
case SYNC_RECVPID_PLS:{
706705
first_child = child;
707706

708707
/* Get the init_func pid. */
@@ -781,16 +780,16 @@ void nsexec(void)
781780
exit(0);
782781
}
783782

784-
/*
785-
* Stage 1: We're in the first child process. Our job is to join any
786-
* provided namespaces in the netlink payload and unshare all
787-
* of the requested namespaces. If we've been asked to
788-
* CLONE_NEWUSER, we will ask our parent (stage 0) to set up
789-
* our user mappings for us. Then, we create a new child
790-
* (stage 2: JUMP_INIT) for PID namespace. We then send the
791-
* child's PID to our parent (stage 0).
792-
*/
793-
case JUMP_CHILD: {
783+
/*
784+
* Stage 1: We're in the first child process. Our job is to join any
785+
* provided namespaces in the netlink payload and unshare all
786+
* of the requested namespaces. If we've been asked to
787+
* CLONE_NEWUSER, we will ask our parent (stage 0) to set up
788+
* our user mappings for us. Then, we create a new child
789+
* (stage 2: JUMP_INIT) for PID namespace. We then send the
790+
* child's PID to our parent (stage 0).
791+
*/
792+
case JUMP_CHILD:{
794793
pid_t child;
795794
enum sync_t s;
796795

@@ -799,7 +798,7 @@ void nsexec(void)
799798
close(sync_child_pipe[1]);
800799

801800
/* For debugging. */
802-
prctl(PR_SET_NAME, (unsigned long) "runc:[1:CHILD]", 0, 0, 0);
801+
prctl(PR_SET_NAME, (unsigned long)"runc:[1:CHILD]", 0, 0, 0);
803802

804803
/*
805804
* We need to setns first. We cannot do this earlier (in stage 0)
@@ -901,13 +900,13 @@ void nsexec(void)
901900
exit(0);
902901
}
903902

904-
/*
905-
* Stage 2: We're the final child process, and the only process that will
906-
* actually return to the Go runtime. Our job is to just do the
907-
* final cleanup steps and then return to the Go runtime to allow
908-
* init_linux.go to run.
909-
*/
910-
case JUMP_INIT: {
903+
/*
904+
* Stage 2: We're the final child process, and the only process that will
905+
* actually return to the Go runtime. Our job is to just do the
906+
* final cleanup steps and then return to the Go runtime to allow
907+
* init_linux.go to run.
908+
*/
909+
case JUMP_INIT:{
911910
/*
912911
* We're inside the child now, having jumped from the
913912
* start_child() code after forking in the parent.
@@ -921,7 +920,7 @@ void nsexec(void)
921920
close(sync_child_pipe[1]);
922921

923922
/* For debugging. */
924-
prctl(PR_SET_NAME, (unsigned long) "runc:[2:INIT]", 0, 0, 0);
923+
prctl(PR_SET_NAME, (unsigned long)"runc:[2:INIT]", 0, 0, 0);
925924

926925
if (read(syncfd, &s, sizeof(s)) != sizeof(s))
927926
bail("failed to sync with parent: read(SYNC_GRANDCHILD)");

script/validate-c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
3+
source "$(dirname "$BASH_SOURCE")/.validate"
4+
5+
IFS=$'\n'
6+
files=($(validate_diff --diff-filter=ACMR --name-only -- '*.c' | grep -v '^vendor/' || true))
7+
unset IFS
8+
9+
# indent(1): "You must use the ‘-T’ option to tell indent the name of all the typenames in your program that are defined by typedef."
10+
INDENT="indent -linux -l120 -T size_t -T jmp_buf"
11+
if [ -z "$(indent --version 2>&1 | grep GNU)" ]; then
12+
echo "Skipping C indentation checks, as GNU indent is not installed."
13+
exit 0
14+
fi
15+
16+
badFiles=()
17+
for f in "${files[@]}"; do
18+
orig=$(mktemp)
19+
formatted=$(mktemp)
20+
# we use "git show" here to validate that what's committed is formatted
21+
git show "$VALIDATE_HEAD:$f" > ${orig}
22+
${INDENT} ${orig} -o ${formatted}
23+
if [ "$(diff -u ${orig} ${formatted})" ]; then
24+
badFiles+=("$f")
25+
fi
26+
rm -f ${orig} ${formatted}
27+
done
28+
29+
if [ ${#badFiles[@]} -eq 0 ]; then
30+
echo 'Congratulations! All C source files are properly formatted.'
31+
else
32+
{
33+
echo "These files are not properly formatted:"
34+
for f in "${badFiles[@]}"; do
35+
echo " - $f"
36+
done
37+
echo
38+
echo "Please reformat the above files using \"${INDENT}\" and commit the result."
39+
echo
40+
} >&2
41+
false
42+
fi

0 commit comments

Comments
 (0)