Skip to content

Commit d7ac16a

Browse files
committed
Work towards improving signals and processes
1 parent de703b1 commit d7ac16a

Some content is hidden

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

96 files changed

+1468
-421
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
Fast portable static native textmode executable containers.
44

55
For an introduction to this project, please read the <a
6-
href="https://justine.storage.googleapis.com/ape.html">αcτµαlly pδrταblε
7-
εxεcµταblε</a> blog post and <a
6+
href="https://justine.lol/ape.html">αcτµαlly pδrταblε εxεcµταblε</a>
7+
blog post and <a
88
href="https://justine.lol/cosmopolitan/index.html">cosmopolitan libc</a>
99
website. API documentation is available <a
1010
href="https://justine.lol/cosmopolitan/documentation.html">here</a>.

ape/ape.lds

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ SECTIONS {
425425
*(.xlm)
426426
. = ALIGN(0x1000);
427427

428+
. = ALIGN(0x10000); /* for brk()/sbrk() allocation */
428429
HIDDEN(_end = .);
429430
PROVIDE_HIDDEN(end = .);
430431
} AT>SmallCode :Ram

build/bootstrap/zipobj.com

44 KB
Binary file not shown.

build/definitions.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ DEFAULT_LDFLAGS = \
185185
-z max-page-size=0x1000 \
186186
-Ttext-segment=$(IMAGE_BASE_VIRTUAL)
187187

188+
ZIPOBJ_FLAGS = \
189+
-b$(IMAGE_BASE_VIRTUAL)
190+
188191
ASONLYFLAGS = \
189192
-g \
190193
--debug-prefix-map="$(PWD)"=

build/rules.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ o/%.pkg:; @build/package $(OUTPUT_OPTION) $(addprefix -d,$(filter %.pkg,$^)) $(f
3434
o/%.h.ok: %.h; @ACTION=CHECK.h build/compile $(COMPILE.c) -x c -g0 -o $@ $<
3535
o/%.h.okk: %.h; @ACTION=CHECK.h build/compile $(COMPILE.cxx) -x c++ -g0 -o $@ $<
3636
o/%.greg.o: %.greg.c; @ACTION=OBJECTIFY.greg build/compile $(OBJECTIFY.greg.c) $(OUTPUT_OPTION) $<
37-
o/%.zip.o: o/%; @build/zipobj $(OUTPUT_OPTION) $<
37+
o/%.zip.o: o/%; @build/zipobj $(ZIPOBJ_FLAGS) $(OUTPUT_OPTION) $<
3838

3939
o/$(MODE)/%.a:; @$(ARCHIVE) $@ $^
4040
o/$(MODE)/%: o/$(MODE)/%.dbg; @ACTION=OBJCOPY TARGET=$@ build/do $(OBJCOPY) -SO binary $< $@
@@ -75,7 +75,7 @@ o/$(MODE)/%.ncabi.o: %.ncabi.c; @ACTION=OBJECTIFY.nc build/compile $(OBJECTIFY.n
7575
o/$(MODE)/%.real.o: %.c; @ACTION=OBJECTIFY.real build/compile $(OBJECTIFY.real.c) $(OUTPUT_OPTION) $<
7676
o/$(MODE)/%.runs: o/$(MODE)/%; @ACTION=CHECK.runs TARGET=$< build/runcom $< $(TESTARGS) && touch $@
7777
o/$(MODE)/%.pkg:; @build/package $(OUTPUT_OPTION) $(addprefix -d,$(filter %.pkg,$^)) $(filter %.o,$^)
78-
o/$(MODE)/%.zip.o: %; @build/zipobj $(OUTPUT_OPTION) $<
78+
o/$(MODE)/%.zip.o: %; @build/zipobj $(ZIPOBJ_FLAGS) $(OUTPUT_OPTION) $<
7979

8080
o/$(MODE)/%-gcc.asm: %.c; @ACTION=OBJECTIFY.c build/compile $(OBJECTIFY.c) -S -g0 $(OUTPUT_OPTION) $<
8181
o/$(MODE)/%-clang.asm: CC = $(CLANG)

examples/ctrlc.c

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
╚─────────────────────────────────────────────────────────────────*/
99
#endif
1010
#include "libc/calls/calls.h"
11+
#include "libc/errno.h"
1112
#include "libc/log/check.h"
1213
#include "libc/log/color.internal.h"
1314
#include "libc/log/log.h"
1415
#include "libc/mem/mem.h"
16+
#include "libc/nt/thread.h"
1517
#include "libc/runtime/gc.h"
1618
#include "libc/runtime/runtime.h"
1719
#include "libc/stdio/stdio.h"
@@ -20,48 +22,41 @@
2022
#include "libc/sysv/consts/fileno.h"
2123
#include "libc/sysv/consts/sig.h"
2224

23-
#define kTutorialMessage "This echos stdio until Ctrl+C is pressed.\n"
24-
#define kVictoryMessage "\rGot Ctrl+C and longjmp() ran dtors (>'.')>\n"
25-
26-
jmp_buf jb;
25+
volatile bool gotctrlc;
2726

2827
void GotCtrlC(int sig) {
29-
gclongjmp(jb, 1);
30-
unreachable;
31-
}
32-
33-
size_t HowManyBytesOfHeapMemoryAreAllocated(void) {
34-
return mallinfo().uordblks;
35-
}
36-
37-
void ReadAndPrintLinesLoop(void) {
38-
ssize_t got;
39-
unsigned char *buf;
40-
buf = gc(malloc(BUFSIZ));
41-
CHECK_NE(0, HowManyBytesOfHeapMemoryAreAllocated());
42-
for (;;) {
43-
CHECK_NE(-1, (got = read(STDIN_FILENO, buf, BUFSIZ)));
44-
CHECK_EQ(got, write(STDOUT_FILENO, buf, got));
45-
}
28+
gotctrlc = true;
4629
}
4730

4831
int main(int argc, char *argv[]) {
49-
int rc;
50-
rc = 0;
51-
showcrashreports();
52-
if (cancolor()) {
53-
CHECK_EQ(0 /* cosmo runtime doesn't link malloc */,
54-
HowManyBytesOfHeapMemoryAreAllocated());
55-
puts("This echos stdio until Ctrl+C is pressed.");
56-
if (!(rc = setjmp(jb))) {
57-
CHECK_NE(SIG_ERR, signal(SIGINT, GotCtrlC));
58-
ReadAndPrintLinesLoop();
59-
unreachable;
32+
ssize_t rc;
33+
size_t got, wrote;
34+
unsigned char buf[512];
35+
fprintf(stderr, "This echos stdio until Ctrl+C is pressed.\n");
36+
CHECK_NE(
37+
-1, sigaction(SIGINT, &(struct sigaction){.sa_handler = GotCtrlC}, NULL));
38+
for (;;) {
39+
rc = read(0, buf, BUFSIZ);
40+
if (rc != -1) {
41+
got = rc;
6042
} else {
61-
--rc;
43+
CHECK_EQ(EINTR, errno);
44+
break;
6245
}
63-
CHECK_EQ(0, HowManyBytesOfHeapMemoryAreAllocated());
64-
puts("\rGot Ctrl+C and longjmp() ran dtors (>'.')>");
46+
if (!got) break;
47+
rc = write(1, buf, got);
48+
if (rc != -1) {
49+
wrote = rc;
50+
} else {
51+
CHECK_EQ(EINTR, errno);
52+
break;
53+
}
54+
CHECK_EQ(got, wrote);
55+
}
56+
if (gotctrlc) {
57+
fprintf(stderr, "Got Ctrl+C\n");
58+
} else {
59+
fprintf(stderr, "Got EOF\n");
6560
}
66-
return rc;
61+
return 0;
6762
}

examples/examples.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ EXAMPLES_DIRECTDEPS = \
5151
LIBC_LOG_ASAN \
5252
LIBC_MEM \
5353
LIBC_NEXGEN32E \
54+
LIBC_NT_KERNEL32 \
5455
LIBC_NT_NTDLL \
5556
LIBC_NT_USER32 \
5657
LIBC_NT_WS2_32 \

examples/sleep.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#if 0
2+
/*─────────────────────────────────────────────────────────────────╗
3+
│ To the extent possible under law, Justine Tunney has waived │
4+
│ all copyright and related or neighboring rights to this file, │
5+
│ as it is written in the following disclaimers: │
6+
│ • http://unlicense.org/ │
7+
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
8+
╚─────────────────────────────────────────────────────────────────*/
9+
#endif
10+
#include "libc/calls/calls.h"
11+
#include "libc/runtime/runtime.h"
12+
#include "libc/stdio/stdio.h"
13+
#include "libc/str/str.h"
14+
#include "libc/time/time.h"
15+
#include "libc/x/x.h"
16+
17+
/**
18+
* 16kb sleep executable that runs on all operating systems.
19+
* https://justine.lol/cosmopolitan/demos/sleep.c
20+
* https://justine.lol/cosmopolitan/demos/sleep.com
21+
* https://justine.lol/cosmopolitan/index.html
22+
*/
23+
24+
#define WRITE(s) write(2, s, strlen(s))
25+
26+
int main(int argc, char *argv[]) {
27+
char *p;
28+
long double x, y;
29+
if (argc != 2) {
30+
WRITE("Usage: ");
31+
WRITE(argv[0]);
32+
WRITE(" SECONDS\n");
33+
exit(1);
34+
}
35+
x = 0;
36+
for (p = argv[1]; isdigit(*p); ++p) {
37+
x *= 10;
38+
x += *p - '0';
39+
}
40+
if (*p == '.') {
41+
y = 1;
42+
for (++p; isdigit(*p); ++p) {
43+
x += (*p - '0') * (y /= 10);
44+
}
45+
}
46+
switch (*p) {
47+
case 'm':
48+
x *= 60;
49+
break;
50+
case 'h':
51+
x *= 60 * 60;
52+
break;
53+
case 'd':
54+
x *= 60 * 60 * 24;
55+
break;
56+
default:
57+
break;
58+
}
59+
dsleep(x);
60+
return 0;
61+
}

examples/unbourne.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -617,10 +617,10 @@
617617

618618
#define setlocate(l, s) 0
619619
#define equal(s1, s2) (strcmp(s1, s2) == 0)
620-
#define getenv(p) bltinlookup((p), 0)
621-
#define isodigit(c) ((c) >= '0' && (c) <= '7')
622-
#define octtobin(c) ((c) - '0')
623-
#define scopy(s1, s2) ((void)strcpy(s2, s1))
620+
/* #define getenv(p) bltinlookup((p), 0) */
621+
#define isodigit(c) ((c) >= '0' && (c) <= '7')
622+
#define octtobin(c) ((c) - '0')
623+
#define scopy(s1, s2) ((void)strcpy(s2, s1))
624624

625625
#define TRACE(param)
626626
/* #define TRACE(param) \ */

libc/calls/atfork.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/bits/bits.h"
1920
#include "libc/macros.h"
2021
#include "libc/runtime/runtime.h"
2122
#include "libc/sysv/errfuns.h"
2223

2324
static struct AtFork {
24-
size_t i;
25+
volatile size_t i;
2526
struct AtForkCallback {
2627
void (*fn)(void *);
2728
void *arg;
@@ -33,17 +34,25 @@ static struct AtFork {
3334
*
3435
* @return 0 on success, or -1 w/ errno
3536
* @note vfork() won't invoke callbacks
37+
* @asyncsignalsafe
3638
*/
3739
int atfork(void *fn, void *arg) {
38-
if (g_atfork.i == ARRAYLEN(g_atfork.p)) return enomem();
39-
g_atfork.p[g_atfork.i++] = (struct AtForkCallback){.fn = fn, .arg = arg};
40-
return 0;
40+
size_t i;
41+
for (;;) {
42+
i = g_atfork.i;
43+
if (i == ARRAYLEN(g_atfork.p)) return enomem();
44+
if (cmpxchg(&g_atfork.i, i, i + 1)) {
45+
g_atfork.p[i] = (struct AtForkCallback){.fn = fn, .arg = arg};
46+
return 0;
47+
}
48+
}
4149
}
4250

4351
/**
4452
* Triggers callbacks registered by atfork().
4553
*
4654
* @note only fork() should call this
55+
* @asyncsignalsafe
4756
*/
4857
void __onfork(void) {
4958
size_t i;

0 commit comments

Comments
 (0)