aboutsummaryrefslogtreecommitdiffstats
path: root/sdk/proc
diff options
context:
space:
mode:
authorWilliam Harrington <kb0iic@berzerkula.org>2025-01-14 16:06:02 -0600
committerWilliam Harrington <kb0iic@berzerkula.org>2025-01-14 16:06:02 -0600
commit0cc9b20c15460213e488bf5e70963b941482f628 (patch)
treebb0143245583ec846630f39bfa2258dba640ccd7 /sdk/proc
parent0e084ade5069756d487b5c948c48b777e37c00c9 (diff)
Add source.
Diffstat (limited to 'sdk/proc')
-rw-r--r--sdk/proc/Makefile.in18
-rw-r--r--sdk/proc/bind.conf1
-rw-r--r--sdk/proc/make.conf5
-rw-r--r--sdk/proc/pdetach.c78
-rw-r--r--sdk/proc/priority.c73
-rw-r--r--sdk/proc/process.h57
-rw-r--r--sdk/proc/spawn.c92
7 files changed, 324 insertions, 0 deletions
diff --git a/sdk/proc/Makefile.in b/sdk/proc/Makefile.in
new file mode 100644
index 0000000..94ae3cb
--- /dev/null
+++ b/sdk/proc/Makefile.in
@@ -0,0 +1,18 @@
+#
+# Template to build our "other" object modules(libother.a)
+# $Id: Makefile.in 1.2 Wed, 19 Mar 1997 12:44:53 -0500 dyfet $
+# Copyright (c) 1997 by Tycho Softworks.
+#
+
+OBJS = spawn.o pdetach.o priority.o
+
+.c.o:
+ $(CC) $(CFLAGS) $(OPTIMIZE) -I.. -o $@ -c $<
+ $(AR) r ../lib/libproc.a $@
+
+all: $(OBJS)
+ ranlib ../lib/libproc.a
+
+clean:
+ rm *.o
+
diff --git a/sdk/proc/bind.conf b/sdk/proc/bind.conf
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/sdk/proc/bind.conf
@@ -0,0 +1 @@
+
diff --git a/sdk/proc/make.conf b/sdk/proc/make.conf
new file mode 100644
index 0000000..2ce0aaa
--- /dev/null
+++ b/sdk/proc/make.conf
@@ -0,0 +1,5 @@
+inc=$CONFIG_HOST/include
+
+fn_find_file SYS_RESOURCE_H_MISSING $inc/sys/resource.h
+fn_find_type SETPRIORITY_F_MISSING setpriority $inc/sys/resource.h
+
diff --git a/sdk/proc/pdetach.c b/sdk/proc/pdetach.c
new file mode 100644
index 0000000..40814e4
--- /dev/null
+++ b/sdk/proc/pdetach.c
@@ -0,0 +1,78 @@
+/*
+ * Deamon-ify a user process; detach from controlling terminal and parent.
+ * $Id: daemon.c 1.2 Wed, 19 Mar 1997 12:44:53 -0500 dyfet $
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions on distribution and reuse see product license.
+ *
+ * Abstract:
+ * Daemon processes are commonly used in UNIX to build server
+ * applications. This module captures the essense of functionality
+ * required to make a process into a daemon within a single function
+ * call.
+ *
+ * Functions:
+ * daemon() - convert user process into a daemon.
+ */
+
+#include <proc/process.h>
+#include <std/signal.h>
+
+/* Daemonify a user process under UNIX.
+ *
+ * Abstract:
+ * In UNIX, a user process becomes a daemon by detaching itself from
+ * it's parent process and establishes it's own process group. A
+ * daemon may also detach itself from it's controlling terminal.
+ * This is usually accomplished through fork().
+ *
+ * Paramaters:
+ * flag - specifies daemon mode of operation:
+ * D_KEEPALL keeps all files open.
+ * D_KEEPSTDIO keeps stdio (stdin, stdout, stderr) open.
+ * D_KEEPNONIO detaches from stdio, keeps other files.
+ * D_KEEPNONE closes all open files.
+ *
+ * Returns:
+ * New pid of user process running as a daemon.
+ *
+ * Exceptions:
+ * Any failure terminates the process. No error message is possible
+ * since the process may already be detached from user I/O.
+ */
+
+pid_t pdetach(int flag)
+{
+ pid_t pid;
+ int max = OPEN_MAX;
+ int i;
+
+ signal(SIGHUP, SIG_IGN);
+
+ i = 0;
+ if(flag == D_KEEPSTDIO)
+ i = 3;
+
+ if(flag == D_KEEPNONIO)
+ max = 3;
+
+ while((i < max) && (flag != D_KEEPALL))
+ close(i++);
+
+ pid = fork();
+ if(pid < 0)
+ return pid;
+
+ if(pid > 0)
+ exit(EX_OK);
+
+ setsid();
+ setpgid(0, getpid());
+ pid = fork();
+ if(pid < 0)
+ return pid;
+
+ if(pid > 0)
+ exit(EX_OK);
+
+ return getpid();
+}
diff --git a/sdk/proc/priority.c b/sdk/proc/priority.c
new file mode 100644
index 0000000..8818567
--- /dev/null
+++ b/sdk/proc/priority.c
@@ -0,0 +1,73 @@
+/*
+ * Specify process for soft realtime scheduling treatment.
+ * $Id$
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions of distribution and reuse see product license.
+ */
+
+#include <proc/process.h>
+#include <std/time.h>
+
+#ifndef SYS_RESOURCE_H_MISSING
+#include <sys/resource.h>
+#endif
+
+#ifndef SETPRIORITY_F_MISSING
+
+int priority(int pri)
+{
+#ifdef _POSIX_PRIORITY_SCHEDULING
+ #define _P __P
+
+ #include <sched.h>
+
+ struct sched_param p;
+#endif
+ int newpri = getpriority(PRIO_PROCESS, 0) - pri - 1;
+
+ if(setpriority(PRIO_PROCESS, 0, newpri))
+ return -1;
+
+#ifdef _POSIX_MEMLOCK
+ #include <linux/mman.h>
+
+ if(mlockall(MCL_CURRENT | MCL_FUTURE))
+ return -1;
+#endif
+
+#ifdef _POSIX_PRIORITY_SCHEDULING
+ if(pri > 0)
+ {
+ p.sched_priority = sched_get_priority_min(SCHED_RR) + pri - 1;
+ if(sched_setscheduler(0, SCHED_RR, &p))
+ return -1;
+ }
+#endif
+
+ return 0;
+}
+
+#else
+
+int priority(int priority)
+{
+ return nice(- priority - 1);
+}
+#endif
+
+#ifdef _POSIX_PRIORITY_SCHEDULING
+#include <sched.h>
+
+void yield(void)
+{
+ sched_yield();
+}
+#else
+
+void yield(void)
+{
+ sleep(0);
+}
+#endif
+
+
diff --git a/sdk/proc/process.h b/sdk/proc/process.h
new file mode 100644
index 0000000..d65b765
--- /dev/null
+++ b/sdk/proc/process.h
@@ -0,0 +1,57 @@
+/*
+ * Portable process handling routines.
+ * $Id$
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions of distribution and reuse see product license.
+ */
+
+#ifndef __PROC_PROCESS_H__
+#define __PROC_PROCESS_H__
+
+#ifndef __STD_PROCESS_H__
+#include <std/process.h>
+#endif
+
+/* Spawn services under UNIX fork() */
+
+#define P_NOWAIT 0x00 /* Default, run concurrent */
+#define P_WAIT 0x01 /* Wait for and return exit status */
+#define P_BACKGROUND 0x02 /* Detach child from our stdio */
+#define P_SESSION 0x04 /* Use setsid() on child */
+#define P_OVERLAY 0x08 /* Really 'exec' called via spawn */
+
+/* Some common/portable spawn varients and masks */
+
+#define P_DETACH P_NOWAIT | P_SESSION | P_BACKGROUND
+#define P_NOWAITO P_NOWAIT | P_SESSION
+
+/* Daemon initialization options */
+
+#define D_KEEPSTDIO 0 /* Daemon keeps stdio connection */
+#define D_KEEPALL 1 /* Daemon keeps all open files */
+#define D_KEEPNONIO 2 /* Detach from stdio, keep others open */
+#define D_KEEPNONE 3 /* Deamon closes all files */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Common process invokation services using fork() */
+
+#ifdef __NAMESPACE
+#define spawnv __NAMESPACE(spawnv)
+#define spawnvp __NAMESPACE(spawnvp)
+#define pdetach __NAMESPACE(pdetach)
+#define priority __NAMEPSACE(priority)
+#endif
+
+int spawnv(const int P_mode, const char *path, char *const argv[]);
+int spawnvp(const int P_mode, const char *path, char *const argv[]);
+pid_t pdetach(const int D_flag); /* Make current process a daemon */
+int priority(int pri); /* set realtime priorities */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/sdk/proc/spawn.c b/sdk/proc/spawn.c
new file mode 100644
index 0000000..2b6d7db
--- /dev/null
+++ b/sdk/proc/spawn.c
@@ -0,0 +1,92 @@
+/*
+ * Spawn services built on fork() for those libc's that lack spawning.
+ * $Id$
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions of distribution and reuse see product license.
+ */
+
+#include <proc/process.h>
+#include <std/files.h>
+
+static void spawn_redirect(int io, int mode)
+{
+ int fd = open("/dev/null", mode);
+
+ if(fd < 0)
+ exit(EX_OSFILE);
+
+ if(fd != io)
+ {
+ dup2(fd, io);
+ close(fd);
+ }
+}
+
+static int spawn_wait(pid_t pid, int mode)
+{
+ int waitflag = mode & P_WAIT;
+
+ /* -1 on fork failure */
+
+ if(pid == -1)
+ return -1;
+
+ if(!waitflag)
+ return pid;
+
+ waitpid(pid, &waitflag, 0);
+ return WEXITSTATUS(waitflag);
+}
+
+static void spawn_mode(int mode)
+{
+ int i;
+
+ if(mode & P_BACKGROUND)
+ {
+ spawn_redirect(0, O_RDONLY);
+ spawn_redirect(1, O_WRONLY);
+ spawn_redirect(2, O_WRONLY);
+ }
+
+ for(i = 3; i < OPEN_MAX; ++i)
+ close(i);
+
+ if(mode & P_SESSION)
+ setsid();
+}
+
+int spawnv(const int mode, const char *path, char *const argv[])
+{
+ pid_t pid = 0;
+ int status;
+
+ if(!(mode & P_OVERLAY))
+ pid = fork();
+
+ if(pid)
+ return spawn_wait(pid, mode);
+
+ spawn_mode(mode);
+ execv(path, argv);
+ exit(EX_UNAVAILABLE);
+ return -1;
+}
+
+int spawnvp(const int mode, const char *path, char *const argv[])
+{
+ pid_t pid = 0;
+ int status;
+
+ if(!(mode & P_OVERLAY))
+ pid = fork();
+
+ if(pid)
+ return spawn_wait(pid, mode);
+
+ spawn_mode(mode);
+ execvp(path, argv);
+ exit(EX_UNAVAILABLE);
+ return -1;
+}
+