Fixup style, makefile

This commit is contained in:
Thomas Orozco 2015-02-22 21:51:17 -08:00
parent 1bba98e471
commit 28feb78176
4 changed files with 23 additions and 24 deletions

View File

@ -6,7 +6,7 @@ BIN = tini
all: $(BIN)
$(BIN): $(OBJ)
$(CC) $(LDFLAGS) -o $@ $(OBJ) $(LDLIBS)
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $<
$(OBJ):

1
README
View File

@ -1 +0,0 @@
Simple but correct init for Docker containers

View File

@ -1,11 +1,10 @@
VERSION = 0.1.0
# paths
PREFIX = /usr/local
MANPREFIX = $(PREFIX)/share/man
PREFIX=/usr/local
CC = cc
LD = $(CC)
CPPFLAGS =
CFLAGS = -Wextra -Wall -Os
LDFLAGS = -s -static
CPPFLAGS=-D_FORTIFY_SOURCE=2
CFLAGS=-std=gnu99 -Wextra -Wall -pedantic -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security
LDFLAGS=-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-s

35
tini.c
View File

@ -35,6 +35,8 @@ void print_usage_and_exit(char *name, FILE *file, int status) {
}
int main(int argc, char *argv[]) {
char* name = argv[0];
siginfo_t sig;
pid_t child_pid;
@ -46,8 +48,6 @@ int main(int argc, char *argv[]) {
ts.tv_sec = 1;
ts.tv_nsec = 0;
char* name = argv[0];
/* Start with argument processing */
int c;
while ((c = getopt (argc, argv, "h")) != -1) {
@ -59,7 +59,7 @@ int main(int argc, char *argv[]) {
print_usage_and_exit(name, stderr, 1);
break;
default:
// Should never happen
/* Should never happen */
abort();
}
}
@ -72,7 +72,7 @@ int main(int argc, char *argv[]) {
child_args[i] = NULL;
if (i == 0) {
// User forgot to provide args!
/* User forgot to provide args! */
print_usage_and_exit(name, stdout, 1);
}
@ -109,13 +109,14 @@ int main(int argc, char *argv[]) {
/* There is a signal to handle here */
switch (sig.si_signo) {
case SIGCHLD:
// Special-cased, as we don't forward SIGCHLD. Instead, we'll
// fallthrough to reaping processes.
/* Special-cased, as we don't forward SIGCHLD. Instead, we'll
* fallthrough to reaping processes.
*/
printf("[INFO ] Received SIGCHLD\n");
break;
default:
printf("[INFO ] Passing signal: %s\n", strsignal(sig.si_signo));
// Forward anything else
/* Forward anything else */
kill(child_pid, sig.si_signo);
break;
}
@ -126,24 +127,25 @@ int main(int argc, char *argv[]) {
current_pid = waitpid(-1, &current_status, WNOHANG);
switch (current_pid) {
case -1:
// An error occured. Print it and exit.
/* An error occured. Print it and exit. */
perror("waitpids returned -1");
return 1;
case 0:
// No child to reap. We'll break out of the loop here.
/* No child to reap. We'll break out of the loop here. */
break;
default:
// A child was reaped. Check whether it's the main one,
// and go for another iteration otherwise.
/* A child was reaped. Check whether it's the main one,
* and go for another iteration otherwise.
*/
if (current_pid == child_pid) {
printf("[INFO ] Main child has exited\n");
if (WIFEXITED(current_status)) {
// Our process exited normally.
/* Our process exited normally. */
printf("[DEBUG] Main child exited normally (check exit status)\n");
return WEXITSTATUS(current_status);
} else if (WIFSIGNALED(current_status)) {
// Our process was terminated. Emulate what sh / bash
// would do, which is to return 128 + signal number.
/* Our process was terminated. Emulate what sh / bash
* would do, which is to return 128 + signal number.
*/
printf("[DEBUG] Main child exited with signal\n");
return 128 + WTERMSIG(current_status);
} else {
@ -154,8 +156,7 @@ int main(int argc, char *argv[]) {
continue;
}
// If we make it here, that's because we did not continue in
// the switch case.
/* If we make it here, that's because we did not continue in the switch case. */
break;
}