Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
socket_unix_listen.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
21
23 /* Pops backlog, path; returns listen fd (>0) or 0 */
24 Value backlogv = pop_value(vm);
25 Value pathv = pop_value(vm);
26 int fd = 0;
27#ifdef __unix__
28 if (pathv.type != VAL_STRING || backlogv.type != VAL_INT) {
29 fprintf(stderr, "Runtime type error: unix_listen expects (string path, int backlog)\n");
30 free_value(backlogv);
32 push_value(vm, make_int(0));
33 break;
34 }
36 int backlog = (int)backlogv.i;
37 if (path) {
38 int s = socket(AF_UNIX, SOCK_STREAM, 0);
39 if (s >= 0) {
40 struct sockaddr_un addr;
41 memset(&addr, 0, sizeof(addr));
42 addr.sun_family = AF_UNIX;
43 size_t maxlen = sizeof(addr.sun_path) - 1;
44 strncpy(addr.sun_path, path, maxlen);
45 addr.sun_path[maxlen] = '\0';
46 unlink(addr.sun_path); /* best effort */
47 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
48 if (listen(s, backlog > 0 ? backlog : 1) == 0) {
49 fd = s;
50 } else {
51 close(s);
52 }
53 } else {
54 close(s);
55 }
56 }
57 free(path);
58 }
59#endif
60 free_value(backlogv);
62 push_value(vm, make_int(fd > 0 ? fd : 0));
63 break;
64}
@ OP_SOCK_UNIX_LISTEN
Definition bytecode.h:226
free(vals)
uint32_t s
Definition rol.c:31
Value pathv
Definition serial_open.c:91
int fd
Definition serial_open.c:92
free_value(backlogv)
push_value(vm, make_int(fd > 0 ? fd :0))
Tagged union representing a Fun value.
Definition value.h:68
int64_t i
Definition value.h:71
ValueType type
Definition value.h:69
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
Definition value.c:641
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
@ VAL_STRING
Definition value.h:53
@ VAL_INT
Definition value.h:51
#define fprintf
Definition vm.c:200
Value path
Definition write_file.c:33