commit 15fa8d79685fdc2290275ec5ea4e4528cb88903f
parent c95ba8d989f2a859308ebda05f2c9cfe09377552
Author: Friedel Schön <[email protected]>
Date: Tue, 14 May 2024 16:29:19 +0200
replacing libowfat with stdlib
Diffstat:
M | Makefile | | | 12 | +----------- |
M | minit-update.c | | | 70 | ++++++++++++++++++++++++++++++++++------------------------------------ |
M | minit.c | | | 71 | +++++++++++++---------------------------------------------------------- |
M | msvc.c | | | 72 | +++++++++++++++++++++++++++++++++++++++--------------------------------- |
4 files changed, 87 insertions(+), 138 deletions(-)
diff --git a/Makefile b/Makefile
@@ -23,14 +23,6 @@ else
MINITROOT=/etc/minit
endif
-LDLIBS=-lowfat
-
-libowfat_path = $(strip $(foreach dir,../libowfat*,$(wildcard $(dir)/textcode.h)))
-ifneq ($(libowfat_path),)
-CFLAGS+=$(foreach fnord,$(libowfat_path),-I$(dir $(fnord)))
-LDFLAGS+=$(foreach fnord,$(libowfat_path),-L$(dir $(fnord)))
-endif
-
minit: minit.o split.o openreadclose.o opendevconsole.o
msvc: msvc.o
minit-update: minit-update.o
@@ -43,9 +35,7 @@ pidfilehack: pidfilehack.o
$(CROSS)$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
clean:
- rm -f *.o minit msvc pidfilehack hard-reboot write_proc killall5 \
- shutdown minit-update serdo ftrigger waitinterface waitport \
- governor powersave sepcode
+ rm -f *.o minit msvc pidfilehack minit-update
install-files:
install -d $(DESTDIR)$(MINITROOT) $(DESTDIR)/sbin $(DESTDIR)/bin $(DESTDIR)$(MANDIR)/man8 $(DESTDIR)$(MANDIR)/man1
diff --git a/minit-update.c b/minit-update.c
@@ -3,8 +3,7 @@
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
-#include <libowfat/buffer.h>
-#include <libowfat/str.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
@@ -38,25 +37,25 @@ void addprocess(struct process* p);
void die(const char* msg) {
- buffer_putsflush(buffer_2, msg);
+ fputs(msg, stderr);
_exit(111);
}
-void buffer_putsnlflush(buffer* b, const char* msg) {
- buffer_puts(b, msg);
- buffer_putflush(b, "\n", 1);
-}
+// void buffer_putsnlflush(buffer* b, const char* msg) {
+// fputs(msg, b);
+// buffer_putflush(b, "\n", 1);
+// }
#ifdef WITH_STRERROR
void buffer_puts_strerror(const char* msg) {
- buffer_puts(buffer_2, "minit-update: ");
- buffer_puts(buffer_2, msg);
- buffer_putsnlflush(buffer_2, strerror(errno));
+ fputs("minit-update: ", stderr);
+ fputs(msg, stderr);
+ buffer_putsnlflush(stderr, strerror(errno));
}
#else
-# define buffer_puts_strerror(a) buffer_putsflush(buffer_2, a)
+# define buffer_puts_strerror(a) fputs(a, stderr)
#endif
@@ -70,17 +69,17 @@ void* xmalloc(size_t size) {
void copywrite(const char* service) {
strncpy(buf + 1, service, BUFLEN);
buf[BUFLEN] = 0;
- write(infd, buf, str_len(buf));
+ write(infd, buf, strlen(buf));
}
int read_reply_from_minit(void) {
if (read_outfd(buf, BUFLEN) == 1) {
if (buf[0] == '1') return 1;
- if (buf[0] == '0') buffer_puts(buffer_2, "expected '1' from minit, got '0' - minit too old?\n");
+ if (buf[0] == '0') fputs("expected '1' from minit, got '0' - minit too old?\n", stderr);
}
/* XXX: Uuuh. Should this be checked?
- else buffer_putsflush(buffer_2, "minit response not understood\n");
+ else fputs("minit response not understood\n", stderr);
*/
return 0;
}
@@ -95,26 +94,26 @@ void find_service(int subdir, char* name, char* parent) {
if (chdir(name)) return;
if (parent) {
- service = xmalloc(str_len(parent) + str_len(name) + 2);
+ service = xmalloc(strlen(parent) + strlen(name) + 2);
strcpy(service, parent);
strcat(service, "/");
strcat(service, name);
} else {
if (subdir) {
- service = xmalloc(str_len(name) + 1);
+ service = xmalloc(strlen(name) + 1);
strcpy(service, name);
}
}
#if 0
- buffer_putsnlflush(buffer_1,service);
+ buffer_putsnlflush(stdout,service);
#endif
if (service) { /* request and read a "struct process" from minit */
struct process tmp;
if (verbose) {
- buffer_puts(buffer_1, "minit-update: status for ");
- buffer_puts(buffer_1, service);
+ fputs("minit-update: status for ", stdout);
+ fputs(service, stdout);
}
buf[0] = 'D';
@@ -124,14 +123,14 @@ void find_service(int subdir, char* name, char* parent) {
case sizeof(tmp):
tmp.name = strdup(service);
addprocess(&tmp);
- if (verbose) buffer_puts(buffer_1, " saved.\n");
+ if (verbose) fputs(" saved.\n", stdout);
break;
case 1:
- if (verbose) buffer_puts(buffer_1, " failed - minit has no information on this service\n");
+ if (verbose) fputs(" failed - minit has no information on this service\n", stdout);
#if 0
break;
default:
- buffer_puts(buffer_1, " failed - read incomplete structure!\n");
+ fputs(" failed - read incomplete structure!\n", stdout);
#endif
}
}
@@ -146,13 +145,13 @@ void find_service(int subdir, char* name, char* parent) {
find_service(1, dir->d_name, service);
#if 0
} else {
- buffer_putsnlflush(buffer_1,dir->d_name);
+ buffer_putsnlflush(stdout,dir->d_name);
#endif
}
} else {
- buffer_puts(buffer_2, dir->d_name);
- buffer_puts(buffer_2, ": cannot stat\n");
- buffer_puts_strerror("lstat() failed: ");
+ fputs(dir->d_name, stderr);
+ fputs(": cannot stat\n", stderr);
+ fprintf(stderr, "lstat() failed: %s\n", strerror(errno));
}
}
} /* while */
@@ -163,7 +162,6 @@ ret:
if (service) free(service);
chdir(MINITROOT);
if (parent) chdir(parent);
- buffer_flush(buffer_1);
}
@@ -183,8 +181,8 @@ int main(int argc, char** argv) {
do_update = 1;
break;
default:
- buffer_puts(buffer_2, "minit-update: Unknown Option: ");
- buffer_putsnlflush(buffer_2, argv[argc]);
+ fputs("minit-update: Unknown Option: ", stderr);
+ fprintf(stderr, "%s\n", argv[argc]);
}
} else
die(USAGE);
@@ -196,7 +194,7 @@ int main(int argc, char** argv) {
if (infd < 0 || outfd < 0) die("could not open " MINITROOT "/in or " MINITROOT "/out\n");
while (lockf(infd, F_TLOCK, 1)) {
- buffer_puts_strerror("could not acquire lock: ");
+ fprintf(stderr, "could not acquire lock: %s\n", strerror(errno));
sleep(1);
}
@@ -205,10 +203,10 @@ int main(int argc, char** argv) {
if (maxprocess == -1)
die("Could not extract running services from minit\n");
- if (verbose) buffer_putsflush(buffer_1, "minit-update: telling minit to execve itself\n");
+ if (verbose) fputs("minit-update: telling minit to execve itself\n", stdout);
if (!do_update) {
- buffer_putsflush(buffer_2, "Test mode: No update done.\n");
+ fputs("Test mode: No update done.\n", stderr);
return 0;
}
@@ -217,8 +215,8 @@ int main(int argc, char** argv) {
for (i = 0; i <= maxprocess; i++) {
if (verbose) {
- buffer_puts(buffer_1, "minit-update: restoring status for ");
- buffer_putsnlflush(buffer_1, root[i].name);
+ fputs("minit-update: restoring status for ", stdout);
+ printf("%s\n", root[i].name);
}
buf[0] = 'U';
@@ -229,8 +227,8 @@ int main(int argc, char** argv) {
write(infd, &root[i], sizeof(struct process));
if (read_reply_from_minit() && verbose) {
- buffer_puts(buffer_1, "minit-update: restored service ");
- buffer_putsnlflush(buffer_1, root[i].name);
+ fputs("minit-update: restored service ", stdout);
+ printf("%s\n", root[i].name);
}
} /* for() */
diff --git a/minit.c b/minit.c
@@ -7,9 +7,6 @@
#include <alloca.h>
#include <errno.h>
#include <fcntl.h>
-#include <libowfat/compiletimeassert.h>
-#include <libowfat/fmt.h>
-#include <libowfat/str.h>
#include <limits.h>
#include <linux/kd.h>
#include <poll.h>
@@ -26,52 +23,9 @@
#include <time.h>
#include <unistd.h>
-compiletimeassert(sizeof(MINITROOT) + 64 < PATH_MAX);
-
-#define MALLOC_TEST
-#if !defined(__dietlibc__) && !defined(__GLIBC__)
-# undef MALLOC_TEST
-#endif
-
-#ifdef MALLOC_TEST
-extern void* __libc_malloc(size_t size);
-extern void* __libc_realloc(void* x, size_t size);
-extern void __libc_free(void* x);
-static char malloc_buf[1024];
-static unsigned long n;
-static struct process procbuf[50];
-void* malloc(size_t size) {
- if (n + size < sizeof(malloc_buf)) {
- char* tmp = malloc_buf + n;
- n += size;
- n = (n + 3) & ~3;
- return tmp;
- }
- return __libc_malloc(size);
-}
-void free(void* x) {
- if ((char*) x >= malloc_buf && (char*) x < malloc_buf + sizeof(malloc_buf)) return;
- __libc_free(x);
-}
-void* realloc(void* x, size_t size) {
- if (x == 0 || x == procbuf) {
- void* y;
- if (size <= sizeof(procbuf))
- return procbuf;
- y = __libc_malloc(size);
- if (!y) return 0;
- memcpy(y, x, size);
- return y;
- }
- return __libc_realloc(x, size);
-}
-#endif
-
static struct process* root;
-
-static int infd, outfd;
-static int maxprocess = -1;
-static int processalloc;
+static int maxprocess = -1;
+static int processalloc;
char** Argv;
@@ -158,7 +112,7 @@ int loadservice(char* service) {
tmp.__stdin = 0;
tmp.__stdout = 1;
{
- char* logservice = alloca(str_len(service) + 5);
+ char* logservice = alloca(strlen(service) + 5);
strcpy(logservice, service);
strcat(logservice, "/log");
tmp.logservice = loadservice(logservice);
@@ -197,7 +151,7 @@ void handlekilled(pid_t killed) {
{
char buf[50];
snprintf(buf, 50, " %d\n", killed);
- write(2, buf, str_len(buf));
+ write(2, buf, strlen(buf));
}
#endif
if (killed == (pid_t) -1) {
@@ -407,7 +361,7 @@ int startservice(int service, int pause, int father) {
if (root[service].circular)
return 0;
root[service].circular = 1;
- root[service].father = father;
+ root[service].father = father;
#ifdef HISTORY
{
memmove(history + 1, history, sizeof(int) * ((HISTORY) -1));
@@ -455,7 +409,7 @@ int startservice(int service, int pause, int father) {
}
static void _puts(const char* s) {
- write(1, s, str_len(s));
+ write(1, s, strlen(s));
}
void childhandler() {
@@ -494,6 +448,7 @@ int main(int argc, char* argv[]) {
struct pollfd pfd;
time_t last = time(0);
int nfds = 1;
+ int outfd, infd;
#ifdef HISTORY
for (i = 0; i < HISTORY; ++i)
@@ -608,7 +563,7 @@ int main(int argc, char* argv[]) {
int idx = 0, tmp;
buf[i] = 0;
-/* write(1,buf,str_len(buf)); write(1,"\n",1); */
+/* write(1,buf,strlen(buf)); write(1,"\n",1); */
#ifdef UPDATE
if (!strcmp(buf, "update")) {
execve("/sbin/minit", argv, environ);
@@ -623,7 +578,7 @@ int main(int argc, char* argv[]) {
else {
switch (buf[0]) {
case 'p':
- write(outfd, buf, fmt_ulong(buf, root[idx].pid));
+ dprintf(outfd, "%d", root[idx].pid);
break;
#ifdef UPDATE
case 'D':
@@ -657,7 +612,7 @@ int main(int argc, char* argv[]) {
}
goto ok;
case 'P': {
- unsigned char* x = (unsigned char*) buf + str_len(buf) + 1;
+ unsigned char* x = (unsigned char*) buf + strlen(buf) + 1;
unsigned char c;
tmp = 0;
while ((c = *x++ - '0') < 10) tmp = tmp * 10 + c;
@@ -683,7 +638,7 @@ int main(int argc, char* argv[]) {
write(outfd, "1", 1);
break;
case 'u':
- write(outfd, buf, fmt_ulong(buf, time(0) - root[idx].startedat));
+ dprintf(outfd, "%lu", time(0) - root[idx].startedat);
break;
case 'd':
write(outfd, "1:", 2);
@@ -691,7 +646,7 @@ int main(int argc, char* argv[]) {
int i;
for (i = 0; i <= maxprocess; ++i) {
if (root[i].father == idx)
- write(outfd, root[i].name, str_len(root[i].name) + 1);
+ write(outfd, root[i].name, strlen(root[i].name) + 1);
}
write(outfd, "\0", 2);
}
@@ -706,7 +661,7 @@ int main(int argc, char* argv[]) {
int i;
for (i = 0; i < HISTORY; ++i)
if (history[i] != -1)
- write(outfd, root[history[i]].name, str_len(root[history[i]].name) + 1);
+ write(outfd, root[history[i]].name, strlen(root[history[i]].name) + 1);
write(outfd, "\0", 2);
}
#else
diff --git a/msvc.c b/msvc.c
@@ -1,26 +1,35 @@
+#include "minit.h"
+
#include <errno.h>
#include <fcntl.h>
-#include <libowfat/buffer.h>
-#include <libowfat/errmsg.h>
-#include <libowfat/fmt.h>
-#include <libowfat/str.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <unistd.h>
-#include "minit.h"
static int infd, outfd;
static char buf[1500];
+#define FMT_ULONG 40
+
+size_t fmt_ulong(char* dest, unsigned long i) {
+ register unsigned long len, tmp, len2;
+ /* first count the number of bytes needed */
+ for (len = 1, tmp = i; tmp > 9; ++len) tmp /= 10;
+ if (dest)
+ for (tmp = i, dest += len, len2 = len + 1; --len2; tmp /= 10)
+ *--dest = (char) ((tmp % 10) + '0');
+ return len;
+}
+
void addservice(char* service) {
char* x;
- if (str_start(service, MINITROOT "/"))
+ if (strncmp(service, MINITROOT "/", strlen(service)) == 0)
service += sizeof(MINITROOT "/") - 1;
- x = service + str_len(service) - 1;
+ x = service + strlen(service) - 1;
while (x > service && *x == '/') {
*x = 0;
--x;
@@ -31,7 +40,7 @@ void addservice(char* service) {
int addreadwrite(char* service) {
addservice(service);
- write(infd, buf, str_len(buf));
+ write(infd, buf, strlen(buf));
return read(outfd, buf, 1500);
}
@@ -59,9 +68,9 @@ int setpid(char* service, pid_t pid) {
int len;
buf[0] = 'P';
addservice(service);
- tmp = buf + str_len(buf) + 1;
+ tmp = buf + strlen(buf) + 1;
tmp[fmt_ulong(tmp, pid)] = 0;
- write(infd, buf, str_len(buf) + str_len(tmp) + 2);
+ write(infd, buf, strlen(buf) + strlen(tmp) + 2);
len = read(outfd, buf, 1500);
return (len != 1 || buf[0] == '0');
}
@@ -106,7 +115,7 @@ void dumphistory() {
done = i = 0;
if (first) {
if (tmp[0] == '0') {
- carp("minit compiled without history support.");
+ fprintf(stderr, "minit compiled without history support.");
return;
}
i += 2;
@@ -137,7 +146,7 @@ void dumpdependencies(char* service) {
char first, last;
buf[0] = 'd';
addservice(service);
- write(infd, buf, str_len(buf));
+ write(infd, buf, strlen(buf));
first = 1;
last = 'x';
for (;;) {
@@ -147,7 +156,7 @@ void dumpdependencies(char* service) {
done = i = 0;
if (first) {
if (tmp[0] == '0') {
- carp(service, ": no such service.");
+ fprintf(stderr, service, ": no such service.");
return;
}
i += 2;
@@ -174,7 +183,7 @@ void dumpdependencies(char* service) {
int main(int argc, char* argv[]) {
if (argc < 2) {
- msg(
+ printf(
"usage: msvc -[uodpchaitkogC] service\n"
" msvc -Ppid service\n"
" -u\tup; start service with respawn\n"
@@ -194,12 +203,11 @@ int main(int argc, char* argv[]) {
" -C\tClear; remove service form active list\n");
return 0;
}
- errmsg_iam("msvc");
infd = open(MINITROOT "/in", O_WRONLY);
outfd = open(MINITROOT "/out", O_RDONLY);
if (infd >= 0) {
while (lockf(infd, F_LOCK, 1)) {
- carp("could not acquire lock!");
+ fprintf(stderr, "could not acquire lock!");
sleep(1);
}
if (argc == 2 && argv[1][1] != 'H') {
@@ -218,13 +226,11 @@ int main(int argc, char* argv[]) {
else if (pid == 1)
what = "finished ";
else {
- len = fmt_str(tmp, "up (pid ");
- len += fmt_ulong(tmp + len, pid);
- tmp[len + fmt_str(tmp + len, ") ")] = 0;
- what = tmp;
+ len = snprintf(tmp, sizeof(tmp), "up (pid %u)", pid);
+ what = tmp;
}
tmp2[fmt_ulong(tmp2, ut)] = 0;
- msg(argv[1], ": ", what, tmp2, " seconds");
+ printf("%s: %s%s seconds\n", argv[1], what, tmp2);
} else {
char tmp[FMT_ULONG * 2 + 5];
len = fmt_ulong(tmp, pid);
@@ -242,7 +248,7 @@ int main(int argc, char* argv[]) {
else
return 0;
} else
- carp(argv[1], ": no such service.");
+ fprintf(stderr, "%s: no such service\n.", argv[1]);
return 1;
} else {
int i;
@@ -255,7 +261,7 @@ int main(int argc, char* argv[]) {
for (i = 2; i < argc; ++i) {
pid = __readpid(argv[i]);
if (pid < 2) {
- carp(argv[i], pid == 1 ? ": service terminated" : ": no such service");
+ fprintf(stderr, "%s: %s\n", argv[i], pid == 1 ? "service terminated" : "no such service");
ret = 1;
} else {
char tmp[FMT_ULONG];
@@ -296,7 +302,7 @@ int main(int argc, char* argv[]) {
case 'o':
for (i = 2; i < argc; ++i)
if (startservice(argv[i]) || respawn(argv[i], 0)) {
- carp("Could not start ", argv[i]);
+ fprintf(stderr, "Could not start %s\n", argv[i]);
ret = 1;
}
break;
@@ -304,25 +310,25 @@ int main(int argc, char* argv[]) {
for (i = 2; i < argc; ++i) {
pid = __readpid(argv[i]);
if (pid == 0) {
- carp(argv[i], ": no such service");
+ fprintf(stderr, "%s: no such service\n", argv[i]);
ret = 1;
} else if (pid == 1)
continue;
else if (respawn(argv[i], 0) || kill(pid, SIGTERM) || kill(pid, SIGCONT))
- carp(argv[i], ": failed to send signal");
+ fprintf(stderr, "%s: failed to send signal\n", argv[i]);
}
break;
case 'u':
for (i = 2; i < argc; ++i)
if (startservice(argv[i]) || respawn(argv[i], 1)) {
- carp("Could not start ", argv[i]);
+ fprintf(stderr, "Could not start %s\n", argv[i]);
ret = 1;
}
break;
case 'C':
for (i = 2; i < argc; ++i)
if (check_remove(argv[i])) {
- carp(argv[i], " could not be cleared");
+ fprintf(stderr, "%s could not be cleared\n", argv[i]);
ret = 1;
}
break;
@@ -330,7 +336,7 @@ int main(int argc, char* argv[]) {
pid = atoi(argv[1] + 2);
if (pid > 1)
if (setpid(argv[2], pid)) {
- carp("Could not set PID of service ", argv[2]);
+ fprintf(stderr, "Could not set PID of service %s\n", argv[2]);
ret = 1;
}
break;
@@ -347,10 +353,10 @@ int main(int argc, char* argv[]) {
for (i = 2; i < argc; i++) {
pid = __readpid(argv[i]);
if (!pid) {
- carp(argv[i], ": no such service");
+ fprintf(stderr, "%s: no such service\n", argv[i]);
ret = 1;
} else if (pid == 1) {
- carp(argv[i], ": service not running");
+ fprintf(stderr, "%s: service not running\n", argv[i]);
ret = 1;
} else if (kill(pid, sig)) {
char tmp[FMT_ULONG];
@@ -371,14 +377,14 @@ int main(int argc, char* argv[]) {
}
tmp[fmt_ulong(tmp, sig)] = 0;
tmp2[fmt_ulong(tmp2, pid)] = 0;
- carp(argv[i], ": could not send signal ", tmp, " to PID ", tmp2, ": ", s);
+ fprintf(stderr, "%s: could not send signal %s to PID %s: %s\n", argv[i], tmp, tmp2, s);
ret = 1;
}
}
return ret;
}
} else {
- carp("minit: could not open " MINITROOT "/in or " MINITROOT "/out");
+ fprintf(stderr, "minit: could not open " MINITROOT "/in or " MINITROOT "/out\n");
return 1;
}
}