Remove OpenBSD support

This is becoming a nightmare.  Just support Linux.
This commit is contained in:
sin 2013-08-14 14:19:05 +01:00
parent 92c3390657
commit 97cbad7eef
14 changed files with 62 additions and 214 deletions

View File

@ -3,22 +3,15 @@ include config.mk
.POSIX:
.SUFFIXES: .c .o
HDR = util.h arg.h ubase.h
HDR = arg.h ubase.h util.h
LIB = \
$(OS)/dmesg.o \
$(OS)/grabmntinfo.o \
$(OS)/umount.o \
util/eprintf.o \
util/estrtol.o
util/estrtol.o \
util/grabmntinfo.o
SRC = \
df.c \
dmesg.c \
umount.c \
stat.c
ifeq ($(OS),linux)
SRC += \
halt.c \
insmod.c \
lsmod.c \
@ -26,10 +19,11 @@ SRC += \
mount.c \
reboot.c \
rmmod.c \
stat.c \
swapoff.c \
swapon.c \
umount.c \
unshare.c
endif
OBJ = $(SRC:.c=.o) $(LIB)
BIN = $(SRC:.c=)
@ -39,7 +33,6 @@ all: options binlib
options:
@echo ubase build options:
@echo "OS = $(OS)"
@echo "CFLAGS = $(CFLAGS)"
@echo "LDFLAGS = $(LDFLAGS)"
@echo "CC = $(CC)"

View File

@ -5,10 +5,6 @@ VERSION = 0.0
PREFIX = /usr/local
MANPREFIX = $(PREFIX)/share/man
# OS to build against
OS = linux
#OS = openbsd
#CC = gcc
#CC = musl-gcc
LD = $(CC)

2
df.c
View File

@ -2,7 +2,7 @@
#include <sys/statvfs.h>
#include <stdio.h>
#include <stdlib.h>
#include "ubase.h"
#include "grabmntinfo.h"
#include "util.h"
static void mnt_show(const char *fsname, const char *dir);

46
dmesg.c
View File

@ -1,10 +1,19 @@
/* See LICENSE file for copyright and license details. */
#include <sys/klog.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "ubase.h"
#include <string.h>
#include "grabmntinfo.h"
#include "util.h"
static int dmesg_show(int fd, const void *buf, size_t n);
enum {
SYSLOG_ACTION_READ_ALL = 3,
SYSLOG_ACTION_SIZE_BUFFER = 10
};
static void
usage(void)
{
@ -22,17 +31,17 @@ main(int argc, char *argv[])
usage();
} ARGEND;
n = dmesg_size();
n = klogctl(SYSLOG_ACTION_SIZE_BUFFER, NULL, 0);
if (n < 0)
eprintf("dmesg_size:");
eprintf("klogctl:");
buf = malloc(n);
if (!buf)
eprintf("malloc:");
n = dmesg_read(buf, n);
n = klogctl(SYSLOG_ACTION_READ_ALL, buf, n);
if (n < 0)
eprintf("dmesg_read:");
eprintf("klogctl:");
n = dmesg_show(STDOUT_FILENO, buf, n);
if (n < 0)
@ -41,3 +50,30 @@ main(int argc, char *argv[])
free(buf);
return 0;
}
int
dmesg_show(int fd, const void *buf, size_t n)
{
int last = '\n';
char newbuf[n], *q = newbuf;
const char *p = buf;
size_t i;
memset(newbuf, 0, n);
for (i = 0; i < n; ) {
if (last == '\n' && p[i] == '<') {
i += 2;
if (i + 1 < n && p[i + 1] == '>')
i++;
} else {
*q++ = p[i];
}
last = p[i++];
}
if (write(fd, newbuf, n) != n)
return -1;
if (last != '\n')
if (write(fd, "\n", 1) != 1)
return -1;
return 0;
}

8
grabmntinfo.h Normal file
View File

@ -0,0 +1,8 @@
/* See LICENSE file for copyright and license details. */
struct mntinfo {
const char *fsname;
const char *mntdir;
};
int grabmntinfo(struct mntinfo **minfo);

View File

@ -1,49 +0,0 @@
/* See LICENSE file for copyright and license details. */
#include <sys/klog.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
enum {
SYSLOG_ACTION_READ_ALL = 3,
SYSLOG_ACTION_SIZE_BUFFER = 10
};
int
dmesg_size(void)
{
return klogctl(SYSLOG_ACTION_SIZE_BUFFER, NULL, 0);
}
int
dmesg_read(void *buf, size_t n)
{
return klogctl(SYSLOG_ACTION_READ_ALL, buf, n);
}
int
dmesg_show(int fd, const void *buf, size_t n)
{
int last = '\n';
char newbuf[n], *q = newbuf;
const char *p = buf;
size_t i;
memset(newbuf, 0, n);
for (i = 0; i < n; ) {
if (last == '\n' && p[i] == '<') {
i += 2;
if (i + 1 < n && p[i + 1] == '>')
i++;
} else {
*q++ = p[i];
}
last = p[i++];
}
if (write(fd, newbuf, n) != n)
return -1;
if (last != '\n')
if (write(fd, "\n", 1) != 1)
return -1;
return 0;
}

View File

@ -1,17 +0,0 @@
/* See LICENSE file for copyright and license details. */
#include <sys/mount.h>
#include <stdio.h>
#include "../ubase.h"
#include "../util.h"
int
do_umount(const char *target, int opts)
{
int flags = 0;
if (opts & UBASE_MNT_FORCE)
flags |= MNT_FORCE;
if (opts & UBASE_MNT_DETACH)
flags |= MNT_DETACH;
return umount2(target, flags);
}

View File

@ -4,7 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ubase.h"
#include "grabmntinfo.h"
#include "util.h"
struct {

View File

@ -1,47 +0,0 @@
/* See LICENSE file for copyright and license details. */
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/msgbuf.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
dmesg_size(void)
{
int mib[2], msgbufsize;
size_t len;
int ret;
mib[0] = CTL_KERN;
mib[1] = KERN_MSGBUFSIZE;
len = sizeof(msgbufsize);
ret = sysctl(mib, 2, &msgbufsize, &len, NULL, 0);
if (ret < 0)
return ret;
msgbufsize += sizeof(struct msgbuf) - 1;
return msgbufsize;
}
int
dmesg_read(void *buf, size_t n)
{
int mib[2];
int ret;
memset(buf, 0, n);
mib[0] = CTL_KERN;
mib[1] = KERN_MSGBUF;
ret = sysctl(mib, 2, buf, &n, NULL, 0);
if (ret < 0)
return ret;
memmove(buf, ((struct msgbuf *)buf)->msg_bufc, n);
return n;
}
int
dmesg_show(int fd, const void *buf, size_t n)
{
return write(fd, buf, n);
}

View File

@ -1,29 +0,0 @@
/* See LICENSE file for copyright and license details. */
#include <sys/param.h>
#include <sys/mount.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../ubase.h"
#include "../util.h"
int
grabmntinfo(struct mntinfo **minfo)
{
int siz, i;
struct statfs *mntbuf;
struct mntinfo *mi;
siz = getmntinfo(&mntbuf, MNT_WAIT);
if (!siz)
eprintf("getmntinfo:");
mi = malloc(siz * sizeof(*mi));
if (!mi)
eprintf("malloc:");
for (i = 0; i < siz; i++) {
mi[i].fsname = strdup(mntbuf[i].f_mntfromname);
mi[i].mntdir = strdup(mntbuf[i].f_mntonname);
}
*minfo = mi;
return siz;
}

View File

@ -1,21 +0,0 @@
/* See LICENSE file for copyright and license details. */
#include <sys/param.h>
#include <sys/mount.h>
#include <errno.h>
#include <stdio.h>
#include "../ubase.h"
#include "../util.h"
int
do_umount(const char *target, int opts)
{
int flags = 0;
if (opts & UBASE_MNT_FORCE)
flags |= MNT_FORCE;
if (opts & UBASE_MNT_DETACH) {
errno = ENOTSUP;
return -1;
}
return unmount(target, flags);
}

22
ubase.h
View File

@ -1,22 +0,0 @@
/* See LICENSE file for copyright and license details. */
/* grabmntinfo.c */
struct mntinfo {
const char *fsname;
const char *mntdir;
};
int grabmntinfo(struct mntinfo **minfo);
/* dmesg.c */
int dmesg_size(void);
int dmesg_read(void *buf, size_t n);
int dmesg_show(int fd, const void *buf, size_t n);
/* umount.c */
enum {
UBASE_MNT_FORCE = 1 << 0,
UBASE_MNT_DETACH = 1 << 1
};
int do_umount(const char *target, int opts);

View File

@ -1,6 +1,6 @@
/* See LICENSE file for copyright and license details. */
#include <sys/mount.h>
#include <stdio.h>
#include "ubase.h"
#include "util.h"
static void
@ -17,10 +17,10 @@ main(int argc, char *argv[]) {
ARGBEGIN {
case 'f':
flags |= UBASE_MNT_FORCE;
flags |= MNT_FORCE;
break;
case 'l':
flags |= UBASE_MNT_DETACH;
flags |= MNT_DETACH;
break;
default:
usage();
@ -30,8 +30,8 @@ main(int argc, char *argv[]) {
usage();
for (i = 0; i < argc; i++) {
if (do_umount(argv[i], flags) < 0)
eprintf("do_umount:");
if (umount2(argv[i], flags) < 0)
eprintf("umount2:");
ret = 1;
}
return ret;

View File

@ -3,7 +3,7 @@
#include <stdlib.h>
#include <string.h>
#include <mntent.h>
#include "../ubase.h"
#include "../grabmntinfo.h"
#include "../util.h"
int