commit 053a362

uint  ·  2026-10-29 19:52:38 +0000 UTC
parent 68ae24e
Pipe: Add basic pipe structure, test programs
4 files changed,  +100, -0
+11, -0
 1@@ -0,0 +1,11 @@
 2+#ifndef ULEUPIPE_H
 3+#define ULEUPIPE_H
 4+
 5+typedef struct {
 6+	int fd;
 7+} ulepipe;
 8+
 9+int ulepipe_connect(ulepipe* ctx, const char* path);
10+
11+#endif /* ULEUPIPE_H */
12+
+55, -0
 1@@ -0,0 +1,55 @@
 2+#include <sys/socket.h>
 3+#include <sys/un.h>
 4+#include <unistd.h>
 5+#include <string.h>
 6+#include <errno.h>
 7+
 8+#include "ulepipe.h"
 9+
10+int
11+ulepipe_connect(ulepipe* ctx, const char* path)
12+{
13+	struct sockaddr_un addr = { 0 };
14+	int fd = -1;
15+
16+	if (!ctx || !path) {
17+		errno = EINVAL;
18+		return -1;
19+	}
20+
21+	if (strlen(path) >= sizeof(addr.sun_path)) {
22+		errno = ENAMETOOLONG;
23+		return -1;
24+	}
25+
26+	fd = socket(AF_UNIX, SOCK_STREAM, 0);
27+	if (fd == -1)
28+		return -1;
29+
30+	memset(&addr, 0, sizeof(addr));
31+	addr.sun_family = AF_UNIX;
32+	strcpy(addr.sun_path, path);
33+
34+	if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
35+		int saved_errno = errno;
36+		close(fd);
37+		errno = saved_errno;
38+		return -1;
39+	}
40+
41+	ctx->fd = fd;
42+	return 0;
43+}
44+
45+void
46+tpipe_close(ulepipe* ctx)
47+{
48+	if (!ctx)
49+		return;
50+
51+	if (ctx->fd >= 0) {
52+		close(ctx->fd);
53+		ctx->fd = -1;
54+	}
55+}
56+
+17, -0
 1@@ -0,0 +1,17 @@
 2+#include <stdlib.h>
 3+
 4+#include "ulepipe.h"
 5+
 6+struct {
 7+	int   x;
 8+	int   y;
 9+	int   z;
10+	char* name;
11+} state = { 0 };
12+
13+int main(void)
14+{
15+	ulepipe master = { 0 };
16+	return EXIT_SUCCESS;
17+}
18+
+17, -0
 1@@ -0,0 +1,17 @@
 2+#include <stdlib.h>
 3+
 4+#include "ulepipe.h"
 5+
 6+struct {
 7+	int   x;
 8+	int   y;
 9+	int   z;
10+	char* name;
11+} state = { 0 };
12+
13+int main(void)
14+{
15+	ulepipe master = { 0 };
16+	return EXIT_SUCCESS;
17+}
18+