cleanup src/linux and src/misc trees, etc.

previously, it was pretty much random which one of these trees a given
function appeared in. they have now been organized into:

src/linux: non-POSIX linux syscalls (possibly shard with other nixen)
src/legacy: various obsolete/legacy functions, mostly wrappers
src/misc: still mostly uncategorized; some misc POSIX, some nonstd
src/crypt: crypt hash functions

further cleanup will be done later.
This commit is contained in:
Rich Felker 2012-09-07 00:48:25 -04:00
commit b9bb8f67bb
45 changed files with 74 additions and 98 deletions

View file

@ -1,33 +0,0 @@
#define _GNU_SOURCE
#include <fcntl.h>
#include <unistd.h>
int daemon(int nochdir, int noclose)
{
if (!nochdir && chdir("/"))
return -1;
if (!noclose) {
int fd, failed = 0;
if ((fd = open("/dev/null", O_RDWR)) < 0) return -1;
if (dup2(fd, 0) < 0 || dup2(fd, 1) < 0 || dup2(fd, 2) < 0)
failed++;
if (fd > 2) close(fd);
if (failed) return -1;
}
switch(fork()) {
case 0: break;
case -1: return -1;
default: _exit(0);
}
if (setsid() < 0) return -1;
switch(fork()) {
case 0: break;
case -1: return -1;
default: _exit(0);
}
return 0;
}

27
src/linux/epoll.c Normal file
View file

@ -0,0 +1,27 @@
#include <sys/epoll.h>
#include "syscall.h"
int epoll_create(int size)
{
return syscall(SYS_epoll_create, size);
}
int epoll_create1(int flags)
{
return syscall(SYS_epoll_create1, flags);
}
int epoll_ctl(int fd, int op, int fd2, struct epoll_event *ev)
{
return syscall(SYS_epoll_ctl, fd, op, fd2, ev);
}
int epoll_pwait(int fd, struct epoll_event *ev, int cnt, int to, const sigset_t *sigs)
{
return syscall(SYS_epoll_pwait, fd, ev, cnt, to, sigs, __SYSCALL_SSLEN);
}
int epoll_wait(int fd, struct epoll_event *ev, int cnt, int to)
{
return syscall(SYS_epoll_wait, fd, ev, cnt, to);
}

View file

@ -1,7 +0,0 @@
#include <sys/epoll.h>
#include "syscall.h"
int epoll_create(int size)
{
return syscall(SYS_epoll_create, size);
}

View file

@ -1,7 +0,0 @@
#include <sys/epoll.h>
#include "syscall.h"
int epoll_create1(int flags)
{
return syscall(SYS_epoll_create1, flags);
}

View file

@ -1,7 +0,0 @@
#include <sys/epoll.h>
#include "syscall.h"
int epoll_ctl(int fd, int op, int fd2, struct epoll_event *ev)
{
return syscall(SYS_epoll_ctl, fd, op, fd2, ev);
}

View file

@ -1,7 +0,0 @@
#include <sys/epoll.h>
#include "syscall.h"
int epoll_pwait(int fd, struct epoll_event *ev, int cnt, int to, const sigset_t *sigs)
{
return syscall(SYS_epoll_pwait, fd, ev, cnt, to, sigs, __SYSCALL_SSLEN);
}

View file

@ -1,7 +0,0 @@
#include <sys/epoll.h>
#include "syscall.h"
int epoll_wait(int fd, struct epoll_event *ev, int cnt, int to)
{
return syscall(SYS_epoll_wait, fd, ev, cnt, to);
}

View file

@ -1,60 +0,0 @@
#include <err.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
void vwarn(const char *fmt, va_list ap)
{
if (fmt) vfprintf(stderr, fmt, ap);
perror("");
}
void vwarnx(const char *fmt, va_list ap)
{
if (fmt) vfprintf(stderr, fmt, ap);
putc('\n', stderr);
}
_Noreturn void verr(int status, const char *fmt, va_list ap)
{
vwarn(fmt, ap);
exit(status);
}
_Noreturn void verrx(int status, const char *fmt, va_list ap)
{
vwarnx(fmt, ap);
exit(status);
}
void warn(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vwarn(fmt, ap);
va_end(ap);
}
void warnx(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vwarnx(fmt, ap);
va_end(ap);
}
_Noreturn void err(int status, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verr(status, fmt, ap);
va_end(ap);
}
_Noreturn void errx(int status, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verrx(status, fmt, ap);
va_end(ap);
}

View file

@ -1,7 +1,18 @@
#include <sys/eventfd.h>
#include <unistd.h>
#include "syscall.h"
int eventfd(unsigned int count, int flags)
{
return syscall(flags ? SYS_eventfd2 : SYS_eventfd, count, flags);
}
int eventfd_read(int fd, eventfd_t *value)
{
return (sizeof(*value) == read(fd, value, sizeof(*value))) ? 0 : -1;
}
int eventfd_write(int fd, eventfd_t value)
{
return (sizeof(value) == write(fd, &value, sizeof(value))) ? 0 : -1;
}

View file

@ -1,7 +0,0 @@
#include <sys/eventfd.h>
#include <unistd.h>
int eventfd_read(int fd, eventfd_t *value)
{
return (sizeof(*value) == read(fd, value, sizeof(*value))) ? 0 : -1;
}

View file

@ -1,7 +0,0 @@
#include <sys/eventfd.h>
#include <unistd.h>
int eventfd_write(int fd, eventfd_t value)
{
return (sizeof(value) == write(fd, &value, sizeof(value))) ? 0 : -1;
}

View file

@ -1,9 +0,0 @@
#include <limits.h>
#include <sys/resource.h>
int getdtablesize(void)
{
struct rlimit rl;
getrlimit(RLIMIT_NOFILE, &rl);
return rl.rlim_max < INT_MAX ? rl.rlim_max : INT_MAX;
}

View file

@ -1,4 +0,0 @@
long gethostid()
{
return 0;
}

View file

@ -1,52 +0,0 @@
#define _GNU_SOURCE
#include <stddef.h>
#include <getopt.h>
#include <stdio.h>
static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
{
if (optind >= argc || !argv[optind] || argv[optind][0] != '-') return -1;
if ((longonly && argv[optind][1]) ||
(argv[optind][1] == '-' && argv[optind][2]))
{
int i;
for (i=0; longopts[i].name; i++) {
const char *name = longopts[i].name;
char *opt = argv[optind]+1;
if (*opt == '-') opt++;
for (; *name && *name == *opt; name++, opt++);
if (*name || (*opt && *opt != '=')) continue;
if (*opt == '=') {
if (!longopts[i].has_arg) continue;
optarg = opt+1;
} else {
if (longopts[i].has_arg == required_argument) {
if (!(optarg = argv[++optind]))
return ':';
} else optarg = NULL;
}
optind++;
if (idx) *idx = i;
if (longopts[i].flag) {
*longopts[i].flag = longopts[i].val;
return 0;
}
return longopts[i].val;
}
if (argv[optind][1] == '-') {
optind++;
return '?';
}
}
return getopt(argc, argv, optstring);
}
int getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx)
{
return __getopt_long(argc, argv, optstring, longopts, idx, 0);
}
int getopt_long_only(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx)
{
return __getopt_long(argc, argv, optstring, longopts, idx, 1);
}

View file

@ -1,7 +0,0 @@
#include <unistd.h>
#include <limits.h>
int getpagesize(void)
{
return PAGE_SIZE;
}

View file

@ -1,39 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
char *getpass(const char *prompt)
{
int fd;
struct termios s, t;
ssize_t l;
static char password[128];
if ((fd = open("/dev/tty", O_RDONLY|O_NOCTTY)) < 0) fd = 0;
tcgetattr(fd, &t);
s = t;
t.c_lflag &= ~(ECHO|ISIG);
t.c_lflag |= ICANON;
t.c_iflag &= ~(INLCR|IGNCR);
t.c_iflag |= ICRNL;
tcsetattr(fd, TCSAFLUSH, &t);
tcdrain(fd);
fputs(prompt, stderr);
fflush(stderr);
l = read(fd, password, sizeof password);
if (l >= 0) {
if (l > 0 && password[l-1] == '\n') l--;
password[l] = 0;
}
tcsetattr(fd, TCSAFLUSH, &s);
if (fd > 2) close(fd);
return password;
}

View file

@ -1,15 +0,0 @@
#include <sys/types.h>
#include <unistd.h>
#include <grp.h>
#include <limits.h>
int getgrouplist(const char *, gid_t, gid_t *, int *);
int setgroups(size_t, const gid_t *);
int initgroups(const char *user, gid_t gid)
{
gid_t groups[NGROUPS_MAX];
int count = NGROUPS_MAX;
if (getgrouplist(user, gid, groups, &count) < 0) return -1;
return setgroups(count, groups);
}

21
src/linux/inotify.c Normal file
View file

@ -0,0 +1,21 @@
#include <sys/inotify.h>
#include "syscall.h"
int inotify_init()
{
return syscall(SYS_inotify_init);
}
int inotify_init1(int flags)
{
return syscall(SYS_inotify_init1, flags);
}
int inotify_add_watch(int fd, const char *pathname, uint32_t mask)
{
return syscall(SYS_inotify_add_watch, fd, pathname, mask);
}
int inotify_rm_watch(int fd, uint32_t wd)
{
return syscall(SYS_inotify_rm_watch, fd, wd);
}

View file

@ -1,7 +0,0 @@
#include <sys/inotify.h>
#include "syscall.h"
int inotify_add_watch(int fd, const char *pathname, uint32_t mask)
{
return syscall(SYS_inotify_add_watch, fd, pathname, mask);
}

View file

@ -1,7 +0,0 @@
#include <sys/inotify.h>
#include "syscall.h"
int inotify_init()
{
return syscall(SYS_inotify_init);
}

View file

@ -1,7 +0,0 @@
#include <sys/inotify.h>
#include "syscall.h"
int inotify_init1(int flags)
{
return syscall(SYS_inotify_init1, flags);
}

View file

@ -1,7 +0,0 @@
#include <sys/inotify.h>
#include "syscall.h"
int inotify_rm_watch(int fd, uint32_t wd)
{
return syscall(SYS_inotify_rm_watch, fd, wd);
}

View file

@ -1,68 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <mntent.h>
#include <errno.h>
FILE *setmntent(const char *name, const char *mode)
{
return fopen(name, mode);
}
int endmntent(FILE *f)
{
fclose(f);
return 1;
}
struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen)
{
int cnt, n[8];
mnt->mnt_freq = 0;
mnt->mnt_passno = 0;
do {
fgets(linebuf, buflen, f);
if (feof(f) || ferror(f)) return 0;
if (!strchr(linebuf, '\n')) {
fscanf(f, "%*[^\n]%*[\n]");
errno = ERANGE;
return 0;
}
cnt = sscanf(linebuf, " %n%*s%n %n%*s%n %n%*s%n %n%*s%n %d %d",
n, n+1, n+2, n+3, n+4, n+5, n+6, n+7,
&mnt->mnt_freq, &mnt->mnt_passno);
} while (cnt < 2 || linebuf[n[0]] == '#');
linebuf[n[1]] = 0;
linebuf[n[3]] = 0;
linebuf[n[5]] = 0;
linebuf[n[7]] = 0;
mnt->mnt_fsname = linebuf+n[0];
mnt->mnt_dir = linebuf+n[2];
mnt->mnt_type = linebuf+n[4];
mnt->mnt_opts = linebuf+n[6];
return mnt;
}
struct mntent *getmntent(FILE *f)
{
static char linebuf[256];
static struct mntent mnt;
return getmntent_r(f, &mnt, linebuf, sizeof linebuf);
}
int addmntent(FILE *f, const struct mntent *mnt)
{
if (fseek(f, 0, SEEK_END)) return 1;
return fprintf(f, "%s\t%s\t%s\t%s\t%d\t%d\n",
mnt->mnt_fsname, mnt->mnt_dir, mnt->mnt_type, mnt->mnt_opts,
mnt->mnt_freq, mnt->mnt_passno) < 0;
}
char *hasmntopt(const struct mntent *mnt, const char *opt)
{
return strstr(mnt->mnt_opts, opt);
}

View file

@ -5,3 +5,13 @@ int mount(const char *special, const char *dir, const char *fstype, unsigned lon
{
return syscall(SYS_mount, special, dir, fstype, flags, data);
}
int umount(const char *special)
{
return syscall(SYS_umount2, special, 0);
}
int umount2(const char *special, int flags)
{
return syscall(SYS_umount2, special, flags);
}

8
src/linux/prlimit.c Normal file
View file

@ -0,0 +1,8 @@
#include <unistd.h>
#include <sys/resource.h>
#include "syscall.h"
int prlimit(pid_t pid, int resource, const struct rlimit *new_limit, struct rlimit *old_limit)
{
return syscall(SYS_prlimit64, pid, resource, new_limit, old_limit);
}

25
src/linux/ptrace.c Normal file
View file

@ -0,0 +1,25 @@
#include <sys/ptrace.h>
#include <stdarg.h>
#include <unistd.h>
#include "syscall.h"
long ptrace(int req, ...)
{
va_list ap;
pid_t pid;
void *addr, *data, *addr2;
long ret, result;
va_start(ap, req);
pid = va_arg(ap, pid_t);
addr = va_arg(ap, void *);
data = va_arg(ap, void *);
addr2 = va_arg(ap, void *);
va_end(ap);
if (req-1U < 3) data = &result;
ret = syscall(SYS_ptrace, req, pid, addr, data, addr2);
if (ret < 0 || req-1U >= 3) return ret;
return result;
}

View file

@ -5,3 +5,8 @@ int swapon(const char *path, int flags)
{
return syscall(SYS_swapon, path, flags);
}
int swapoff(const char *path)
{
return syscall(SYS_swapoff, path);
}

View file

@ -1,7 +0,0 @@
#include <sys/swap.h>
#include "syscall.h"
int swapoff(const char *path)
{
return syscall(SYS_swapoff, path);
}

View file

@ -1,19 +0,0 @@
#include "syscall.h"
#include <stdarg.h>
#undef syscall
long syscall(long n, ...)
{
va_list ap;
long a,b,c,d,e,f;
va_start(ap, n);
a=va_arg(ap, long);
b=va_arg(ap, long);
c=va_arg(ap, long);
d=va_arg(ap, long);
e=va_arg(ap, long);
f=va_arg(ap, long);
va_end(ap);
return __syscall_ret(__syscall(n,a,b,c,d,e,f));
}

View file

@ -1,7 +0,0 @@
#include <sys/mount.h>
#include "syscall.h"
int umount(const char *special)
{
return syscall(SYS_umount2, special, 0);
}

View file

@ -1,7 +0,0 @@
#include <sys/mount.h>
#include "syscall.h"
int umount2(const char *special, int flags)
{
return syscall(SYS_umount2, special, flags);
}