diff options
author | Rich Felker <dalias@aerifal.cx> | 2012-09-07 00:48:25 -0400 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2012-09-07 00:48:25 -0400 |
commit | b9bb8f67bbac9bab5314fb00974ad469476e936e (patch) | |
tree | 89fc3a733b4cd9a2346c5b77d8953b4f7aa9dd22 /src/linux | |
parent | 780aede41913528781bbc8ff0d0e27505595f7c0 (diff) | |
download | musl-b9bb8f67bbac9bab5314fb00974ad469476e936e.tar.gz |
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.
Diffstat (limited to 'src/linux')
31 files changed, 107 insertions, 404 deletions
diff --git a/src/linux/daemon.c b/src/linux/daemon.c deleted file mode 100644 index 1568b1dc..00000000 --- a/src/linux/daemon.c +++ /dev/null @@ -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; -} diff --git a/src/linux/epoll.c b/src/linux/epoll.c new file mode 100644 index 00000000..35f70ed1 --- /dev/null +++ b/src/linux/epoll.c @@ -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); +} diff --git a/src/linux/epoll_create.c b/src/linux/epoll_create.c deleted file mode 100644 index 29d82999..00000000 --- a/src/linux/epoll_create.c +++ /dev/null @@ -1,7 +0,0 @@ -#include <sys/epoll.h> -#include "syscall.h" - -int epoll_create(int size) -{ - return syscall(SYS_epoll_create, size); -} diff --git a/src/linux/epoll_create1.c b/src/linux/epoll_create1.c deleted file mode 100644 index 380b5dad..00000000 --- a/src/linux/epoll_create1.c +++ /dev/null @@ -1,7 +0,0 @@ -#include <sys/epoll.h> -#include "syscall.h" - -int epoll_create1(int flags) -{ - return syscall(SYS_epoll_create1, flags); -} diff --git a/src/linux/epoll_ctl.c b/src/linux/epoll_ctl.c deleted file mode 100644 index da3e999b..00000000 --- a/src/linux/epoll_ctl.c +++ /dev/null @@ -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); -} diff --git a/src/linux/epoll_pwait.c b/src/linux/epoll_pwait.c deleted file mode 100644 index 3ecdbb59..00000000 --- a/src/linux/epoll_pwait.c +++ /dev/null @@ -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); -} diff --git a/src/linux/epoll_wait.c b/src/linux/epoll_wait.c deleted file mode 100644 index 9d3924e0..00000000 --- a/src/linux/epoll_wait.c +++ /dev/null @@ -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); -} diff --git a/src/linux/err.c b/src/linux/err.c deleted file mode 100644 index 0f748538..00000000 --- a/src/linux/err.c +++ /dev/null @@ -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); -} diff --git a/src/linux/eventfd.c b/src/linux/eventfd.c index cb39a7ba..53066487 100644 --- a/src/linux/eventfd.c +++ b/src/linux/eventfd.c @@ -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; +} diff --git a/src/linux/eventfd_read.c b/src/linux/eventfd_read.c deleted file mode 100644 index 969e6615..00000000 --- a/src/linux/eventfd_read.c +++ /dev/null @@ -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; -} diff --git a/src/linux/eventfd_write.c b/src/linux/eventfd_write.c deleted file mode 100644 index 734fa367..00000000 --- a/src/linux/eventfd_write.c +++ /dev/null @@ -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; -} diff --git a/src/linux/getdtablesize.c b/src/linux/getdtablesize.c deleted file mode 100644 index 623a6af3..00000000 --- a/src/linux/getdtablesize.c +++ /dev/null @@ -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; -} diff --git a/src/linux/gethostid.c b/src/linux/gethostid.c deleted file mode 100644 index ea65611a..00000000 --- a/src/linux/gethostid.c +++ /dev/null @@ -1,4 +0,0 @@ -long gethostid() -{ - return 0; -} diff --git a/src/linux/getopt_long.c b/src/linux/getopt_long.c deleted file mode 100644 index 6d3a4a6e..00000000 --- a/src/linux/getopt_long.c +++ /dev/null @@ -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); -} diff --git a/src/linux/getpagesize.c b/src/linux/getpagesize.c deleted file mode 100644 index 5ede652b..00000000 --- a/src/linux/getpagesize.c +++ /dev/null @@ -1,7 +0,0 @@ -#include <unistd.h> -#include <limits.h> - -int getpagesize(void) -{ - return PAGE_SIZE; -} diff --git a/src/linux/getpass.c b/src/linux/getpass.c deleted file mode 100644 index d439a2a5..00000000 --- a/src/linux/getpass.c +++ /dev/null @@ -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; -} diff --git a/src/linux/initgroups.c b/src/linux/initgroups.c deleted file mode 100644 index 545d1859..00000000 --- a/src/linux/initgroups.c +++ /dev/null @@ -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); -} diff --git a/src/linux/inotify.c b/src/linux/inotify.c new file mode 100644 index 00000000..d3b4fa0b --- /dev/null +++ b/src/linux/inotify.c @@ -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); +} diff --git a/src/linux/inotify_add_watch.c b/src/linux/inotify_add_watch.c deleted file mode 100644 index 75f207d7..00000000 --- a/src/linux/inotify_add_watch.c +++ /dev/null @@ -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); -} diff --git a/src/linux/inotify_init.c b/src/linux/inotify_init.c deleted file mode 100644 index 05070846..00000000 --- a/src/linux/inotify_init.c +++ /dev/null @@ -1,7 +0,0 @@ -#include <sys/inotify.h> -#include "syscall.h" - -int inotify_init() -{ - return syscall(SYS_inotify_init); -} diff --git a/src/linux/inotify_init1.c b/src/linux/inotify_init1.c deleted file mode 100644 index 6472a7b2..00000000 --- a/src/linux/inotify_init1.c +++ /dev/null @@ -1,7 +0,0 @@ -#include <sys/inotify.h> -#include "syscall.h" - -int inotify_init1(int flags) -{ - return syscall(SYS_inotify_init1, flags); -} diff --git a/src/linux/inotify_rm_watch.c b/src/linux/inotify_rm_watch.c deleted file mode 100644 index cba597eb..00000000 --- a/src/linux/inotify_rm_watch.c +++ /dev/null @@ -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); -} diff --git a/src/linux/mntent.c b/src/linux/mntent.c deleted file mode 100644 index 3eafba5e..00000000 --- a/src/linux/mntent.c +++ /dev/null @@ -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); -} diff --git a/src/linux/mount.c b/src/linux/mount.c index 83a8db44..34e11af1 100644 --- a/src/linux/mount.c +++ b/src/linux/mount.c @@ -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); +} diff --git a/src/linux/prlimit.c b/src/linux/prlimit.c new file mode 100644 index 00000000..b9dbb446 --- /dev/null +++ b/src/linux/prlimit.c @@ -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); +} diff --git a/src/linux/ptrace.c b/src/linux/ptrace.c new file mode 100644 index 00000000..83b8022b --- /dev/null +++ b/src/linux/ptrace.c @@ -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; +} diff --git a/src/linux/swapon.c b/src/linux/swap.c index 2b40a30b..8137d51e 100644 --- a/src/linux/swapon.c +++ b/src/linux/swap.c @@ -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); +} diff --git a/src/linux/swapoff.c b/src/linux/swapoff.c deleted file mode 100644 index 9f95e82d..00000000 --- a/src/linux/swapoff.c +++ /dev/null @@ -1,7 +0,0 @@ -#include <sys/swap.h> -#include "syscall.h" - -int swapoff(const char *path) -{ - return syscall(SYS_swapoff, path); -} diff --git a/src/linux/syscall.c b/src/linux/syscall.c deleted file mode 100644 index 15355609..00000000 --- a/src/linux/syscall.c +++ /dev/null @@ -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)); -} diff --git a/src/linux/umount.c b/src/linux/umount.c deleted file mode 100644 index fb9b5e73..00000000 --- a/src/linux/umount.c +++ /dev/null @@ -1,7 +0,0 @@ -#include <sys/mount.h> -#include "syscall.h" - -int umount(const char *special) -{ - return syscall(SYS_umount2, special, 0); -} diff --git a/src/linux/umount2.c b/src/linux/umount2.c deleted file mode 100644 index 25ad057c..00000000 --- a/src/linux/umount2.c +++ /dev/null @@ -1,7 +0,0 @@ -#include <sys/mount.h> -#include "syscall.h" - -int umount2(const char *special, int flags) -{ - return syscall(SYS_umount2, special, flags); -} |