Skip to content

Commit 98f95e8

Browse files
authored
Consolidate exit code (#187)
Exiting with EXIT_SUCCESS and EXIT_FAILURE are defined in ANSI C11, aligning the README description to indicate that blink is written in ANSI C11. Additionally, to ensure consistency in exit calls, EXIT_FAILURE_EXEC_FAILED, EXIT_FAILURE_WITH_SIGNAL, EXIT_FAILURE_ERRFD_INIT and EXIT_FAILURE_MMAP_PANIC are introduced, replacing numeric exit codes with semantic macros.
1 parent 4980d2c commit 98f95e8

File tree

10 files changed

+49
-42
lines changed

10 files changed

+49
-42
lines changed

blink/bios.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ static void OnApmService(void) {
868868
SetCarry(false);
869869
} else if (Get16(m->ax) == 0x5307 && m->bl == 1 && m->cl == 3) {
870870
LOGF("APM SHUTDOWN");
871-
exit(0);
871+
exit(EXIT_SUCCESS);
872872
} else {
873873
SetCarry(true);
874874
}

blink/biosrom.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,22 @@ void LoadBios(struct Machine *m, const char *biosprog) {
145145
FormatInt64(tmp, errno);
146146
WriteErrorString(tmp);
147147
WriteErrorString(")\n");
148-
exit(127);
148+
exit(EXIT_FAILURE_EXEC_FAILED);
149149
} else if ((size = st.st_size) < kBiosMinSize) {
150150
WriteErrorString(biosprog);
151151
WriteErrorString(": failed to load alternate BIOS (file too small)\n");
152-
exit(127);
152+
exit(EXIT_FAILURE_EXEC_FAILED);
153153
} else if (size > kBiosMaxSize) {
154154
WriteErrorString(biosprog);
155155
WriteErrorString(": failed to load alternate BIOS (file too large)\n");
156-
exit(127);
156+
exit(EXIT_FAILURE_EXEC_FAILED);
157157
} else if (VfsRead(fd, m->system->real + kBiosEnd - size, size) != size) {
158158
WriteErrorString(biosprog);
159159
WriteErrorString(": failed to load alternate BIOS (errno ");
160160
FormatInt64(tmp, errno);
161161
WriteErrorString(tmp);
162162
WriteErrorString(")\n");
163-
exit(127);
163+
exit(EXIT_FAILURE_EXEC_FAILED);
164164
}
165165
} else {
166166
// if no BIOS image file name is given, then load a default BIOS image

blink/blink.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ _Noreturn static void PrintUsage(int argc, char *argv[], int rc, int fd) {
260260

261261
_Noreturn static void PrintVersion(void) {
262262
Print(1, VERSION);
263-
exit(0);
263+
exit(EXIT_SUCCESS);
264264
}
265265

266266
static void GetOpts(int argc, char *argv[]) {
@@ -380,13 +380,13 @@ int main(int argc, char *argv[]) {
380380
#ifndef DISABLE_OVERLAYS
381381
if (SetOverlays(FLAG_overlays, true)) {
382382
WriteErrorString("bad blink overlays spec; see log for details\n");
383-
exit(1);
383+
exit(EXIT_FAILURE);
384384
}
385385
#endif
386386
#ifndef DISABLE_VFS
387387
if (VfsInit(FLAG_prefix)) {
388388
WriteErrorString("error: vfs initialization failed\n");
389-
exit(1);
389+
exit(EXIT_FAILURE);
390390
}
391391
#endif
392392
HandleSigs();
@@ -396,7 +396,7 @@ int main(int argc, char *argv[]) {
396396
WriteErrorString(": command not found: ");
397397
WriteErrorString(argv[optind_]);
398398
WriteErrorString("\n");
399-
exit(127);
399+
exit(EXIT_FAILURE_EXEC_FAILED);
400400
}
401401
argv[optind_] = g_pathbuf;
402402
return Exec(g_pathbuf, g_pathbuf, argv + optind_ + FLAG_zero, environ);

blink/blinkenlights.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,7 +2092,7 @@ static void RewindHistory(int delta) {
20922092
static ssize_t HandleEpipe(ssize_t rc) {
20932093
if (rc == -1 && errno == EPIPE) {
20942094
LOGF("got EPIPE, shutting down");
2095-
exit(128 + EPIPE);
2095+
exit(EXIT_FAILURE_WITH_SIGNAL(EPIPE));
20962096
}
20972097
return rc;
20982098
}
@@ -2609,7 +2609,7 @@ static void LaunchDebuggerReactively(void) {
26092609
action |= MODAL;
26102610
} else {
26112611
fprintf(stderr, "ERROR: %s\n", systemfailure);
2612-
exit(1);
2612+
exit(EXIT_FAILURE);
26132613
}
26142614
}
26152615
}
@@ -3093,7 +3093,7 @@ static void ReadKeyboard(void) {
30933093
return;
30943094
}
30953095
fprintf(stderr, "ReadKeyboard failed: %s\n", DescribeHostErrno(errno));
3096-
exit(1);
3096+
exit(EXIT_FAILURE);
30973097
}
30983098
HandleKeyboard(buf);
30993099
}
@@ -3408,7 +3408,7 @@ static void Tui(void) {
34083408
ScrollMemoryViews();
34093409
if (m->signals & ~m->sigmask) {
34103410
if ((sig = ConsumeSignal(m, 0, 0))) {
3411-
exit(128 + sig);
3411+
exit(EXIT_FAILURE_WITH_SIGNAL(sig));
34123412
}
34133413
}
34143414
if (!(action & CONTINUE) || interactive) {
@@ -3453,7 +3453,7 @@ static void Tui(void) {
34533453

34543454
_Noreturn static void PrintVersion(void) {
34553455
fputs(VERSION, stdout);
3456-
exit(0);
3456+
exit(EXIT_SUCCESS);
34573457
}
34583458

34593459
static void GetOpts(int argc, char *argv[]) {
@@ -3488,7 +3488,7 @@ static void GetOpts(int argc, char *argv[]) {
34883488
"linearization not possible on this system"
34893489
" (word size is %d bits and page size is %ld)\n",
34903490
bsr(UINTPTR_MAX) + 1, sysconf(_SC_PAGESIZE));
3491-
exit(1);
3491+
exit(EXIT_FAILURE);
34923492
}
34933493
break;
34943494
case 'R':
@@ -3566,7 +3566,7 @@ int VirtualMachine(int argc, char *argv[]) {
35663566
codepath = pathbuf;
35673567
} else {
35683568
fprintf(stderr, "%s: command not found: %s\n", argv[0], argv[optind_]);
3569-
exit(127);
3569+
exit(EXIT_FAILURE_EXEC_FAILED);
35703570
}
35713571
optind_++;
35723572
do {
@@ -3601,7 +3601,7 @@ int VirtualMachine(int argc, char *argv[]) {
36013601
}
36023602
if (tty == -1) {
36033603
WriteErrorString("failed to open /dev/tty\n");
3604-
exit(1);
3604+
exit(EXIT_FAILURE);
36053605
}
36063606
ttyin = tty;
36073607
ttyout = tty;
@@ -3647,7 +3647,7 @@ void TerminateSignal(struct Machine *m, int sig, int code) {
36473647
LOGF("terminating due to signal %s code=%d", DescribeSignal(sig), code);
36483648
WriteErrorString("\r\033[K\033[J"
36493649
"terminating due to signal; see log\n");
3650-
exit(128 + sig);
3650+
exit(EXIT_FAILURE_WITH_SIGNAL(sig));
36513651
}
36523652
}
36533653

@@ -3700,13 +3700,13 @@ int main(int argc, char *argv[]) {
37003700
#ifndef DISABLE_OVERLAYS
37013701
if (SetOverlays(FLAG_overlays, true)) {
37023702
WriteErrorString("bad blink overlays spec; see log for details\n");
3703-
exit(1);
3703+
exit(EXIT_FAILURE);
37043704
}
37053705
#endif
37063706
#ifndef DISABLE_VFS
37073707
if (VfsInit(FLAG_prefix)) {
37083708
WriteErrorString("error: vfs initialization failed\n");
3709-
exit(1);
3709+
exit(EXIT_FAILURE);
37103710
}
37113711
#endif
37123712
#ifdef HAVE_JIT

blink/demangle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ static void SpawnCxxFilt(void) {
135135
if (pipefds[1][0] > 1) unassert(!close(pipefds[1][0]));
136136
if (pipefds[1][1] > 1) unassert(!close(pipefds[1][1]));
137137
execv(executable, (char *const[]){(char *)cxxfilt, 0});
138-
_exit(127);
138+
_exit(EXIT_FAILURE_EXEC_FAILED);
139139
}
140140
unassert(!close(pipefds[0][1]));
141141
unassert(!close(pipefds[1][0]));

blink/errfd.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "blink/thread.h"
2828
#include "blink/tunables.h"
2929

30+
#define EXIT_FAILURE_ERRFD_INIT 200
31+
3032
static int g_errfd;
3133

3234
int WriteErrorString(const char *buf) {
@@ -49,5 +51,5 @@ int WriteError(int fd, const char *buf, int len) {
4951
void WriteErrorInit(void) {
5052
if (g_errfd) return;
5153
g_errfd = fcntl(2, F_DUPFD_CLOEXEC, kMinBlinkFd);
52-
if (g_errfd == -1) exit(200);
54+
if (g_errfd == -1) exit(EXIT_FAILURE_ERRFD_INIT);
5355
}

blink/loader.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,28 +128,28 @@ static i64 LoadElfLoadSegment(struct Machine *m, const char *path, void *image,
128128
}
129129
if (offset > imagesize) {
130130
ERRF("bad phdr offset");
131-
exit(127);
131+
exit(EXIT_FAILURE_EXEC_FAILED);
132132
}
133133
if (filesz > imagesize) {
134134
ERRF("bad phdr filesz");
135-
exit(127);
135+
exit(EXIT_FAILURE_EXEC_FAILED);
136136
}
137137
if (filesz && offset + filesz > imagesize) {
138138
ERRF("corrupt elf program header");
139-
exit(127);
139+
exit(EXIT_FAILURE_EXEC_FAILED);
140140
}
141141
if (end < last_end) {
142142
ERRF("program headers aren't ordered, expected %" PRIx64 " >= %" PRIx64,
143143
end, last_end);
144-
exit(127);
144+
exit(EXIT_FAILURE_EXEC_FAILED);
145145
}
146146
if (skew != (offset & (pagesize - 1))) {
147147
WriteErrorString(
148148
"p_vaddr p_offset skew unequal w.r.t. page size; try either "
149149
"(1) rebuilding your program using the linker flags: -static "
150150
"-Wl,-z,common-page-size=65536,-z,max-page-size=65536 or (2) "
151151
"using `blink -m` to disable the linear memory optimization\n");
152-
exit(127);
152+
exit(EXIT_FAILURE_EXEC_FAILED);
153153
}
154154

155155
// on systems with a page size greater than the elf executable (e.g.
@@ -210,7 +210,7 @@ static i64 LoadElfLoadSegment(struct Machine *m, const char *path, void *image,
210210
start + bulk, offset, offset + bulk);
211211
if (ReserveVirtual(s, start, bulk, key, fd, offset, 0, 0) == -1) {
212212
ERRF("failed to map elf program header file data");
213-
exit(127);
213+
exit(EXIT_FAILURE_EXEC_FAILED);
214214
}
215215
if ((amt = bulk - filesz)) {
216216
ELF_LOGF("note: next copy is actually bzero() kludge");
@@ -225,7 +225,7 @@ static i64 LoadElfLoadSegment(struct Machine *m, const char *path, void *image,
225225
ELF_LOGF("alloc %" PRIx64 "-%" PRIx64, start, end);
226226
if (ReserveVirtual(s, start, end - start, key, -1, 0, 0, 0) == -1) {
227227
ERRF("failed to allocate program header bss");
228-
exit(127);
228+
exit(EXIT_FAILURE_EXEC_FAILED);
229229
}
230230
}
231231
}
@@ -378,7 +378,7 @@ static i64 ChooseAslr(const Elf64_Ehdr_ *ehdr, size_t size, i64 dflt,
378378
i64 aslr;
379379
if (GetElfMemorySize(ehdr, size, base) <= 0) {
380380
ERRF("couldn't determine boundaries of loaded executable");
381-
exit(127);
381+
exit(EXIT_FAILURE_EXEC_FAILED);
382382
}
383383
if (Read16(ehdr->type) == ET_DYN_ && !*base) {
384384
aslr = dflt;
@@ -390,7 +390,7 @@ static i64 ChooseAslr(const Elf64_Ehdr_ *ehdr, size_t size, i64 dflt,
390390
*base += aslr;
391391
if (!(*base & ~(FLAG_pagesize - 1))) {
392392
ERRF("won't load program to null base address");
393-
exit(127);
393+
exit(EXIT_FAILURE_EXEC_FAILED);
394394
}
395395
return aslr;
396396
}
@@ -427,7 +427,7 @@ static bool LoadElf(struct Machine *m, //
427427
elf->interpreter = (char *)ehdr + Read64(phdr->offset);
428428
if (elf->interpreter[Read64(phdr->filesz) - 1]) {
429429
ELF_LOGF("elf interpreter not nul terminated");
430-
exit(127);
430+
exit(EXIT_FAILURE_EXEC_FAILED);
431431
}
432432
break;
433433
default:
@@ -455,7 +455,7 @@ static bool LoadElf(struct Machine *m, //
455455
FormatInt64(ibuf, errno);
456456
WriteErrorString(ibuf);
457457
WriteErrorString(")\n");
458-
exit(127);
458+
exit(EXIT_FAILURE_EXEC_FAILED);
459459
}
460460
aslr = ChooseAslr(
461461
ehdri, st.st_size,
@@ -717,7 +717,7 @@ void LoadProgram(struct Machine *m, char *execfn, char *prog, char **args,
717717
FormatInt64(tmp, errno);
718718
WriteErrorString(tmp);
719719
WriteErrorString(")\n");
720-
exit(127);
720+
exit(EXIT_FAILURE_EXEC_FAILED);
721721
}
722722
status = CanEmulateData(m, &prog, &args, isfirst, (char *)map, mapsize);
723723
if (!status) {
@@ -727,7 +727,7 @@ error: unsupported executable; we need:\n\
727727
- flat executables (.bin files)\n\
728728
- actually portable executables (MZqFpD/jartsr)\n\
729729
- scripts with #!shebang meeting above criteria\n");
730-
exit(127);
730+
exit(EXIT_FAILURE_EXEC_FAILED);
731731
} else if (status == 1) {
732732
break; // file is a real executable
733733
} else if (status == 2) {
@@ -739,7 +739,7 @@ error: unsupported executable; we need:\n\
739739
isfirst = false;
740740
} else {
741741
LOGF("shell scripts can't interpret shell scripts");
742-
exit(127);
742+
exit(EXIT_FAILURE_EXEC_FAILED);
743743
}
744744
} else {
745745
__builtin_unreachable();
@@ -780,7 +780,7 @@ error: unsupported executable; we need:\n\
780780
// Cosmopolitan programs pretty much require at least 47-bit virtual
781781
// addresses; if the host lacks these, then emulate them w/ software
782782
if (FLAG_vabits < 47) FLAG_nolinear = true;
783-
if (GetElfHeader(tmp, prog, (const char *)map) == -1) exit(127);
783+
if (GetElfHeader(tmp, prog, (const char *)map) == -1) exit(EXIT_FAILURE_EXEC_FAILED);
784784
memcpy(map, tmp, 64);
785785
execstack = LoadElf(m, elf, (Elf64_Ehdr_ *)map, mapsize, fd);
786786
} else {
@@ -797,7 +797,7 @@ error: unsupported executable; we need:\n\
797797
Put64(m->sp, stack + kStackSize);
798798
} else {
799799
LOGF("failed to reserve stack memory");
800-
exit(127);
800+
exit(EXIT_FAILURE_EXEC_FAILED);
801801
}
802802
m->system->loaded = true; // in case rwx stack is smc write-protected :'(
803803
LoadArgv(m, execfn, prog, args, vars, elf->rng);

blink/machine.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#include "blink/tunables.h"
2424
#include "blink/x86.h"
2525

26+
#define EXIT_FAILURE_EXEC_FAILED 127
27+
#define EXIT_FAILURE_WITH_SIGNAL(sig) 128 + sig
28+
2629
#define kArgRde 1
2730
#define kArgDisp 2
2831
#define kArgUimm0 3

blink/memorymalloc.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ void KillOtherThreads(struct System *s) {
282282
struct timespec deadline;
283283
if (atomic_exchange(&s->killer, true)) {
284284
FreeMachine(g_machine);
285-
pthread_exit(0);
285+
pthread_exit(EXIT_SUCCESS);
286286
}
287287
StartOver:
288288
unassert(s == g_machine->system);
@@ -740,6 +740,8 @@ static void RemoveVirtual(struct System *s, i64 virt, i64 size,
740740
}
741741
}
742742

743+
#define EXIT_FAILURE_MMAP_PANIC 250
744+
743745
_Noreturn static void PanicDueToMmap(void) {
744746
#ifndef NDEBUG
745747
WriteErrorString(
@@ -748,7 +750,7 @@ _Noreturn static void PanicDueToMmap(void) {
748750
WriteErrorString(
749751
"unrecoverable mmap() crisis: Blink was built with NDEBUG\n");
750752
#endif
751-
exit(250);
753+
exit(EXIT_FAILURE_MMAP_PANIC);
752754
}
753755

754756
static int FailDueToHostAlignment(i64 virt, long pagesize, const char *kind) {
@@ -937,7 +939,7 @@ i64 ReserveVirtual(struct System *s, i64 virt, i64 size, u64 flags, int fd,
937939
if (!(pt & PAGE_V)) {
938940
if ((pt = AllocatePageTable(s)) == -1) {
939941
WriteErrorString("mmap() crisis: ran out of page table memory\n");
940-
exit(250);
942+
exit(EXIT_FAILURE_MMAP_PANIC);
941943
}
942944
StorePte(mi, pt);
943945
}

blink/syscall.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ _Noreturn void SysExit(struct Machine *m, int rc) {
392392
} else {
393393
ClearChildTid(m);
394394
FreeMachine(m);
395-
pthread_exit(0);
395+
pthread_exit(EXIT_SUCCESS);
396396
}
397397
#else
398398
SysExitGroup(m, rc);

0 commit comments

Comments
 (0)