diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2017-12-21 11:18:28 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2017-12-21 11:18:28 +0000 |
commit | 2990ce9b390ec1e2bfa1c043c406878e8aff86dd (patch) | |
tree | 270e5e2bfa3cdb2fc795039fc45901d93290567e /src/libstddjb | |
parent | 933e986a9207d2b61c5119e18603b44b924e7226 (diff) | |
download | skalibs-2990ce9b390ec1e2bfa1c043c406878e8aff86dd.tar.xz |
Big 2.6.3.0 reorganization
- Add libposixplz, update headers
- Add memmem and friends
- Add textmessage to libunixonacid
- Update some sysdeps tests
Diffstat (limited to 'src/libstddjb')
-rw-r--r-- | src/libstddjb/byte_search.c | 11 | ||||
-rw-r--r-- | src/libstddjb/doublefork.c | 50 | ||||
-rw-r--r-- | src/libstddjb/env_get.c | 2 | ||||
-rw-r--r-- | src/libstddjb/execvep.c | 39 | ||||
-rw-r--r-- | src/libstddjb/getpeereid.c | 66 | ||||
-rw-r--r-- | src/libstddjb/pathexec.c | 2 | ||||
-rw-r--r-- | src/libstddjb/pathexec_run.c | 1 | ||||
-rw-r--r-- | src/libstddjb/setgroups.c | 50 | ||||
-rw-r--r-- | src/libstddjb/siovec_search.c | 30 | ||||
-rw-r--r-- | src/libstddjb/skagetlnmaxsep.c | 29 | ||||
-rw-r--r-- | src/libstddjb/touch.c | 53 | ||||
-rw-r--r-- | src/libstddjb/xexecvep.c | 1 |
12 files changed, 74 insertions, 260 deletions
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 <string.h> +#include <skalibs/posixplz.h> +#include <skalibs/bytestr.h> + +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 <sys/wait.h> -#include <unistd.h> -#include <errno.h> -#include <skalibs/uint64.h> -#include <skalibs/allreadwrite.h> -#include <skalibs/djbunix.h> - -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 <skalibs/env.h> -#include <skalibs/environ.h> +#include <skalibs/posixplz.h> 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 <unistd.h> -#include <string.h> -#include <errno.h> -#include <skalibs/bytestr.h> -#include <skalibs/djbunix.h> - -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 <skalibs/sysdeps.h> - -#ifdef SKALIBS_HASGETPEEREID -/* syscall exists - do nothing */ - -#else - -#ifdef SKALIBS_HASSOPEERCRED -/* implementation with SO_PEERCRED */ - -#include <skalibs/nonposix.h> -#include <sys/socket.h> -#include <sys/un.h> -#include <skalibs/getpeereid.h> - -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 <skalibs/nonposix.h> -#include <ucred.h> -#include <skalibs/getpeereid.h> - -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 <errno.h> -#include <skalibs/getpeereid.h> - -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 <skalibs/env.h> #include <skalibs/djbunix.h> -#include <skalibs/environ.h> +#include <skalibs/posixplz.h> 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 <skalibs/config.h> #include <skalibs/env.h> +#include <skalibs/posixplz.h> #include <skalibs/djbunix.h> 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 <skalibs/sysdeps.h> - -#ifdef SKALIBS_HASSETGROUPS - -#include <skalibs/nonposix.h> -#include <string.h> -#include <unistd.h> -#include <grp.h> -#include <skalibs/setgroups.h> - -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 <string.h> +#include <sys/uio.h> +#include <skalibs/posixplz.h> +#include <skalibs/siovec.h> + +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 <sys/uio.h> +#include <errno.h> +#include <skalibs/buffer.h> +#include <skalibs/siovec.h> +#include <skalibs/stralloc.h> +#include <skalibs/skamisc.h> + +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 <skalibs/sysdeps.h> - -#ifdef SKALIBS_HASFUTIMENS - -#include <skalibs/nonposix.h> -#include <time.h> -#include <sys/stat.h> -#include <skalibs/djbunix.h> - -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 <skalibs/nonposix.h> -#include <sys/time.h> -#include <skalibs/djbunix.h> - -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 <sys/time.h> -#include <skalibs/djbunix.h> - -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 <errno.h> +#include <skalibs/posixplz.h> #include <skalibs/djbunix.h> #include <skalibs/strerr2.h> |