commit b97b7916ff74bb1a757e4d07f9170bc38e945354
parent b4c9b934074f88394bb9c8f76ccb579b6277f773
Author: xplshn <anto@xplshn.com.ar>
Date: Thu, 19 Nov 2026 11:21:56 -0300
tar: fix
Signed-off-by: xplshn <anto@xplshn.com.ar>
Diffstat:
22 files changed, 301 insertions(+), 194 deletions(-)
diff --git a/GNUmakefile b/GNUmakefile
@@ -13,6 +13,8 @@ NINQU_LIB_SRC =\
shared/libutil/eprintf.c\
shared/libutil/strlcpy.c\
shared/libutil/strlcat.c\
+ shared/libutil/estrlcpy.c\
+ shared/libutil/estrlcat.c\
shared/libutil/strtonum.c\
shared/libutil/fshut.c\
shared/libutil/mkdirp.c\
diff --git a/Makefile b/Makefile
@@ -17,6 +17,8 @@ NINQU_LIB_SRC =\
shared/libutil/eprintf.c\
shared/libutil/strlcpy.c\
shared/libutil/strlcat.c\
+ shared/libutil/estrlcpy.c\
+ shared/libutil/estrlcat.c\
shared/libutil/strtonum.c\
shared/libutil/fshut.c\
shared/libutil/mkdirp.c\
diff --git a/cmd/posix/mkdir.c b/cmd/posix/mkdir.c
@@ -45,8 +45,10 @@ main(int argc, char *argv[])
for (; *argv; argc--, argv++) {
if (pflag) {
- if (mkdirp(*argv, mode, 0777 & (~mask | 0300)) < 0)
+ if (mkdirp(*argv, mode, 0777 & (~mask | 0300)) < 0) {
+ weprintf("mkdir %s:", *argv);
ret = 1;
+ }
} else if (mkdir(*argv, mode) < 0) {
weprintf("mkdir %s:", *argv);
ret = 1;
diff --git a/cmd/pseudo/free.c b/cmd/pseudo/free.c
@@ -57,18 +57,20 @@ main(int argc, char *argv[])
if (get_meminfo(&mi) < 0)
eprintf("get_meminfo:");
- printf(" %13s%13s%13s%13s%13s\n", "total", "used", "free", "shared", "buffers");
+ printf(" %13s%13s%13s%13s%13s%13s\n", "total", "used", "free", "shared", "buffers", "cached");
printf("Mem: ");
printf(
- "%13llu%13llu%13llu%13llu%13llu\n",
+ "%13llu%13llu%13llu%13llu%13llu%13llu\n",
scale(mi.total),
scale(mi.total - mi.free),
scale(mi.free),
scale(mi.shared),
- scale(mi.buffers)
+ scale(mi.buffers),
+ scale(mi.cached)
);
+ /* what programs hold, and what they could still have */
printf("-/+ buffers/cache:");
- printf("%13llu%13llu\n", scale(mi.total - mi.free - mi.buffers), scale(mi.free + mi.buffers));
+ printf("%13llu%13llu\n", scale(mi.total - mi.available), scale(mi.available));
printf("Swap:");
printf(
"%13llu%13llu%13llu\n",
diff --git a/cmd/pseudo/tar.c b/cmd/pseudo/tar.c
@@ -317,19 +317,27 @@ decomp(int fd, const char *tool, const char *flags)
return fds[0];
}
+/* fills buf unless the input ends first: a pipe from a decompressor hands
+ * back whatever it has, and a short block would put every later header
+ * at the wrong offset */
static ssize_t
eread(int fd, void *buf, size_t n)
{
+ size_t got = 0;
ssize_t r;
-again:
- r = read(fd, buf, n);
- if (r < 0) {
- if (errno == EINTR)
- goto again;
- eprintf("read:");
+ while (got < n) {
+ r = read(fd, (char *)buf + got, n - got);
+ if (r < 0) {
+ if (errno == EINTR)
+ continue;
+ eprintf("read:");
+ }
+ if (r == 0)
+ break;
+ got += (size_t)r;
}
- return r;
+ return (ssize_t)got;
}
static ssize_t
diff --git a/ninqu.rules b/ninqu.rules
@@ -136,8 +136,11 @@
(rule $(NAME)
(glob "cmd/$(CATEGORY)/*.c")
(gate build_$(CATEGORY)_$(IDENT))
- (dep $(MATCH) FEATURES_H LIBUTIL LIBUTF LIBBLAKE3)
- (after $(EXTRA_AFTER))
+ ; compiled and linked in one step, so the header that every compile
+ ; -includes is order-only: as a dep it would land in $(IN) and be
+ ; passed to the linker. its path still makes a change rebuild
+ (dep $(MATCH) LIBUTIL LIBUTF LIBBLAKE3)
+ (after "scripts/mk/features.h" $(EXTRA_AFTER))
(out $(STEM))
(description $(DESC))
(feature perfile)
diff --git a/shared/fs.h b/shared/fs.h
@@ -18,6 +18,7 @@ struct recursor {
int maxdepth;
int follow;
int flags;
+ int err; /* errno of the first failure */
};
enum {
@@ -26,6 +27,9 @@ enum {
SILENT = 1 << 2,
CONFIRM = 1 << 3,
IGNORE = 1 << 4,
+ /* for a library: nothing printed and nothing asked, failures are only
+ * counted and the first one kept in err */
+ QUIET = 1 << 5,
};
extern int cp_aflag;
@@ -34,8 +38,10 @@ extern int cp_iflag;
extern int cp_pflag;
extern int cp_rflag;
extern int cp_vflag;
+extern int cp_qflag;
extern int cp_follow;
extern int cp_status;
+extern int cp_errno;
extern int rm_status;
diff --git a/shared/libutil/concat.c b/shared/libutil/concat.c
@@ -6,18 +6,13 @@
int
concat(int f1, const char *s1, int f2, const char *s2)
{
- char buf[BUFSIZ];
- ssize_t n;
-
- while ((n = read(f1, buf, sizeof(buf))) > 0) {
- if (writeall(f2, buf, n) < 0) {
+ switch (copyall(f1, f2)) {
+ case -1:
+ weprintf("read %s:", s1);
+ return -1;
+ case -2:
weprintf("write %s:", s2);
return -2;
- }
- }
- if (n < 0) {
- weprintf("read %s:", s1);
- return -1;
}
return 0;
}
diff --git a/shared/libutil/copyall.c b/shared/libutil/copyall.c
@@ -0,0 +1,19 @@
+/* see LICENSE file for copyright and license details */
+#include <stdio.h>
+#include <unistd.h>
+
+#include "../util.h"
+
+/* -1 when reading failed and -2 when writing did, errno says why */
+int
+copyall(int f1, int f2)
+{
+ char buf[BUFSIZ];
+ ssize_t n;
+
+ while ((n = read(f1, buf, sizeof(buf))) > 0) {
+ if (writeall(f2, buf, n) < 0)
+ return -2;
+ }
+ return n < 0 ? -1 : 0;
+}
diff --git a/shared/libutil/cp.c b/shared/libutil/cp.c
@@ -8,6 +8,7 @@
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -25,16 +26,51 @@ int cp_iflag = 0;
int cp_pflag = 0;
int cp_rflag = 0;
int cp_vflag = 0;
+int cp_qflag = 0;
int cp_status = 0;
+int cp_errno = 0;
int cp_follow;
+/* a failure: counted, the first one kept in cp_errno, printed unless the
+ * caller is a library that asked for quiet */
+static int
+cp_fail(const char *fmt, ...)
+{
+ va_list ap;
+ int err = errno;
+
+ if (!cp_status)
+ cp_errno = err;
+ cp_status = 1;
+ if (!cp_qflag) {
+ va_start(ap, fmt);
+ errno = err;
+ xvprintf(fmt, ap);
+ va_end(ap);
+ }
+ errno = err;
+ return 0;
+}
+
+/* dir/name into buf, -1 when it does not fit */
+static int
+cp_join(char *buf, size_t n, const char *dir, const char *name)
+{
+ if (strlcpy(buf, dir, n) >= n || (dir[strlen(dir) - 1] != '/' && strlcat(buf, "/", n) >= n)
+ || strlcat(buf, name, n) >= n) {
+ errno = ENAMETOOLONG;
+ return -1;
+ }
+ return 0;
+}
+
int
cp(const char *s1, const char *s2, int depth)
{
DIR *dp;
int f1, f2, flags = 0;
struct dirent *d;
- struct stat st;
+ struct stat st, st2;
struct timespec times[2];
ssize_t r;
char target[PATH_MAX], ns1[PATH_MAX], ns2[PATH_MAX];
@@ -42,47 +78,38 @@ cp(const char *s1, const char *s2, int depth)
if (cp_follow == 'P' || (cp_follow == 'H' && depth))
flags |= AT_SYMLINK_NOFOLLOW;
- if (fstatat(AT_FDCWD, s1, &st, flags) < 0) {
- weprintf("stat %s:", s1);
- cp_status = 1;
- return 0;
+ if (fstatat(AT_FDCWD, s1, &st, flags) < 0)
+ return cp_fail("stat %s:", s1);
+ if (stat(s2, &st2) == 0 && st.st_dev == st2.st_dev && st.st_ino == st2.st_ino) {
+ errno = EINVAL;
+ return cp_fail("%s -> %s: same file\n", s1, s2);
}
- if (cp_iflag && access(s2, F_OK) == 0) {
+ if (cp_iflag && !cp_qflag && access(s2, F_OK) == 0) {
if (!confirm("overwrite '%s'? ", s2))
return 0;
}
- if (cp_vflag)
+ if (cp_vflag && !cp_qflag)
printf("%s -> %s\n", s1, s2);
if (S_ISLNK(st.st_mode)) {
if ((r = readlink(s1, target, sizeof(target) - 1)) >= 0) {
target[r] = '\0';
- if (cp_fflag && unlink(s2) < 0 && errno != ENOENT) {
- weprintf("unlink %s:", s2);
- cp_status = 1;
- return 0;
- } else if (symlink(target, s2) < 0) {
- weprintf("symlink %s -> %s:", s2, target);
- cp_status = 1;
- return 0;
- }
+ if (cp_fflag && unlink(s2) < 0 && errno != ENOENT)
+ return cp_fail("unlink %s:", s2);
+ else if (symlink(target, s2) < 0)
+ return cp_fail("symlink %s -> %s:", s2, target);
}
} else if (S_ISDIR(st.st_mode)) {
if (!cp_rflag) {
- weprintf("%s is a directory\n", s1);
- cp_status = 1;
- return 0;
- }
- if (!(dp = opendir(s1))) {
- weprintf("opendir %s:", s1);
- cp_status = 1;
- return 0;
+ errno = EISDIR;
+ return cp_fail("%s is a directory\n", s1);
}
+ if (!(dp = opendir(s1)))
+ return cp_fail("opendir %s:", s1);
if (mkdir(s2, st.st_mode) < 0 && errno != EEXIST) {
- weprintf("mkdir %s:", s2);
- cp_status = 1;
+ cp_fail("mkdir %s:", s2);
closedir(dp);
return 0;
}
@@ -90,18 +117,11 @@ cp(const char *s1, const char *s2, int depth)
while ((d = readdir(dp))) {
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
-
- estrlcpy(ns1, s1, sizeof(ns1));
- if (s1[strlen(s1) - 1] != '/')
- estrlcat(ns1, "/", sizeof(ns1));
- estrlcat(ns1, d->d_name, sizeof(ns1));
-
- estrlcpy(ns2, s2, sizeof(ns2));
- if (s2[strlen(s2) - 1] != '/')
- estrlcat(ns2, "/", sizeof(ns2));
- estrlcat(ns2, d->d_name, sizeof(ns2));
-
- fnck(ns1, ns2, cp, depth + 1);
+ if (cp_join(ns1, sizeof(ns1), s1, d->d_name) < 0
+ || cp_join(ns2, sizeof(ns2), s2, d->d_name) < 0)
+ cp_fail("%s/%s:", s1, d->d_name);
+ else
+ cp(ns1, ns2, depth + 1);
}
closedir(dp);
@@ -110,73 +130,54 @@ cp(const char *s1, const char *s2, int depth)
&& (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) || S_ISSOCK(st.st_mode)
|| S_ISFIFO(st.st_mode))
) {
- if (cp_fflag && unlink(s2) < 0 && errno != ENOENT) {
- weprintf("unlink %s:", s2);
- cp_status = 1;
- return 0;
- } else if (mknod(s2, st.st_mode, st.st_rdev) < 0) {
- weprintf("mknod %s:", s2);
- cp_status = 1;
- return 0;
- }
+ if (cp_fflag && unlink(s2) < 0 && errno != ENOENT)
+ return cp_fail("unlink %s:", s2);
+ else if (mknod(s2, st.st_mode, st.st_rdev) < 0)
+ return cp_fail("mknod %s:", s2);
} else {
- if ((f1 = open(s1, O_RDONLY)) < 0) {
- weprintf("open %s:", s1);
- cp_status = 1;
- return 0;
- }
+ if ((f1 = open(s1, O_RDONLY)) < 0)
+ return cp_fail("open %s:", s1);
if ((f2 = creat(s2, st.st_mode)) < 0 && cp_fflag) {
if (unlink(s2) < 0 && errno != ENOENT) {
- weprintf("unlink %s:", s2);
- cp_status = 1;
+ cp_fail("unlink %s:", s2);
close(f1);
return 0;
}
f2 = creat(s2, st.st_mode);
}
if (f2 < 0) {
- weprintf("creat %s:", s2);
- cp_status = 1;
+ cp_fail("creat %s:", s2);
close(f1);
return 0;
}
- if (concat(f1, s1, f2, s2) < 0) {
- cp_status = 1;
- close(f1);
- close(f2);
- return 0;
- }
+ if ((r = copyall(f1, f2)) == -1)
+ cp_fail("read %s:", s1);
+ else if (r == -2)
+ cp_fail("write %s:", s2);
close(f1);
close(f2);
+ if (r < 0)
+ return 0;
}
if (cp_aflag || cp_pflag) {
/* atime and mtime */
times[0] = st.st_atim;
times[1] = st.st_mtim;
- if (utimensat(AT_FDCWD, s2, times, AT_SYMLINK_NOFOLLOW) < 0) {
- weprintf("utimensat %s:", s2);
- cp_status = 1;
- }
+ if (utimensat(AT_FDCWD, s2, times, AT_SYMLINK_NOFOLLOW) < 0)
+ cp_fail("utimensat %s:", s2);
/* owner and mode */
if (!S_ISLNK(st.st_mode)) {
if (chown(s2, st.st_uid, st.st_gid) < 0) {
- weprintf("chown %s:", s2);
- cp_status = 1;
+ cp_fail("chown %s:", s2);
st.st_mode &= ~(S_ISUID | S_ISGID);
}
- if (chmod(s2, st.st_mode) < 0) {
- weprintf("chmod %s:", s2);
- cp_status = 1;
- }
- } else {
- if (lchown(s2, st.st_uid, st.st_gid) < 0) {
- weprintf("lchown %s:", s2);
- cp_status = 1;
- return 0;
- }
+ if (chmod(s2, st.st_mode) < 0)
+ cp_fail("chmod %s:", s2);
+ } else if (lchown(s2, st.st_uid, st.st_gid) < 0) {
+ return cp_fail("lchown %s:", s2);
}
}
diff --git a/shared/libutil/estrlcat.c b/shared/libutil/estrlcat.c
@@ -0,0 +1,15 @@
+/* see LICENSE file for copyright and license details */
+#include <sys/types.h>
+
+#include "../util.h"
+
+size_t
+estrlcat(char *dst, const char *src, size_t siz)
+{
+ size_t ret;
+
+ if ((ret = strlcat(dst, src, siz)) >= siz)
+ eprintf("strlcat: input string too long\n");
+
+ return ret;
+}
diff --git a/shared/libutil/estrlcpy.c b/shared/libutil/estrlcpy.c
@@ -0,0 +1,15 @@
+/* see LICENSE file for copyright and license details */
+#include <sys/types.h>
+
+#include "../util.h"
+
+size_t
+estrlcpy(char *dst, const char *src, size_t siz)
+{
+ size_t ret;
+
+ if ((ret = strlcpy(dst, src, siz)) >= siz)
+ eprintf("strlcpy: input string too long\n");
+
+ return ret;
+}
diff --git a/shared/libutil/mkdirp.c b/shared/libutil/mkdirp.c
@@ -16,24 +16,22 @@ mkdirp(const char *path, mode_t mode, mode_t pmode)
if (S_ISDIR(st.st_mode))
return 0;
errno = ENOTDIR;
- weprintf("%s:", path);
return -1;
}
- estrlcpy(tmp, path, sizeof(tmp));
+ if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp)) {
+ errno = ENAMETOOLONG;
+ return -1;
+ }
for (p = tmp + (tmp[0] == '/'); *p; p++) {
if (*p != '/')
continue;
*p = '\0';
- if (mkdir(tmp, pmode) < 0 && errno != EEXIST) {
- weprintf("mkdir %s:", tmp);
+ if (mkdir(tmp, pmode) < 0 && errno != EEXIST)
return -1;
- }
*p = '/';
}
- if (mkdir(tmp, mode) < 0 && errno != EEXIST) {
- weprintf("mkdir %s:", tmp);
+ if (mkdir(tmp, mode) < 0 && errno != EEXIST)
return -1;
- }
return 0;
}
diff --git a/shared/libutil/proc.c b/shared/libutil/proc.c
@@ -25,38 +25,42 @@ parsecmdline(pid_t pid, char *buf, size_t siz)
if (fd < 0)
return -1;
n = read(fd, buf, siz > 0 ? siz - 1 : 0);
- if (n < 0) {
- weprintf("read %s:", path);
- close(fd);
- return -1;
- }
- if (!n) {
- close(fd);
+ close(fd);
+ if (n <= 0)
return -1;
- }
buf[n] = '\0';
for (i = 0; i < n; i++)
if (buf[i] == '\0')
buf[i] = ' ';
- close(fd);
return 0;
}
int
parsestat(pid_t pid, struct procstat *ps)
{
- char path[PATH_MAX];
+ char path[PATH_MAX], line[BUFSIZ], *lp, *rp;
FILE *fp;
size_t len;
+ int n;
snprintf(path, sizeof(path), ARUU_LINUX_PATH_PROC "/%d/stat", pid);
if (!(fp = fopen(path, "r")))
return -1;
- fscanf(
- fp,
- "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu",
- &ps->pid,
- ps->comm,
+ n = fgets(line, sizeof(line), fp) != NULL;
+ fclose(fp);
+ /* comm is whatever the process named itself, spaces and parens included,
+ * so it runs from the first ( to the last ) */
+ if (!n || !(lp = strchr(line, '(')) || !(rp = strrchr(line, ')')) || rp < lp) {
+ errno = EINVAL;
+ return -1;
+ }
+ len = MIN((size_t)(rp - lp - 1), sizeof(ps->comm) - 1);
+ memcpy(ps->comm, lp + 1, len);
+ ps->comm[len] = '\0';
+ ps->pid = pid;
+ n = sscanf(
+ rp + 1,
+ " %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %llu %lu %ld %lu",
&ps->state,
&ps->ppid,
&ps->pgrp,
@@ -69,11 +73,7 @@ parsestat(pid_t pid, struct procstat *ps)
&ps->majflt,
&ps->cmajflt,
&ps->utime,
- &ps->stime
- );
- fscanf(
- fp,
- "%ld %ld %ld %ld %ld %ld %llu %lu %ld %ld",
+ &ps->stime,
&ps->cutime,
&ps->cstime,
&ps->priority,
@@ -85,12 +85,10 @@ parsestat(pid_t pid, struct procstat *ps)
&ps->rss,
&ps->rsslim
);
- /* filter out '(' and ')' from comm */
- if ((len = strlen(ps->comm)) > 0)
- len--;
- ps->comm[len] = '\0';
- memmove(ps->comm, ps->comm + 1, len);
- fclose(fp);
+ if (n != 23) {
+ errno = EINVAL;
+ return -1;
+ }
return 0;
}
@@ -107,14 +105,10 @@ parsestatus(pid_t pid, struct procstatus *pstatus)
if (fd < 0)
return -1;
n = read(fd, buf, sizeof(buf) - 1);
- if (n < 0)
- eprintf("%s: read error:", path);
- if (!n) {
- close(fd);
+ close(fd);
+ if (n <= 0)
return -1;
- }
buf[n] = '\0';
- close(fd);
off = strstr(buf, "Uid:");
if (!off)
return -1;
diff --git a/shared/libutil/recurse.c b/shared/libutil/recurse.c
@@ -15,6 +15,20 @@
int recurse_status = 0;
+/* a failure of the walk at r->path: SILENT ones are ignored, QUIET ones
+ * only counted */
+static void
+recurse_fail(struct recursor *r, const char *what)
+{
+ if (r->flags & SILENT)
+ return;
+ if (!r->err)
+ r->err = errno;
+ if (!(r->flags & QUIET))
+ weprintf("%s %s:", what, r->path);
+ recurse_status = 1;
+}
+
void
recurse(int dirfd, const char *name, void *data, struct recursor *r)
{
@@ -25,17 +39,17 @@ recurse(int dirfd, const char *name, void *data, struct recursor *r)
int flags = 0, fd;
size_t pathlen = r->pathlen;
- if (dirfd == AT_FDCWD)
- pathlen = estrlcpy(r->path, name, sizeof(r->path));
+ if (dirfd == AT_FDCWD && (pathlen = strlcpy(r->path, name, sizeof(r->path))) >= sizeof(r->path)) {
+ errno = ENAMETOOLONG;
+ recurse_fail(r, "path");
+ return;
+ }
if (r->follow == 'P' || (r->follow == 'H' && r->depth))
flags |= AT_SYMLINK_NOFOLLOW;
if (fstatat(dirfd, name, &st, flags) < 0) {
- if (!(r->flags & SILENT)) {
- weprintf("stat %s:", r->path);
- recurse_status = 1;
- }
+ recurse_fail(r, "stat");
return;
}
if (!S_ISDIR(st.st_mode)) {
@@ -43,7 +57,10 @@ recurse(int dirfd, const char *name, void *data, struct recursor *r)
return;
}
- new = emalloc(sizeof(struct history));
+ if (!(new = malloc(sizeof(struct history)))) {
+ recurse_fail(r, "malloc");
+ return;
+ }
new->prev = r->hist;
r->hist = new;
new->dev = st.st_dev;
@@ -57,16 +74,13 @@ recurse(int dirfd, const char *name, void *data, struct recursor *r)
r->fn(dirfd, name, &st, data, r);
if (!r->maxdepth || r->depth + 1 < r->maxdepth) {
- fd = openat(dirfd, name, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
- if (fd < 0) {
- weprintf("open %s:", r->path);
- recurse_status = 1;
+ if ((fd = openat(dirfd, name, O_RDONLY | O_CLOEXEC | O_DIRECTORY)) < 0) {
+ recurse_fail(r, "open");
+ return;
}
if (!(dp = fdopendir(fd))) {
- if (!(r->flags & SILENT)) {
- weprintf("fdopendir:");
- recurse_status = 1;
- }
+ recurse_fail(r, "fdopendir");
+ close(fd);
return;
}
if (r->path[pathlen - 1] != '/')
@@ -76,12 +90,12 @@ recurse(int dirfd, const char *name, void *data, struct recursor *r)
while ((d = readdir(dp))) {
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
- r->pathlen = pathlen + estrlcpy(r->path + pathlen, d->d_name, sizeof(r->path) - pathlen);
- if (fstatat(fd, d->d_name, &dst, flags) < 0) {
- if (!(r->flags & SILENT)) {
- weprintf("stat %s:", r->path);
- recurse_status = 1;
- }
+ r->pathlen = pathlen + strlcpy(r->path + pathlen, d->d_name, sizeof(r->path) - pathlen);
+ if (r->pathlen >= sizeof(r->path)) {
+ errno = ENAMETOOLONG;
+ recurse_fail(r, "path");
+ } else if (fstatat(fd, d->d_name, &dst, flags) < 0) {
+ recurse_fail(r, "stat");
} else if ((r->flags & SAMEDEV) && dst.st_dev != st.st_dev) {
continue;
} else {
diff --git a/shared/libutil/rm.c b/shared/libutil/rm.c
@@ -29,7 +29,7 @@ rm(int dirfd, const char *name, struct stat *st, void *data, struct recursor *r)
goto err;
}
- if (!quiet && ((!write && isatty(0)) || ask)) {
+ if (!quiet && !(r->flags & QUIET) && ((!write && isatty(0)) || ask)) {
if (!confirm("remove file '%s'? ", r->path))
return;
}
@@ -45,7 +45,10 @@ rm(int dirfd, const char *name, struct stat *st, void *data, struct recursor *r)
err:
if (!ignore) {
- weprintf("cannot remove '%s':", r->path);
+ if (!r->err)
+ r->err = errno;
+ if (!(r->flags & QUIET))
+ weprintf("cannot remove '%s':", r->path);
rm_status = 1;
}
}
diff --git a/shared/libutil/strlcat.c b/shared/libutil/strlcat.c
@@ -50,14 +50,3 @@ strlcat(char *dst, const char *src, size_t siz)
*d = '\0';
return (dlen + (s - src)); /* count does not include NUL */
}
-
-size_t
-estrlcat(char *dst, const char *src, size_t siz)
-{
- size_t ret;
-
- if ((ret = strlcat(dst, src, siz)) >= siz)
- eprintf("strlcat: input string too long\n");
-
- return ret;
-}
diff --git a/shared/libutil/strlcpy.c b/shared/libutil/strlcpy.c
@@ -46,14 +46,3 @@ strlcpy(char *dst, const char *src, size_t siz)
}
return (s - src - 1); /* count does not include NUL */
}
-
-size_t
-estrlcpy(char *dst, const char *src, size_t siz)
-{
- size_t ret;
-
- if ((ret = strlcpy(dst, src, siz)) >= siz)
- eprintf("strlcpy: input string too long\n");
-
- return ret;
-}
diff --git a/shared/libutil/sysinfo_linux.c b/shared/libutil/sysinfo_linux.c
@@ -4,8 +4,12 @@
#endif
#endif
#if defined(__linux__)
+#include "../paths.h"
#include "../sysinfo.h"
+#include "../util.h"
+#include <stddef.h>
+#include <stdio.h>
#include <string.h>
#include <sys/klog.h>
#include <sys/sysinfo.h>
@@ -21,26 +25,70 @@ get_uptime(long *uptime_secs)
return 0;
}
-int
-get_meminfo(struct MemInfo *mi)
+/* the fields of meminfo free(1) shows, in kB there */
+static const struct {
+ const char *name;
+ size_t off;
+} meminfo_fields[] = {
+ {"MemTotal:", offsetof(struct MemInfo, total) },
+ {"MemFree:", offsetof(struct MemInfo, free) },
+ {"MemAvailable:", offsetof(struct MemInfo, available)},
+ {"Shmem:", offsetof(struct MemInfo, shared) },
+ {"Buffers:", offsetof(struct MemInfo, buffers) },
+ {"Cached:", offsetof(struct MemInfo, cached) },
+ {"SReclaimable:", offsetof(struct MemInfo, cached) },
+ {"SwapTotal:", offsetof(struct MemInfo, totalswap)},
+ {"SwapFree:", offsetof(struct MemInfo, freeswap) },
+};
+
+/* sysinfo(2) knows nothing of the page cache, so this is the fallback */
+static int
+meminfo_sysinfo(struct MemInfo *mi)
{
struct sysinfo info;
unsigned long long unit;
- memset(mi, 0, sizeof(*mi));
if (sysinfo(&info) < 0)
return -1;
unit = info.mem_unit ? info.mem_unit : 1;
mi->total = info.totalram * unit;
mi->free = info.freeram * unit;
+ mi->available = (info.freeram + info.bufferram) * unit;
mi->shared = info.sharedram * unit;
mi->buffers = info.bufferram * unit;
- mi->cached = 0;
mi->totalswap = info.totalswap * unit;
mi->freeswap = info.freeswap * unit;
return 0;
}
+int
+get_meminfo(struct MemInfo *mi)
+{
+ FILE *fp;
+ char line[128];
+ unsigned long long kb;
+ size_t i, len;
+ int avail = 0;
+
+ memset(mi, 0, sizeof(*mi));
+ if (!(fp = fopen(ARUU_LINUX_PATH_PROC_MEMINFO, "r")))
+ return meminfo_sysinfo(mi);
+ while (fgets(line, sizeof(line), fp)) {
+ for (i = 0; i < LEN(meminfo_fields); i++) {
+ len = strlen(meminfo_fields[i].name);
+ if (strncmp(line, meminfo_fields[i].name, len) || sscanf(line + len, "%llu", &kb) != 1)
+ continue;
+ *(unsigned long long *)((char *)mi + meminfo_fields[i].off) += kb * 1024;
+ avail |= !strcmp(meminfo_fields[i].name, "MemAvailable:");
+ }
+ }
+ fclose(fp);
+ /* kernels before 3.14 have no MemAvailable */
+ if (!avail)
+ mi->available = mi->free + mi->buffers + mi->cached;
+ return 0;
+}
+
ssize_t
get_dmesg(char *buf, size_t len)
{
diff --git a/shared/proc.h b/shared/proc.h
@@ -24,7 +24,7 @@ struct procstat {
unsigned long long starttime;
unsigned long vsize;
long rss;
- long rsslim;
+ unsigned long rsslim;
};
struct procstatus {
diff --git a/shared/sysinfo.h b/shared/sysinfo.h
@@ -6,10 +6,11 @@
struct MemInfo {
unsigned long long total;
- unsigned long long free;
+ unsigned long long free; /* not used at all */
+ unsigned long long available; /* free plus what the kernel can take back */
unsigned long long shared;
unsigned long long buffers;
- unsigned long long cached;
+ unsigned long long cached; /* page cache and reclaimable slab */
unsigned long long totalswap;
unsigned long long freeswap;
};
diff --git a/shared/util.h b/shared/util.h
@@ -81,6 +81,7 @@ int eregcomp(regex_t *, const char *, int);
/* io */
ssize_t writeall(int, const void *, size_t);
+int copyall(int, int);
int concat(int, const char *, int, const char *);
/* misc */