From 2990ce9b390ec1e2bfa1c043c406878e8aff86dd Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Thu, 21 Dec 2017 11:18:28 +0000 Subject: Big 2.6.3.0 reorganization - Add libposixplz, update headers - Add memmem and friends - Add textmessage to libunixonacid - Update some sysdeps tests --- src/libstddjb/byte_search.c | 11 +++++++ src/libstddjb/doublefork.c | 50 -------------------------------- src/libstddjb/env_get.c | 2 +- src/libstddjb/execvep.c | 39 ------------------------- src/libstddjb/getpeereid.c | 66 ------------------------------------------ src/libstddjb/pathexec.c | 2 +- src/libstddjb/pathexec_run.c | 1 + src/libstddjb/setgroups.c | 50 -------------------------------- src/libstddjb/siovec_search.c | 30 +++++++++++++++++++ src/libstddjb/skagetlnmaxsep.c | 29 +++++++++++++++++++ src/libstddjb/touch.c | 53 --------------------------------- src/libstddjb/xexecvep.c | 1 + 12 files changed, 74 insertions(+), 260 deletions(-) create mode 100644 src/libstddjb/byte_search.c delete mode 100644 src/libstddjb/doublefork.c delete mode 100644 src/libstddjb/execvep.c delete mode 100644 src/libstddjb/getpeereid.c delete mode 100644 src/libstddjb/setgroups.c create mode 100644 src/libstddjb/siovec_search.c create mode 100644 src/libstddjb/skagetlnmaxsep.c delete mode 100644 src/libstddjb/touch.c (limited to 'src/libstddjb') diff --git a/src/libstddjb/byte_search.c b/src/libstddjb/byte_search.c new file mode 100644 index 0000000..286d806 --- /dev/null +++ b/src/libstddjb/byte_search.c @@ -0,0 +1,11 @@ +/* ISC license. */ + +#include +#include +#include + +size_t byte_search (char const *haystack, size_t hlen, char const *needle, size_t nlen) +{ + char *p = memmem(haystack, hlen, needle, nlen) ; + return p ? p - haystack : hlen + 1 - nlen ; +} diff --git a/src/libstddjb/doublefork.c b/src/libstddjb/doublefork.c deleted file mode 100644 index da512f8..0000000 --- a/src/libstddjb/doublefork.c +++ /dev/null @@ -1,50 +0,0 @@ -/* ISC license. */ - -#include -#include -#include -#include -#include -#include - -pid_t doublefork () -{ - char pack[8] ; - int fd[2] ; - pid_t child ; - if (pipe(fd) == -1) return -1 ; - child = fork() ; - switch (child) - { - case -1: - { - fd_close(fd[1]) ; - fd_close(fd[0]) ; - return -1 ; - } - case 0: - { - pid_t pid ; - fd_close(fd[0]) ; - pid = fork() ; - switch (pid) - { - case -1: _exit(errno) ; - case 0: fd_close(fd[1]) ; return 0 ; - } - uint64_pack_big(pack, pid) ; - _exit((allwrite(fd[1], pack, 8) < 8) ? errno : 0) ; - } - } - fd_close(fd[1]) ; - { - uint64_t grandchild = 0 ; - int wstat ; - if (allread(fd[0], pack, 8) < 8) grandchild = 1 ; - fd_close(fd[0]) ; - wait_pid(child, &wstat) ; - if (grandchild) return (errno = WIFSIGNALED(wstat) ? EINTR : WEXITSTATUS(wstat), -1) ; - uint64_unpack_big(pack, &grandchild) ; - return (pid_t)grandchild ; - } -} diff --git a/src/libstddjb/env_get.c b/src/libstddjb/env_get.c index 927955f..f523f45 100644 --- a/src/libstddjb/env_get.c +++ b/src/libstddjb/env_get.c @@ -1,7 +1,7 @@ /* ISC license. */ #include -#include +#include char const *env_get (char const *s) { diff --git a/src/libstddjb/execvep.c b/src/libstddjb/execvep.c deleted file mode 100644 index 3dae8bc..0000000 --- a/src/libstddjb/execvep.c +++ /dev/null @@ -1,39 +0,0 @@ -/* ISC license. */ - -#include -#include -#include -#include -#include - -void execvep (char const *file, char const *const *argv, char const *const *envp, char const *path) -{ - if (!path) errno = EINVAL ; - else if (file[str_chr(file, '/')]) - execve(file, (char *const *)argv, (char *const *)envp) ; /* execve prototype sucks */ - else - { - size_t pathlen = strlen(path) + 1 ; - size_t filelen = strlen(file) ; - int savederrno = 0 ; - while (pathlen) - { - size_t split = byte_chr(path, pathlen - 1, ':') ; - if (split) - { - char tmp[split + 2 + filelen] ; - memcpy(tmp, path, split) ; - tmp[split] = '/' ; - memcpy(tmp + split + 1, file, filelen + 1) ; - execve(tmp, (char *const *)argv, (char *const *)envp) ; - if (errno != ENOENT) - { - savederrno = errno ; - if ((errno != EACCES) && (errno != EPERM) && (errno != EISDIR)) break ; - } - } - path += split+1 ; pathlen -= split+1 ; - } - if (savederrno) errno = savederrno ; - } -} diff --git a/src/libstddjb/getpeereid.c b/src/libstddjb/getpeereid.c deleted file mode 100644 index 0d36abb..0000000 --- a/src/libstddjb/getpeereid.c +++ /dev/null @@ -1,66 +0,0 @@ -/* ISC license. */ - -#include - -#ifdef SKALIBS_HASGETPEEREID -/* syscall exists - do nothing */ - -#else - -#ifdef SKALIBS_HASSOPEERCRED -/* implementation with SO_PEERCRED */ - -#include -#include -#include -#include - -int getpeereid (int s, uid_t *u, gid_t *g) -{ - struct ucred blah ; - socklen_t len = sizeof(blah) ; - - if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &blah, &len) == -1) - return -1 ; - *u = blah.uid ; - *g = blah.gid ; - return 0 ; -} - -#else - -#ifdef SKALIBS_HASGETPEERUCRED -/* implementation with getpeerucred() */ - -#include -#include -#include - -int getpeereid (int s, uid_t *u, gid_t *g) -{ - ucred_t *cred ; - if (getpeerucred(s, &cred) == -1) return -1 ; - *u = ucred_geteuid(cred) ; - *g = ucred_getegid(cred) ; - ucred_free(cred) ; - return 0 ; -} - -#else - -/* can't find a real implementation, make a stub */ - -#include -#include - -int getpeereid (int s, uid_t *uid, gid_t *gid) -{ - (void)s ; - *uid = *gid = -1 ; - errno = ENOSYS ; - return -1 ; -} - -#endif -#endif -#endif diff --git a/src/libstddjb/pathexec.c b/src/libstddjb/pathexec.c index b7b088e..da8788c 100644 --- a/src/libstddjb/pathexec.c +++ b/src/libstddjb/pathexec.c @@ -4,7 +4,7 @@ #include #include -#include +#include void pathexec (char const *const *argv) { diff --git a/src/libstddjb/pathexec_run.c b/src/libstddjb/pathexec_run.c index 4d9b291..ff3211d 100644 --- a/src/libstddjb/pathexec_run.c +++ b/src/libstddjb/pathexec_run.c @@ -2,6 +2,7 @@ #include #include +#include #include void pathexec_run (char const *file, char const *const *argv, char const *const *envp) diff --git a/src/libstddjb/setgroups.c b/src/libstddjb/setgroups.c deleted file mode 100644 index d064ed2..0000000 --- a/src/libstddjb/setgroups.c +++ /dev/null @@ -1,50 +0,0 @@ -/* ISC license. */ - -#include - -#ifdef SKALIBS_HASSETGROUPS - -#include -#include -#include -#include -#include - -int setgroups_and_gid (gid_t g, size_t n, gid_t const *tab) -{ - size_t i = 1 ; - if (!n) return setgroups(1, &g) ; - if (tab[0] == g) return setgroups(n, tab) ; - for (; i < n ; i++) if (tab[i] == g) break ; - if (i < n) - { - gid_t newtab[n] ; - newtab[0] = g ; - memcpy(newtab + 1, tab, i * sizeof(gid_t)) ; - memcpy(newtab + i + 1, tab + i + 1, (n - i - 1) * sizeof(gid_t)) ; - return setgroups(n, newtab) ; - } - else - { - gid_t newtab[n+1] ; - newtab[0] = g ; - memcpy(newtab + 1, tab, n * sizeof(gid_t)) ; - return setgroups(n+1, newtab) ; - } -} - -int setgroups_with_egid (size_t n, gid_t const *tab) -{ - return setgroups_and_gid(getegid(), n, tab) ; -} - -int skalibs_setgroups (size_t n, gid_t const *tab) -{ -#ifdef SKALIBS_BSD_SUCKS - return setgroups_with_egid(n, tab) ; -#else - return setgroups(n, tab) ; -#endif -} - -#endif diff --git a/src/libstddjb/siovec_search.c b/src/libstddjb/siovec_search.c new file mode 100644 index 0000000..ef64e96 --- /dev/null +++ b/src/libstddjb/siovec_search.c @@ -0,0 +1,30 @@ +/* ISC license. */ + +#include +#include +#include +#include + +size_t siovec_search (struct iovec const *v, unsigned int n, char const *needle, size_t nlen) +{ + size_t vlen = siovec_len(v, n) ; + size_t w = 0 ; + unsigned int i = 0 ; + for (; i < n ; i++) + { + char *p = memmem(v[i].iov_base, v[i].iov_len, needle, nlen) ; + if (p) return w + (p - (char *)v[i].iov_base) ; + if (i < n-1 && nlen > 1 && v[i].iov_len) + { + size_t prelen = v[i].iov_len < nlen ? v[i].iov_len : nlen ; + size_t postlen = vlen - w - v[i].iov_len < nlen ? vlen - w - v[i].iov_len : nlen ; + char buf[prelen + postlen - 2] ; + memcpy(buf, (char *)v[i].iov_base + v[i].iov_len - prelen + 1, prelen - 1) ; + siovec_gather(v + i + 1, n - 1 - i, buf + prelen - 1, postlen - 1) ; + p = memmem(buf, prelen + postlen - 2, needle, nlen) ; + if (p) return w + v[i].iov_len - prelen + 1 + (p - buf) ; + } + w += v[i].iov_len ; + } + return w ; +} diff --git a/src/libstddjb/skagetlnmaxsep.c b/src/libstddjb/skagetlnmaxsep.c new file mode 100644 index 0000000..7742c06 --- /dev/null +++ b/src/libstddjb/skagetlnmaxsep.c @@ -0,0 +1,29 @@ +/* ISC license. */ + +#include +#include +#include +#include +#include +#include + +int skagetlnmaxsep (buffer *b, stralloc *sa, size_t max, char const *sep, size_t seplen) +{ + size_t start = sa->len ; + for (;;) + { + struct iovec v[2] ; + size_t pos ; + int r ; + buffer_rpeek(b, v) ; + pos = siovec_bytein(v, 2, sep, seplen) ; + r = pos < buffer_len(b) ; pos += r ; + if (!stralloc_readyplus(sa, pos)) return -1 ; + buffer_getnofill(b, sa->s + sa->len, pos) ; sa->len += pos ; + if (r) return 1 ; + if (sa->len - start >= max) return (errno = EMSGSIZE, -1) ; + r = buffer_fill(b) ; + if (r < 0) return r ; + if (!r) return (sa->s && (sa->len > start)) ? (errno = EPIPE, -1) : 0 ; + } +} diff --git a/src/libstddjb/touch.c b/src/libstddjb/touch.c deleted file mode 100644 index dc0c3cf..0000000 --- a/src/libstddjb/touch.c +++ /dev/null @@ -1,53 +0,0 @@ -/* ISC license. */ - -#include - -#ifdef SKALIBS_HASFUTIMENS - -#include -#include -#include -#include - -int touch (char const *file) -{ - int r ; - int fd = open_create(file) ; - if (fd < 0) return 0 ; - r = futimens(fd, 0) >= 0 ; - fd_close(fd) ; - return r ; -} - -#else -#ifdef SKALIBS_HASFUTIMES - -#include -#include -#include - -int touch (char const *file) -{ - int r ; - int fd = open_create(file) ; - if (fd < 0) return 0 ; - r = futimes(fd, 0) >= 0 ; - fd_close(fd) ; - return r ; -} - -#else - -#include -#include - -int touch (char const *file) -{ - int fd = open_create(file) ; - if (fd < 0) return 0 ; - fd_close(fd) ; - return utimes(file, 0) >= 0 ; -} - -#endif -#endif diff --git a/src/libstddjb/xexecvep.c b/src/libstddjb/xexecvep.c index 294f959..17a3abf 100644 --- a/src/libstddjb/xexecvep.c +++ b/src/libstddjb/xexecvep.c @@ -1,6 +1,7 @@ /* ISC license. */ #include +#include #include #include -- cgit v1.2.3