diff options
| -rw-r--r-- | .gitignore | 7 | ||||
| -rw-r--r-- | LICENSE | 10 | ||||
| -rw-r--r-- | Makefile | 6 | ||||
| -rw-r--r-- | README | 35 | ||||
| -rw-r--r-- | sami.3 | 47 | ||||
| -rw-r--r-- | sami.c | 55 | ||||
| -rw-r--r-- | sami.h | 23 |
7 files changed, 183 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7249418 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*~ +#*# +.wakatime-project +*.o +*.a +build +main \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7ae8865 --- /dev/null +++ b/LICENSE @@ -0,0 +1,10 @@ +Permission to use, copy, modify, and/or distribute this software for +any purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL +WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE +FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY +DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN +AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d32d7da --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +all: sami.o +clean: + rm -f sami.o + +README: + mandoc -Tascii sami.3 | col -b > README diff --git a/README b/README new file mode 100644 index 0000000..1f0cb82 --- /dev/null +++ b/README @@ -0,0 +1,35 @@ +SAI(3) Library Functions Manual SAI(3) + +NAME + sami - simple actor model implementation + +SYNOPSIS + #include <sami.h> + + int + SAMI_make(SAMI *actor, SAMI_Handler *handler, void *arg); + + int + SAMI_send(SAMI *actor, void *buf, size_t length); + + int + SAMI_recv(void *buf, size_t length); + + int + SAMI_kill(SAMI *actor); + +DESCRIPTION + sami is a library for handling actors. It uses fork(2) for making + actors, and unix(7) sockets for message delivering. + +RETURN VALUE + All functions return 0 on success, and any other number on failure. + Consider checking errno(7) in the latter case. + +SEE ALSO + fork(2), unix(7) + +AUTHORS + Nakidai Perumenei <nakidai@disroot.org> + +Linux 6.14.7-arch2-1 August 29, 2025 Linux 6.14.7-arch2-1 diff --git a/sami.3 b/sami.3 new file mode 100644 index 0000000..371c682 --- /dev/null +++ b/sami.3 @@ -0,0 +1,47 @@ +.Dd August 29, 2025 +.Dt SAI 3 +.Os +. +.Sh NAME +.Nm sami +.Nd simple actor model implementation +. +.Sh SYNOPSIS +. +.In sami.h +.Ft "int" +.Fn SAMI_make "SAMI *actor" "SAMI_Handler *handler" "void *arg" +. +.Ft "int" +.Fn SAMI_send "SAMI *actor" "void *buf" "size_t length" +. +.Ft "int" +.Fn SAMI_recv "void *buf" "size_t length" +. +.Ft "int" +.Fn SAMI_kill "SAMI *actor" +. +.Sh DESCRIPTION +.Nm +is a library +for handling actors. +It uses +.Xr fork 2 +for making actors, +and +.Xr unix 7 +sockets for message delivering. +. +.Sh RETURN VALUE +All functions return 0 on success, +and any other number on failure. +Consider checking +.Xr errno 7 +in the latter case. +. +.Sh SEE ALSO +.Xr fork 2 , +.Xr unix 7 +. +.Sh AUTHORS +.An Nakidai Perumenei Aq Mt nakidai@disroot.org diff --git a/sami.c b/sami.c new file mode 100644 index 0000000..64adf0a --- /dev/null +++ b/sami.c @@ -0,0 +1,55 @@ +#include "sami.h" + +#include <errno.h> +#include <signal.h> +#include <stdlib.h> + +#include <unistd.h> +#include <sys/socket.h> +#include <sys/wait.h> + + +int SAMI_make(SAMI *actor, SAMI_Handler *handler, void *arg) +{ + int pair[2]; + + if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) == -1) + return 1; + actor->fd = pair[0]; + + if (!(actor->pid = fork())) + { + SAMI__fd = pair[1]; + handler(arg); + exit(0); + } + + return actor->pid == -1; +} + +int SAMI_send(SAMI *actor, void *buf, size_t length) +{ + return send(actor->fd, buf, length, 0) == -1; +} + +int SAMI_recv(void *buf, size_t length) +{ + return recvfrom(SAMI__fd, buf, length, 0, 0, 0) == -1; +} + +int SAMI_kill(SAMI *actor) +{ + int res; + + if (kill(actor->pid, SIGKILL)) + return 1; + + for (;;) + if (waitpid(actor->pid, 0, 0) == -1) + if (errno == EINTR) + continue; + else + return 1; + else + return 0; +} diff --git a/sami.h b/sami.h new file mode 100644 index 0000000..53e5ad8 --- /dev/null +++ b/sami.h @@ -0,0 +1,23 @@ +#ifndef __SAMI_H__ +#define __SAMI_H__ + +#include <stddef.h> +#include <unistd.h> + +extern int SAMI__fd; + +struct SAMI +{ + int fd; + pid_t pid; +}; +typedef struct SAMI SAMI; + +typedef void SAMI_Handler(void *arg); + +int SAMI_make(SAMI *actor, SAMI_Handler *handler, void *arg); +int SAMI_send(SAMI *actor, void *buf, size_t length); +int SAMI_recv(void *buf, size_t length); +int SAMI_kill(SAMI *actor); + +#endif /* __SAMI_H__ */ |