diff --git a/.cirrus.yml b/.cirrus.yml new file mode 100644 index 00000000..15702fa8 --- /dev/null +++ b/.cirrus.yml @@ -0,0 +1,19 @@ +freebsd_instance: + #image: freebsd-12-1-release-amd64 + image_family: freebsd-12-1-snap + +env: + CIRRUS_CLONE_DEPTH: 1 + +task: + only_if: $CIRRUS_BRANCH != 'svn_head' + timeout_in: 15m + pkg_install_script: + - pkg install -y git-lite + script: + - make -j$(sysctl -n hw.ncpu) + install_script: + - make install + test_script: + - make regress + - make -C regress regress || true diff --git a/Makefile b/Makefile index 50405134..633f9a98 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -SUBDIR = libexec got tog +SUBDIR = openbsd-compat libexec got tog .PHONY: release dist diff --git a/Makefile.inc b/Makefile.inc index cbc71713..75319d1d 100644 --- a/Makefile.inc +++ b/Makefile.inc @@ -3,11 +3,20 @@ CPPFLAGS += -DGOT_LIBEXECDIR=${LIBEXECDIR} -DGOT_VERSION=${GOT_VERSION} #CFLAGS += -DGOT_NO_OBJ_CACHE #CFLAGS += -DGOT_OBJ_CACHE_DEBUG +OPENBSD_COMPAT := ${.PARSEDIR}/openbsd-compat +CFLAGS += ${CPPFLAGS} -I${OPENBSD_COMPAT} +CFLAGS += -Wno-pointer-sign + +LDADD += -lopenbsd-compat +LIBADD += md + +WARNS = 0 +CFLAGS += -fPIC + .if "${GOT_RELEASE}" == "Yes" -PREFIX ?= /usr/local -BINDIR ?= ${PREFIX}/bin -LIBEXECDIR ?= ${PREFIX}/libexec -MANDIR ?= ${PREFIX}/man/man +BINDIR ?= /usr/bin +LIBEXECDIR ?= /usr/libexec +MANDIR ?= /usr/share/man/man .else CFLAGS += -Werror -Wall -Wstrict-prototypes -Wunused-variable PREFIX ?= ${HOME} diff --git a/got-version.mk b/got-version.mk index cd406196..7df7b56d 100644 --- a/got-version.mk +++ b/got-version.mk @@ -1,4 +1,4 @@ -GOT_RELEASE=No +GOT_RELEASE=Yes GOT_VERSION_NUMBER=0.43 .if ${GOT_RELEASE} == Yes diff --git a/got/Makefile b/got/Makefile index 0eb11ed9..75c85b26 100644 --- a/got/Makefile +++ b/got/Makefile @@ -13,22 +13,19 @@ SRCS= got.c blame.c commit_graph.c delta.c diff.c \ MAN = ${PROG}.1 got-worktree.5 git-repository.5 got.conf.5 CPPFLAGS = -I${.CURDIR}/../include -I${.CURDIR}/../lib +LDFLAGS += -L${.OBJDIR}/../openbsd-compat .if defined(PROFILE) -LDADD = -lutil_p -lz_p -lc_p +LDADD += -lutil_p -lz_p -lc_p +DPADD += ${LIBZ} ${LIBUTIL} .else -LDADD = -lutil -lz +LIBADD += util z .endif -DPADD = ${LIBZ} ${LIBUTIL} .if ${GOT_RELEASE} != "Yes" -NOMAN = Yes +MAN= .endif -realinstall: - ${INSTALL} ${INSTALL_COPY} -o ${BINOWN} -g ${BINGRP} \ - -m ${BINMODE} ${PROG} ${BINDIR}/${PROG} - dist: mkdir ../got-${GOT_VERSION}/got cp ${SRCS} ${MAN} ../got-${GOT_VERSION}/got diff --git a/got/got.c b/got/got.c index f7f14b9c..52540c3a 100644 --- a/got/got.c +++ b/got/got.c @@ -40,6 +40,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_version.h" #include "got_error.h" #include "got_object.h" @@ -173,7 +175,7 @@ static struct got_cmd got_commands[] = { static void list_commands(FILE *fp) { - int i; + size_t i; fprintf(fp, "commands:"); for (i = 0; i < nitems(got_commands); i++) { @@ -7868,7 +7870,8 @@ write_cmd_list(FILE *f, const char *branch_name, struct got_object_id_queue *commits) { const struct got_error *err = NULL; - int n, i; + size_t i; + int n; char *id_str; struct got_object_qid *qid; @@ -8059,9 +8062,9 @@ histedit_parse_list(struct got_histedit_list *histedit_cmds, { const struct got_error *err = NULL; char *line = NULL, *p, *end; - size_t size; + size_t i, size; ssize_t len; - int lineno = 0, i; + int lineno = 0; const struct got_histedit_cmd *cmd; struct got_object_id *commit_id = NULL; struct got_histedit_list_entry *hle = NULL; diff --git a/gotweb/Makefile b/gotweb/Makefile index 86cf8bc9..4a066c78 100644 --- a/gotweb/Makefile +++ b/gotweb/Makefile @@ -16,11 +16,13 @@ MAN = ${PROG}.conf.5 ${PROG}.8 CPPFLAGS += -I${.CURDIR}/../include -I${.CURDIR}/../lib -I${.CURDIR} \ -I${KCGIBASE}/include -LDADD += -L${KCGIBASE}/lib -lkcgihtml -lkcgi -lz +LDADD += -L${KCGIBASE}/lib -lkcgihtml -lkcgi +LDFLAGS += -L${.OBJDIR}/../openbsd-compat +LIBADD += z LDSTATIC = ${STATIC} .if ${GOT_RELEASE} != "Yes" -NOMAN = Yes +MAN= .endif realinstall: diff --git a/gotweb/Makefile.inc b/gotweb/Makefile.inc index b892a273..1e28d2fe 100644 --- a/gotweb/Makefile.inc +++ b/gotweb/Makefile.inc @@ -1,5 +1,5 @@ KCGIBASE ?= /usr/local -LDADD += -lz -lutil +LIBADD += z util LDSTATIC = ${STATIC} CHROOT_DIR ?= /var/www GOTWEB_DIR = /cgi-bin/gotweb diff --git a/gotweb/gotweb.c b/gotweb/gotweb.c index 9dedeb9d..23bc2730 100644 --- a/gotweb/gotweb.c +++ b/gotweb/gotweb.c @@ -47,6 +47,8 @@ #include #include +#include "openbsd-compat.h" + #include "gotweb.h" #ifndef nitems diff --git a/gotweb/libexec/Makefile.inc b/gotweb/libexec/Makefile.inc index 85bee267..008e88c3 100644 --- a/gotweb/libexec/Makefile.inc +++ b/gotweb/libexec/Makefile.inc @@ -8,4 +8,4 @@ realinstall: ${INSTALL} ${INSTALL_COPY} -o root -g daemon -m 755 ${PROG} \ ${DESTDIR}${CHROOT_DIR}${LIBEXECDIR}/${PROG} -NOMAN = Yes +MAN= diff --git a/gotweb/libexec/got-read-blob/Makefile b/gotweb/libexec/got-read-blob/Makefile index 8a3f38ce..c3996ba7 100644 --- a/gotweb/libexec/got-read-blob/Makefile +++ b/gotweb/libexec/got-read-blob/Makefile @@ -7,8 +7,8 @@ SRCS= got-read-blob.c error.c inflate.c object_parse.c \ path.c privsep.c sha1.c CPPFLAGS = -I${.CURDIR}/../../../include -I${.CURDIR}/../../../lib -LDADD = -lutil -lz -DPADD = ${LIBZ} ${LIBUTIL} +LDFLAGS += -L${.OBJDIR}/../openbsd-compat +LIBADD = util z LDSTATIC = ${STATIC} .PATH: ${.CURDIR}/../../../lib ${.CURDIR}/../../../libexec/got-read-blob diff --git a/gotweb/libexec/got-read-commit/Makefile b/gotweb/libexec/got-read-commit/Makefile index 0996068f..5c7a88fb 100644 --- a/gotweb/libexec/got-read-commit/Makefile +++ b/gotweb/libexec/got-read-commit/Makefile @@ -7,8 +7,8 @@ SRCS= got-read-commit.c error.c inflate.c object_parse.c \ path.c privsep.c sha1.c CPPFLAGS = -I${.CURDIR}/../../../include -I${.CURDIR}/../../../lib -LDADD = -lutil -lz -DPADD = ${LIBZ} ${LIBUTIL} +LDFLAGS += -L${.OBJDIR}/../openbsd-compat +LIBADD = util z LDSTATIC = ${STATIC} .PATH: ${.CURDIR}/../../../lib ${.CURDIR}/../../../libexec/got-read-commit diff --git a/gotweb/libexec/got-read-gitconfig/Makefile b/gotweb/libexec/got-read-gitconfig/Makefile index 77cc7852..c1054cca 100644 --- a/gotweb/libexec/got-read-gitconfig/Makefile +++ b/gotweb/libexec/got-read-gitconfig/Makefile @@ -7,8 +7,8 @@ SRCS= got-read-gitconfig.c error.c inflate.c object_parse.c \ path.c privsep.c sha1.c gitconfig.c CPPFLAGS = -I${.CURDIR}/../../../include -I${.CURDIR}/../../../lib -LDADD = -lutil -lz -DPADD = ${LIBZ} ${LIBUTIL} +LDFLAGS += -L${.OBJDIR}/../openbsd-compat +LIBADD = util z LDSTATIC = ${STATIC} .PATH: ${.CURDIR}/../../../lib ${.CURDIR}/../../../libexec/got-read-gitconfig diff --git a/gotweb/libexec/got-read-gotconfig/Makefile b/gotweb/libexec/got-read-gotconfig/Makefile index 29605918..7333c8ef 100644 --- a/gotweb/libexec/got-read-gotconfig/Makefile +++ b/gotweb/libexec/got-read-gotconfig/Makefile @@ -8,8 +8,8 @@ SRCS= got-read-gotconfig.c error.c inflate.c object_parse.c \ CPPFLAGS = -I${.CURDIR}/../../../include -I${.CURDIR}/../../../lib \ -I${.CURDIR}/../../../libexec/got-read-gotconfig -LDADD = -lutil -lz -DPADD = ${LIBZ} ${LIBUTIL} +LDFLAGS += -L${.OBJDIR}/../openbsd-compat +LIBDADD = util z LDSTATIC = ${STATIC} .PATH: ${.CURDIR}/../../../lib ${.CURDIR}/../../../libexec/got-read-gotconfig diff --git a/gotweb/libexec/got-read-object/Makefile b/gotweb/libexec/got-read-object/Makefile index 4889fe0b..207cc2c0 100644 --- a/gotweb/libexec/got-read-object/Makefile +++ b/gotweb/libexec/got-read-object/Makefile @@ -7,8 +7,8 @@ SRCS= got-read-object.c error.c inflate.c object_parse.c \ path.c privsep.c sha1.c CPPFLAGS = -I${.CURDIR}/../../../include -I${.CURDIR}/../../../lib -LDADD = -lutil -lz -DPADD = ${LIBZ} ${LIBUTIL} +LDFLAGS += -L${.OBJDIR}/../openbsd-compat +LIBADD = util z LDSTATIC = ${STATIC} .PATH: ${.CURDIR}/../../../lib ${.CURDIR}/../../../libexec/got-read-object diff --git a/gotweb/libexec/got-read-pack/Makefile b/gotweb/libexec/got-read-pack/Makefile index a28b9cfd..ca5fdbda 100644 --- a/gotweb/libexec/got-read-pack/Makefile +++ b/gotweb/libexec/got-read-pack/Makefile @@ -8,8 +8,8 @@ SRCS= got-read-pack.c delta.c error.c inflate.c object_cache.c \ privsep.c sha1.c delta_cache.c CPPFLAGS = -I${.CURDIR}/../../../include -I${.CURDIR}/../../../lib -LDADD = -lutil -lz -DPADD = ${LIBZ} ${LIBUTIL} +LDFLAGS += -L${.OBJDIR}/../openbsd-compat +LIBADD = util z LDSTATIC = ${STATIC} .PATH: ${.CURDIR}/../../../lib ${.CURDIR}/../../../libexec/got-read-pack diff --git a/gotweb/libexec/got-read-tag/Makefile b/gotweb/libexec/got-read-tag/Makefile index 3a0b798c..ca1acc06 100644 --- a/gotweb/libexec/got-read-tag/Makefile +++ b/gotweb/libexec/got-read-tag/Makefile @@ -7,8 +7,8 @@ SRCS= got-read-tag.c error.c inflate.c object_parse.c \ path.c privsep.c sha1.c CPPFLAGS = -I${.CURDIR}/../../../include -I${.CURDIR}/../../../lib -LDADD = -lutil -lz -DPADD = ${LIBZ} ${LIBUTIL} +LDFLAGS += -L${.OBJDIR}/../openbsd-compat +LIBADD = util z LDSTATIC = ${STATIC} .PATH: ${.CURDIR}/../../../lib ${.CURDIR}/../../../libexec/got-read-tag diff --git a/gotweb/libexec/got-read-tree/Makefile b/gotweb/libexec/got-read-tree/Makefile index 19a4c9cf..5428fcd2 100644 --- a/gotweb/libexec/got-read-tree/Makefile +++ b/gotweb/libexec/got-read-tree/Makefile @@ -7,8 +7,8 @@ SRCS= got-read-tree.c error.c inflate.c object_parse.c \ path.c privsep.c sha1.c CPPFLAGS = -I${.CURDIR}/../../../include -I${.CURDIR}/../../../lib -LDADD = -lutil -lz -DPADD = ${LIBZ} ${LIBUTIL} +LDFLAGS += -L${.OBJDIR}/../openbsd-compat +LIBADD = util z LDSTATIC = ${STATIC} .PATH: ${.CURDIR}/../../../lib ${.CURDIR}/../../../libexec/got-read-tree diff --git a/gotweb/parse.y b/gotweb/parse.y index 6412c9a5..af0a7699 100644 --- a/gotweb/parse.y +++ b/gotweb/parse.y @@ -32,6 +32,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "gotweb.h" diff --git a/lib/blame.c b/lib/blame.c index f8f1b5ea..423a6dc4 100644 --- a/lib/blame.c +++ b/lib/blame.c @@ -26,6 +26,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" #include "got_cancel.h" diff --git a/lib/buf.c b/lib/buf.c index 3986b23c..d9a202e3 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -35,6 +35,8 @@ #include #include +#include "openbsd-compat.h" + #include "buf.h" #include "got_error.h" diff --git a/lib/commit_graph.c b/lib/commit_graph.c index 29a4d2fb..8e98e417 100644 --- a/lib/commit_graph.c +++ b/lib/commit_graph.c @@ -27,6 +27,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" #include "got_cancel.h" diff --git a/lib/deflate.c b/lib/deflate.c index c7771b4a..d99deb06 100644 --- a/lib/deflate.c +++ b/lib/deflate.c @@ -24,6 +24,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" #include "got_path.h" diff --git a/lib/delta.c b/lib/delta.c index 194fedc8..9747f51d 100644 --- a/lib/delta.c +++ b/lib/delta.c @@ -26,6 +26,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_repository.h" #include "got_object.h" diff --git a/lib/delta_cache.c b/lib/delta_cache.c index a8493a58..16e94fef 100644 --- a/lib/delta_cache.c +++ b/lib/delta_cache.c @@ -24,6 +24,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_object.h" #include "got_error.h" diff --git a/lib/diff.c b/lib/diff.c index 7e5ee069..68e7084b 100644 --- a/lib/diff.c +++ b/lib/diff.c @@ -24,6 +24,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_object.h" #include "got_repository.h" #include "got_error.h" diff --git a/lib/diff3.c b/lib/diff3.c index e8841fa9..163577c9 100644 --- a/lib/diff3.c +++ b/lib/diff3.c @@ -76,6 +76,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_opentemp.h" #include "got_object.h" @@ -280,7 +282,7 @@ got_merge_diff3(int *overlapcnt, int outfd, const char *p1, const char *p2, u_char *data, *patch; size_t dlen, plen; struct diff3_state *d3s; - int i; + size_t i; *overlapcnt = 0; diff --git a/lib/diffreg.c b/lib/diffreg.c index 6ababe76..18b1c18a 100644 --- a/lib/diffreg.c +++ b/lib/diffreg.c @@ -86,6 +86,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" #include "got_diff.h" diff --git a/lib/error.c b/lib/error.c index 92ce7a5f..055f2bd9 100644 --- a/lib/error.c +++ b/lib/error.c @@ -25,6 +25,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" @@ -40,7 +42,7 @@ const struct got_error * got_error(int code) { - int i; + size_t i; for (i = 0; i < nitems(got_errors); i++) { if (code == got_errors[i].code) @@ -54,7 +56,7 @@ const struct got_error * got_error_msg(int code, const char *msg) { static struct got_error err; - int i; + size_t i; for (i = 0; i < nitems(got_errors); i++) { if (code == got_errors[i].code) { @@ -178,7 +180,7 @@ got_error_path(const char *path, int code) { static struct got_error err; static char msg[PATH_MAX + 128]; - int i; + size_t i; for (i = 0; i < nitems(got_errors); i++) { if (code == got_errors[i].code) { diff --git a/lib/fetch.c b/lib/fetch.c index 3bdd5de6..2b1808de 100644 --- a/lib/fetch.c +++ b/lib/fetch.c @@ -42,6 +42,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_reference.h" #include "got_repository.h" @@ -359,9 +361,10 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs, int fetchfd, struct got_repository *repo, got_fetch_progress_cb progress_cb, void *progress_arg) { + size_t i; int imsg_fetchfds[2], imsg_idxfds[2]; int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1; - int tmpfds[3], i; + int tmpfds[3]; int fetchstatus, idxstatus, done = 0; const struct got_error *err; struct imsgbuf fetchibuf, idxibuf; diff --git a/lib/fileindex.c b/lib/fileindex.c index 04443aa0..b46dbb29 100644 --- a/lib/fileindex.c +++ b/lib/fileindex.c @@ -30,6 +30,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" #include "got_path.h" diff --git a/lib/gitconfig.c b/lib/gitconfig.c index 69956c3e..c01b94dd 100644 --- a/lib/gitconfig.c +++ b/lib/gitconfig.c @@ -39,6 +39,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_lib_gitconfig.h" @@ -309,7 +311,7 @@ conf_parse(struct got_gitconfig *conf, int trans, char *buf, size_t sz) const struct got_error * got_gitconfig_open(struct got_gitconfig **conf, int fd) { - unsigned int i; + size_t i; *conf = calloc(1, sizeof(**conf)); if (*conf == NULL) @@ -325,7 +327,7 @@ static void conf_clear(struct got_gitconfig *conf) { struct got_gitconfig_binding *cb; - int i; + size_t i; if (conf->addr) { for (i = 0; i < nitems(conf->bindings); i++) @@ -502,7 +504,7 @@ got_gitconfig_get_section_list(struct got_gitconfig_list **sections, struct got_gitconfig_list *list = NULL; struct got_gitconfig_list_node *node = 0; struct got_gitconfig_binding *cb; - int i; + size_t i; *sections = NULL; diff --git a/lib/gotconfig.c b/lib/gotconfig.c index 7133ec58..d88814a1 100644 --- a/lib/gotconfig.c +++ b/lib/gotconfig.c @@ -29,6 +29,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" #include "got_repository.h" diff --git a/lib/inflate.c b/lib/inflate.c index 1197bbb4..3cb348d9 100644 --- a/lib/inflate.c +++ b/lib/inflate.c @@ -25,6 +25,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" #include "got_path.h" diff --git a/lib/lockfile.c b/lib/lockfile.c index bc4cf864..2e718693 100644 --- a/lib/lockfile.c +++ b/lib/lockfile.c @@ -25,6 +25,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_path.h" diff --git a/lib/object.c b/lib/object.c index a8e56d3a..852c8c8b 100644 --- a/lib/object.c +++ b/lib/object.c @@ -37,6 +37,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" #include "got_repository.h" diff --git a/lib/object_cache.c b/lib/object_cache.c index adc0e664..b0cd2e70 100644 --- a/lib/object_cache.c +++ b/lib/object_cache.c @@ -25,6 +25,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" diff --git a/lib/object_create.c b/lib/object_create.c index a1f3f649..6c4efe0b 100644 --- a/lib/object_create.c +++ b/lib/object_create.c @@ -30,6 +30,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" #include "got_repository.h" diff --git a/lib/object_idset.c b/lib/object_idset.c index 9c296556..8c669355 100644 --- a/lib/object_idset.c +++ b/lib/object_idset.c @@ -25,6 +25,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_object.h" #include "got_error.h" diff --git a/lib/object_parse.c b/lib/object_parse.c index 2428cdcd..04339738 100644 --- a/lib/object_parse.c +++ b/lib/object_parse.c @@ -34,6 +34,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" #include "got_repository.h" @@ -167,7 +169,7 @@ got_object_parse_header(struct got_object **obj, char *buf, size_t len) }; int type = 0; size_t size = 0, hdrlen = 0; - int i; + size_t i; *obj = NULL; diff --git a/lib/opentemp.c b/lib/opentemp.c index e998f5bc..73f48d7f 100644 --- a/lib/opentemp.c +++ b/lib/opentemp.c @@ -20,6 +20,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_opentemp.h" #include "got_error.h" diff --git a/lib/pack.c b/lib/pack.c index 71b5e073..3ce002a8 100644 --- a/lib/pack.c +++ b/lib/pack.c @@ -33,6 +33,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" #include "got_opentemp.h" diff --git a/lib/path.c b/lib/path.c index 54f8f6f9..a670334c 100644 --- a/lib/path.c +++ b/lib/path.c @@ -30,6 +30,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_path.h" diff --git a/lib/privsep.c b/lib/privsep.c index d15fdd1c..1623faa8 100644 --- a/lib/privsep.c +++ b/lib/privsep.c @@ -34,6 +34,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_object.h" #include "got_error.h" #include "got_path.h" @@ -2252,7 +2254,7 @@ got_privsep_unveil_exec_helpers(void) GOT_PATH_PROG_FETCH_PACK, GOT_PATH_PROG_INDEX_PACK, }; - int i; + size_t i; for (i = 0; i < nitems(helpers); i++) { if (unveil(helpers[i], "x") == 0) diff --git a/lib/rcsutil.c b/lib/rcsutil.c index ac95dc7d..949c1991 100644 --- a/lib/rcsutil.c +++ b/lib/rcsutil.c @@ -35,6 +35,8 @@ #include #include +#include "openbsd-compat.h" + #include "buf.h" #include "rcsutil.h" diff --git a/lib/reference.c b/lib/reference.c index 2b33b7d2..106c67f2 100644 --- a/lib/reference.c +++ b/lib/reference.c @@ -32,6 +32,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" #include "got_repository.h" @@ -264,7 +266,7 @@ is_valid_ref_name(const char *name) const char *forbidden_seq[] = { "//", "..", "@{" }; const char *lfs = GOT_LOCKFILE_SUFFIX; const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1; - int i; + size_t i; if (name[0] == '@' && name[1] == '\0') return 0; @@ -439,7 +441,8 @@ got_ref_open(struct got_reference **ref, struct got_repository *repo, const char *subdirs[] = { GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES }; - int i, well_known = is_well_known_ref(refname); + size_t i; + int well_known = is_well_known_ref(refname); struct got_lockfile *lf = NULL; *ref = NULL; diff --git a/lib/repository.c b/lib/repository.c index 036305e9..c408233b 100644 --- a/lib/repository.c +++ b/lib/repository.c @@ -40,6 +40,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_reference.h" #include "got_repository.h" @@ -552,7 +554,7 @@ got_repo_open(struct got_repository **repop, const char *path, struct got_repository *repo = NULL; const struct got_error *err = NULL; char *abspath, *repo_path = NULL; - int i; + size_t i; *repop = NULL; @@ -640,7 +642,7 @@ const struct got_error * got_repo_close(struct got_repository *repo) { const struct got_error *err = NULL, *child_err; - int i; + size_t i; for (i = 0; i < nitems(repo->packidx_cache); i++) { if (repo->packidx_cache[i] == NULL) @@ -814,7 +816,7 @@ cache_packidx(struct got_repository *repo, struct got_packidx *packidx, const char *path_packidx) { const struct got_error *err = NULL; - int i; + size_t i; for (i = 0; i < nitems(repo->packidx_cache); i++) { if (repo->packidx_cache[i] == NULL) @@ -867,7 +869,7 @@ got_repo_search_packidx(struct got_packidx **packidx, int *idx, DIR *packdir; struct dirent *dent; char *path_packidx; - int i; + size_t i; /* Search pack index cache. */ for (i = 0; i < nitems(repo->packidx_cache); i++) { @@ -1005,7 +1007,7 @@ got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo, const struct got_error *err = NULL; struct got_pack *pack = NULL; struct stat sb; - int i; + size_t i; if (packp) *packp = NULL; @@ -1073,7 +1075,7 @@ struct got_pack * got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile) { struct got_pack *pack = NULL; - int i; + size_t i; for (i = 0; i < nitems(repo->packs); i++) { pack = &repo->packs[i]; @@ -1103,7 +1105,7 @@ got_repo_init(const char *repo_path) "\tfilemode = true\n" "\tbare = true\n"; char *path; - int i; + size_t i; if (!got_path_dir_is_empty(repo_path)) return got_error(GOT_ERR_DIR_NOT_EMPTY); diff --git a/lib/sha1.c b/lib/sha1.c index 1f25f1bb..50c293da 100644 --- a/lib/sha1.c +++ b/lib/sha1.c @@ -21,6 +21,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_lib_sha1.h" int diff --git a/lib/utf8.c b/lib/utf8.c index 167f4f59..df87993c 100644 --- a/lib/utf8.c +++ b/lib/utf8.c @@ -23,6 +23,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_utf8.h" diff --git a/lib/worktree.c b/lib/worktree.c index 54a1c38f..3ee4e0d5 100644 --- a/lib/worktree.c +++ b/lib/worktree.c @@ -35,6 +35,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_repository.h" #include "got_reference.h" diff --git a/libexec/Makefile.inc b/libexec/Makefile.inc index da3924e2..7f1fdae6 100644 --- a/libexec/Makefile.inc +++ b/libexec/Makefile.inc @@ -1,7 +1,5 @@ .include "../Makefile.inc" -realinstall: - ${INSTALL} ${INSTALL_COPY} -o ${BINOWN} -g ${BINGRP} \ - -m ${BINMODE} ${PROG} ${LIBEXECDIR}/${PROG} +BINDIR = ${PREFIX}/libexec -NOMAN = Yes +MAN = diff --git a/libexec/got-fetch-pack/Makefile b/libexec/got-fetch-pack/Makefile index 7823de67..53db82b3 100644 --- a/libexec/got-fetch-pack/Makefile +++ b/libexec/got-fetch-pack/Makefile @@ -7,7 +7,7 @@ SRCS= got-fetch-pack.c error.c inflate.c object_parse.c \ path.c privsep.c sha1.c CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib -LDADD = -lutil -lz -DPADD = ${LIBZ} ${LIBUTIL} +LDFLAGS += -L${.OBJDIR}/../../openbsd-compat +LIBADD = util z .include diff --git a/libexec/got-fetch-pack/got-fetch-pack.c b/libexec/got-fetch-pack/got-fetch-pack.c index 90f2ff63..5b5cfd68 100644 --- a/libexec/got-fetch-pack/got-fetch-pack.c +++ b/libexec/got-fetch-pack/got-fetch-pack.c @@ -35,6 +35,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" #include "got_path.h" @@ -437,7 +439,7 @@ match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs, { const struct got_error *err = NULL; char *capa, *equalsign; - int i; + size_t i; *my_capabilities = NULL; do { diff --git a/libexec/got-index-pack/Makefile b/libexec/got-index-pack/Makefile index 483b8359..a2a16b68 100644 --- a/libexec/got-index-pack/Makefile +++ b/libexec/got-index-pack/Makefile @@ -7,7 +7,7 @@ SRCS= got-index-pack.c error.c inflate.c object_parse.c object_idset.c \ delta_cache.c delta.c pack.c path.c privsep.c sha1.c CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib -LDADD = -lutil -lz -DPADD = ${LIBZ} ${LIBUTIL} +LDFLAGS += -L${.OBJDIR}/../../openbsd-compat +LIBADD = util z .include diff --git a/libexec/got-index-pack/got-index-pack.c b/libexec/got-index-pack/got-index-pack.c index 5288649c..f6c6da88 100644 --- a/libexec/got-index-pack/got-index-pack.c +++ b/libexec/got-index-pack/got-index-pack.c @@ -40,6 +40,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" @@ -974,7 +976,8 @@ main(int argc, char **argv) const struct got_error *err = NULL, *close_err; struct imsgbuf ibuf; struct imsg imsg; - int idxfd = -1, tmpfd = -1, i; + size_t i; + int idxfd = -1, tmpfd = -1; FILE *tmpfiles[3]; struct got_pack pack; uint8_t pack_hash[SHA1_DIGEST_LENGTH]; diff --git a/libexec/got-read-blob/Makefile b/libexec/got-read-blob/Makefile index 17ab97b2..a29d2d33 100644 --- a/libexec/got-read-blob/Makefile +++ b/libexec/got-read-blob/Makefile @@ -7,7 +7,7 @@ SRCS= got-read-blob.c error.c inflate.c object_parse.c \ path.c privsep.c sha1.c CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib -LDADD = -lutil -lz -DPADD = ${LIBZ} ${LIBUTIL} +LDFLAGS += -L${.OBJDIR}/../../openbsd-compat +LIBADD = util z .include diff --git a/libexec/got-read-blob/got-read-blob.c b/libexec/got-read-blob/got-read-blob.c index 7e406576..1e61378b 100644 --- a/libexec/got-read-blob/got-read-blob.c +++ b/libexec/got-read-blob/got-read-blob.c @@ -30,6 +30,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" diff --git a/libexec/got-read-commit/Makefile b/libexec/got-read-commit/Makefile index 3f6f7ca0..27f0d0e4 100644 --- a/libexec/got-read-commit/Makefile +++ b/libexec/got-read-commit/Makefile @@ -7,7 +7,7 @@ SRCS= got-read-commit.c error.c inflate.c object_parse.c \ path.c privsep.c sha1.c CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib -LDADD = -lutil -lz -DPADD = ${LIBZ} ${LIBUTIL} +LDFLAGS += -L${.OBJDIR}/../../openbsd-compat +LIBADD = util z .include diff --git a/libexec/got-read-commit/got-read-commit.c b/libexec/got-read-commit/got-read-commit.c index 02685f91..33eb6602 100644 --- a/libexec/got-read-commit/got-read-commit.c +++ b/libexec/got-read-commit/got-read-commit.c @@ -30,6 +30,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" diff --git a/libexec/got-read-gitconfig/Makefile b/libexec/got-read-gitconfig/Makefile index f49bf065..61f5038e 100644 --- a/libexec/got-read-gitconfig/Makefile +++ b/libexec/got-read-gitconfig/Makefile @@ -7,7 +7,7 @@ SRCS= got-read-gitconfig.c error.c inflate.c object_parse.c \ path.c privsep.c sha1.c gitconfig.c CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib -LDADD = -lutil -lz -DPADD = ${LIBZ} ${LIBUTIL} +LDFLAGS += -L${.OBJDIR}/../../openbsd-compat +LIBADD = util z .include diff --git a/libexec/got-read-gitconfig/got-read-gitconfig.c b/libexec/got-read-gitconfig/got-read-gitconfig.c index e24545d2..1f544979 100644 --- a/libexec/got-read-gitconfig/got-read-gitconfig.c +++ b/libexec/got-read-gitconfig/got-read-gitconfig.c @@ -30,6 +30,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" #include "got_repository.h" diff --git a/libexec/got-read-gotconfig/Makefile b/libexec/got-read-gotconfig/Makefile index a683cf2e..4e0e2315 100644 --- a/libexec/got-read-gotconfig/Makefile +++ b/libexec/got-read-gotconfig/Makefile @@ -7,7 +7,7 @@ SRCS= got-read-gotconfig.c error.c inflate.c object_parse.c \ path.c privsep.c sha1.c parse.y CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib -I${.CURDIR} -LDADD = -lutil -lz -DPADD = ${LIBZ} ${LIBUTIL} +LDFLAGS += -L${.OBJDIR}/../../openbsd-compat +LIBADD = util z .include diff --git a/libexec/got-read-gotconfig/got-read-gotconfig.c b/libexec/got-read-gotconfig/got-read-gotconfig.c index 631e1123..c7c445b0 100644 --- a/libexec/got-read-gotconfig/got-read-gotconfig.c +++ b/libexec/got-read-gotconfig/got-read-gotconfig.c @@ -30,6 +30,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" #include "got_path.h" diff --git a/libexec/got-read-gotconfig/parse.y b/libexec/got-read-gotconfig/parse.y index df2c1d75..0f5645f8 100644 --- a/libexec/got-read-gotconfig/parse.y +++ b/libexec/got-read-gotconfig/parse.y @@ -36,6 +36,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "gotconfig.h" @@ -61,7 +63,7 @@ int igetc(void); int lgetc(int); void lungetc(int); int findeol(void); -static int parseport(char *, long long *); +static int parseport(char *, int64_t *); TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead); struct sym { @@ -126,7 +128,7 @@ boolean : STRING { ; numberstring : NUMBER { char *s; - if (asprintf(&s, "%lld", $1) == -1) { + if (asprintf(&s, "%lld", (long long)$1) == -1) { yyerror("string: asprintf"); YYERROR; } @@ -417,7 +419,7 @@ getservice(char *n) } static int -parseport(char *port, long long *pn) +parseport(char *port, int64_t *pn) { if ((*pn = getservice(port)) == -1) { *pn = 0LL; diff --git a/libexec/got-read-object/Makefile b/libexec/got-read-object/Makefile index 26342119..f773c60d 100644 --- a/libexec/got-read-object/Makefile +++ b/libexec/got-read-object/Makefile @@ -7,7 +7,7 @@ SRCS= got-read-object.c error.c inflate.c object_parse.c \ path.c privsep.c sha1.c CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib -LDADD = -lutil -lz -DPADD = ${LIBZ} ${LIBUTIL} +LDFLAGS += -L${.OBJDIR}/../../openbsd-compat +LIBADD = util z .include diff --git a/libexec/got-read-object/got-read-object.c b/libexec/got-read-object/got-read-object.c index 5e190138..3f4881e6 100644 --- a/libexec/got-read-object/got-read-object.c +++ b/libexec/got-read-object/got-read-object.c @@ -30,6 +30,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" diff --git a/libexec/got-read-pack/Makefile b/libexec/got-read-pack/Makefile index 5fe2b7ef..207688dc 100644 --- a/libexec/got-read-pack/Makefile +++ b/libexec/got-read-pack/Makefile @@ -8,7 +8,7 @@ SRCS= got-read-pack.c delta.c error.c inflate.c object_cache.c \ privsep.c sha1.c delta_cache.c CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib -LDADD = -lutil -lz -DPADD = ${LIBZ} ${LIBUTIL} +LDFLAGS += -L${.OBJDIR}/../../openbsd-compat +LIBADD = util z .include diff --git a/libexec/got-read-pack/got-read-pack.c b/libexec/got-read-pack/got-read-pack.c index 27d10ab7..ab77495b 100644 --- a/libexec/got-read-pack/got-read-pack.c +++ b/libexec/got-read-pack/got-read-pack.c @@ -31,6 +31,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" #include "got_path.h" diff --git a/libexec/got-read-tag/Makefile b/libexec/got-read-tag/Makefile index a8026e4c..86533b75 100644 --- a/libexec/got-read-tag/Makefile +++ b/libexec/got-read-tag/Makefile @@ -7,7 +7,7 @@ SRCS= got-read-tag.c error.c inflate.c object_parse.c \ path.c privsep.c sha1.c CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib -LDADD = -lutil -lz -DPADD = ${LIBZ} ${LIBUTIL} +LDFLAGS += -L${.OBJDIR}/../../openbsd-compat +LIBADD = util z .include diff --git a/libexec/got-read-tag/got-read-tag.c b/libexec/got-read-tag/got-read-tag.c index aad8331a..f85bda49 100644 --- a/libexec/got-read-tag/got-read-tag.c +++ b/libexec/got-read-tag/got-read-tag.c @@ -30,6 +30,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" diff --git a/libexec/got-read-tree/Makefile b/libexec/got-read-tree/Makefile index 397775d1..d02879a4 100644 --- a/libexec/got-read-tree/Makefile +++ b/libexec/got-read-tree/Makefile @@ -7,7 +7,7 @@ SRCS= got-read-tree.c error.c inflate.c object_parse.c \ path.c privsep.c sha1.c CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib -LDADD = -lutil -lz -DPADD = ${LIBZ} ${LIBUTIL} +LDFLAGS += -L${.OBJDIR}/../../openbsd-compat +LIBADD = util z .include diff --git a/libexec/got-read-tree/got-read-tree.c b/libexec/got-read-tree/got-read-tree.c index 741d51f9..d8a18dc9 100644 --- a/libexec/got-read-tree/got-read-tree.c +++ b/libexec/got-read-tree/got-read-tree.c @@ -30,6 +30,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" #include "got_path.h" diff --git a/openbsd-compat/Makefile b/openbsd-compat/Makefile new file mode 100644 index 00000000..68efdd3b --- /dev/null +++ b/openbsd-compat/Makefile @@ -0,0 +1,15 @@ +LIB= openbsd-compat +INTERNALLIB= + +SRCS= basename.c \ + dirname.c \ + freezero.c \ + getdtablecount.c \ + imsg.c \ + imsg-buffer.c \ + open.c \ + recallocarray.c + +CFLAGS+= -I${.CURDIR} + +.include diff --git a/openbsd-compat/basename.c b/openbsd-compat/basename.c new file mode 100644 index 00000000..84e9d13d --- /dev/null +++ b/openbsd-compat/basename.c @@ -0,0 +1,65 @@ +/* $OpenBSD: basename.c,v 1.16 2019/01/25 00:19:25 millert Exp $ */ + +/* + * Copyright (c) 1997, 2004 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include + +#include + +char * +basename(const char *path) +{ + static char bname[PATH_MAX]; + size_t len; + const char *endp, *startp; + + /* Empty or NULL string gets treated as "." */ + if (path == NULL || *path == '\0') { + bname[0] = '.'; + bname[1] = '\0'; + return (bname); + } + + /* Strip any trailing slashes */ + endp = path + strlen(path) - 1; + while (endp > path && *endp == '/') + endp--; + + /* All slashes becomes "/" */ + if (endp == path && *endp == '/') { + bname[0] = '/'; + bname[1] = '\0'; + return (bname); + } + + /* Find the start of the base */ + startp = endp; + while (startp > path && *(startp - 1) != '/') + startp--; + + len = endp - startp + 1; + if (len >= sizeof(bname)) { + errno = ENAMETOOLONG; + return (NULL); + } + memcpy(bname, startp, len); + bname[len] = '\0'; + return (bname); +} diff --git a/openbsd-compat/dirname.c b/openbsd-compat/dirname.c new file mode 100644 index 00000000..1973f82e --- /dev/null +++ b/openbsd-compat/dirname.c @@ -0,0 +1,71 @@ +/* $OpenBSD: dirname.c,v 1.16 2019/01/25 00:19:25 millert Exp $ */ + +/* + * Copyright (c) 1997, 2004 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include + +#include + +/* A slightly modified copy of this file exists in libexec/ld.so */ + +char * +dirname(const char *path) +{ + static char dname[PATH_MAX]; + size_t len; + const char *endp; + + /* Empty or NULL string gets treated as "." */ + if (path == NULL || *path == '\0') { + dname[0] = '.'; + dname[1] = '\0'; + return (dname); + } + + /* Strip any trailing slashes */ + endp = path + strlen(path) - 1; + while (endp > path && *endp == '/') + endp--; + + /* Find the start of the dir */ + while (endp > path && *endp != '/') + endp--; + + /* Either the dir is "/" or there are no slashes */ + if (endp == path) { + dname[0] = *endp == '/' ? '/' : '.'; + dname[1] = '\0'; + return (dname); + } else { + /* Move forward past the separating slashes */ + do { + endp--; + } while (endp > path && *endp == '/'); + } + + len = endp - path + 1; + if (len >= sizeof(dname)) { + errno = ENAMETOOLONG; + return (NULL); + } + memcpy(dname, path, len); + dname[len] = '\0'; + return (dname); +} diff --git a/openbsd-compat/endian.h b/openbsd-compat/endian.h new file mode 100644 index 00000000..4da84f32 --- /dev/null +++ b/openbsd-compat/endian.h @@ -0,0 +1,6 @@ +#ifndef _OPENBSD_COMPAT_ENDIAN_H_ +#define _OPENBSD_COMPAT_ENDIAN_H_ + +#include + +#endif diff --git a/openbsd-compat/freezero.c b/openbsd-compat/freezero.c new file mode 100644 index 00000000..90187e68 --- /dev/null +++ b/openbsd-compat/freezero.c @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2008, 2010, 2011, 2016 Otto Moerbeek + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +#include "openbsd-compat.h" + +void +freezero(void *ptr, size_t sz) +{ + explicit_bzero(ptr, sz); + free(ptr); +} diff --git a/openbsd-compat/getdtablecount.c b/openbsd-compat/getdtablecount.c new file mode 100644 index 00000000..0ed85e56 --- /dev/null +++ b/openbsd-compat/getdtablecount.c @@ -0,0 +1,57 @@ +/*- + * Copyright (c) 2015 Craig Rodrigues + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include + +int getdtablecount(void); + +/* + * Return the count of open file descriptors for this process. + */ +int +getdtablecount(void) +{ + int mib[4]; + int error; + int nfds; + size_t len; + + len = sizeof(nfds); + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_NFDS; + mib[3] = 0; + + error = sysctl(mib, 4, &nfds, &len, NULL, 0); + if (error) + return (-1); + return (nfds); +} diff --git a/openbsd-compat/imsg-buffer.c b/openbsd-compat/imsg-buffer.c new file mode 100644 index 00000000..de56921f --- /dev/null +++ b/openbsd-compat/imsg-buffer.c @@ -0,0 +1,311 @@ +/* $OpenBSD: imsg-buffer.c,v 1.12 2019/01/20 02:50:03 bcook Exp $ */ + +/* + * Copyright (c) 2003, 2004 Henning Brauer + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include "imsg.h" + +static int ibuf_realloc(struct ibuf *, size_t); +static void ibuf_enqueue(struct msgbuf *, struct ibuf *); +static void ibuf_dequeue(struct msgbuf *, struct ibuf *); + +struct ibuf * +ibuf_open(size_t len) +{ + struct ibuf *buf; + + if ((buf = calloc(1, sizeof(struct ibuf))) == NULL) + return (NULL); + if ((buf->buf = malloc(len)) == NULL) { + free(buf); + return (NULL); + } + buf->size = buf->max = len; + buf->fd = -1; + + return (buf); +} + +struct ibuf * +ibuf_dynamic(size_t len, size_t max) +{ + struct ibuf *buf; + + if (max < len) + return (NULL); + + if ((buf = ibuf_open(len)) == NULL) + return (NULL); + + if (max > 0) + buf->max = max; + + return (buf); +} + +static int +ibuf_realloc(struct ibuf *buf, size_t len) +{ + unsigned char *b; + + /* on static buffers max is eq size and so the following fails */ + if (buf->wpos + len > buf->max) { + errno = ERANGE; + return (-1); + } + + b = recallocarray(buf->buf, buf->size, buf->wpos + len, 1); + if (b == NULL) + return (-1); + buf->buf = b; + buf->size = buf->wpos + len; + + return (0); +} + +int +ibuf_add(struct ibuf *buf, const void *data, size_t len) +{ + if (buf->wpos + len > buf->size) + if (ibuf_realloc(buf, len) == -1) + return (-1); + + memcpy(buf->buf + buf->wpos, data, len); + buf->wpos += len; + return (0); +} + +void * +ibuf_reserve(struct ibuf *buf, size_t len) +{ + void *b; + + if (buf->wpos + len > buf->size) + if (ibuf_realloc(buf, len) == -1) + return (NULL); + + b = buf->buf + buf->wpos; + buf->wpos += len; + return (b); +} + +void * +ibuf_seek(struct ibuf *buf, size_t pos, size_t len) +{ + /* only allowed to seek in already written parts */ + if (pos + len > buf->wpos) + return (NULL); + + return (buf->buf + pos); +} + +size_t +ibuf_size(struct ibuf *buf) +{ + return (buf->wpos); +} + +size_t +ibuf_left(struct ibuf *buf) +{ + return (buf->max - buf->wpos); +} + +void +ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf) +{ + ibuf_enqueue(msgbuf, buf); +} + +int +ibuf_write(struct msgbuf *msgbuf) +{ + struct iovec iov[IOV_MAX]; + struct ibuf *buf; + unsigned int i = 0; + ssize_t n; + + memset(&iov, 0, sizeof(iov)); + TAILQ_FOREACH(buf, &msgbuf->bufs, entry) { + if (i >= IOV_MAX) + break; + iov[i].iov_base = buf->buf + buf->rpos; + iov[i].iov_len = buf->wpos - buf->rpos; + i++; + } + +again: + if ((n = writev(msgbuf->fd, iov, i)) == -1) { + if (errno == EINTR) + goto again; + if (errno == ENOBUFS) + errno = EAGAIN; + return (-1); + } + + if (n == 0) { /* connection closed */ + errno = 0; + return (0); + } + + msgbuf_drain(msgbuf, n); + + return (1); +} + +void +ibuf_free(struct ibuf *buf) +{ + if (buf == NULL) + return; + freezero(buf->buf, buf->size); + free(buf); +} + +void +msgbuf_init(struct msgbuf *msgbuf) +{ + msgbuf->queued = 0; + msgbuf->fd = -1; + TAILQ_INIT(&msgbuf->bufs); +} + +void +msgbuf_drain(struct msgbuf *msgbuf, size_t n) +{ + struct ibuf *buf, *next; + + for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0; + buf = next) { + next = TAILQ_NEXT(buf, entry); + if (buf->rpos + n >= buf->wpos) { + n -= buf->wpos - buf->rpos; + ibuf_dequeue(msgbuf, buf); + } else { + buf->rpos += n; + n = 0; + } + } +} + +void +msgbuf_clear(struct msgbuf *msgbuf) +{ + struct ibuf *buf; + + while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL) + ibuf_dequeue(msgbuf, buf); +} + +int +msgbuf_write(struct msgbuf *msgbuf) +{ + struct iovec iov[IOV_MAX]; + struct ibuf *buf; + unsigned int i = 0; + ssize_t n; + struct msghdr msg; + struct cmsghdr *cmsg; + union { + struct cmsghdr hdr; + char buf[CMSG_SPACE(sizeof(int))]; + } cmsgbuf; + + memset(&iov, 0, sizeof(iov)); + memset(&msg, 0, sizeof(msg)); + memset(&cmsgbuf, 0, sizeof(cmsgbuf)); + TAILQ_FOREACH(buf, &msgbuf->bufs, entry) { + if (i >= IOV_MAX) + break; + iov[i].iov_base = buf->buf + buf->rpos; + iov[i].iov_len = buf->wpos - buf->rpos; + i++; + if (buf->fd != -1) + break; + } + + msg.msg_iov = iov; + msg.msg_iovlen = i; + + if (buf != NULL && buf->fd != -1) { + msg.msg_control = (caddr_t)&cmsgbuf.buf; + msg.msg_controllen = sizeof(cmsgbuf.buf); + cmsg = CMSG_FIRSTHDR(&msg); + cmsg->cmsg_len = CMSG_LEN(sizeof(int)); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SCM_RIGHTS; + *(int *)(void *)CMSG_DATA(cmsg) = buf->fd; + } + +again: + if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) { + if (errno == EINTR) + goto again; + if (errno == ENOBUFS) + errno = EAGAIN; + return (-1); + } + + if (n == 0) { /* connection closed */ + errno = 0; + return (0); + } + + /* + * assumption: fd got sent if sendmsg sent anything + * this works because fds are passed one at a time + */ + if (buf != NULL && buf->fd != -1) { + close(buf->fd); + buf->fd = -1; + } + + msgbuf_drain(msgbuf, n); + + return (1); +} + +static void +ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf) +{ + TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry); + msgbuf->queued++; +} + +static void +ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf) +{ + TAILQ_REMOVE(&msgbuf->bufs, buf, entry); + + if (buf->fd != -1) + close(buf->fd); + + msgbuf->queued--; + ibuf_free(buf); +} diff --git a/openbsd-compat/imsg.c b/openbsd-compat/imsg.c new file mode 100644 index 00000000..98a3e7c7 --- /dev/null +++ b/openbsd-compat/imsg.c @@ -0,0 +1,304 @@ +/* $OpenBSD: imsg.c,v 1.16 2017/12/14 09:27:44 kettenis Exp $ */ + +/* + * Copyright (c) 2003, 2004 Henning Brauer + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include "imsg.h" + +static int imsg_fd_overhead = 0; + +static int imsg_get_fd(struct imsgbuf *); + +void +imsg_init(struct imsgbuf *ibuf, int fd) +{ + msgbuf_init(&ibuf->w); + memset(&ibuf->r, 0, sizeof(ibuf->r)); + ibuf->fd = fd; + ibuf->w.fd = fd; + ibuf->pid = getpid(); + TAILQ_INIT(&ibuf->fds); +} + +ssize_t +imsg_read(struct imsgbuf *ibuf) +{ + struct msghdr msg; + struct cmsghdr *cmsg; + union { + struct cmsghdr hdr; + char buf[CMSG_SPACE(sizeof(int) * 1)]; + } cmsgbuf; + struct iovec iov; + ssize_t n = -1; + int fd; + struct imsg_fd *ifd; + + memset(&msg, 0, sizeof(msg)); + memset(&cmsgbuf, 0, sizeof(cmsgbuf)); + + iov.iov_base = ibuf->r.buf + ibuf->r.wpos; + iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos; + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + msg.msg_control = &cmsgbuf.buf; + msg.msg_controllen = sizeof(cmsgbuf.buf); + + if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL) + return (-1); + +again: + if (getdtablecount() + imsg_fd_overhead + + (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int)) + >= getdtablesize()) { + errno = EAGAIN; + free(ifd); + return (-1); + } + + if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) { + if (errno == EINTR) + goto again; + goto fail; + } + + ibuf->r.wpos += n; + + for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; + cmsg = CMSG_NXTHDR(&msg, cmsg)) { + if (cmsg->cmsg_level == SOL_SOCKET && + cmsg->cmsg_type == SCM_RIGHTS) { + int i; + int j; + + /* + * We only accept one file descriptor. Due to C + * padding rules, our control buffer might contain + * more than one fd, and we must close them. + */ + j = ((char *)cmsg + cmsg->cmsg_len - + (char *)CMSG_DATA(cmsg)) / sizeof(int); + for (i = 0; i < j; i++) { + fd = ((int *)(void *)CMSG_DATA(cmsg))[i]; + if (ifd != NULL) { + ifd->fd = fd; + TAILQ_INSERT_TAIL(&ibuf->fds, ifd, + entry); + ifd = NULL; + } else + close(fd); + } + } + /* we do not handle other ctl data level */ + } + +fail: + free(ifd); + return (n); +} + +ssize_t +imsg_get(struct imsgbuf *ibuf, struct imsg *imsg) +{ + size_t av, left, datalen; + + av = ibuf->r.wpos; + + if (IMSG_HEADER_SIZE > av) + return (0); + + memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr)); + if (imsg->hdr.len < IMSG_HEADER_SIZE || + imsg->hdr.len > MAX_IMSGSIZE) { + errno = ERANGE; + return (-1); + } + if (imsg->hdr.len > av) + return (0); + datalen = imsg->hdr.len - IMSG_HEADER_SIZE; + ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE; + if (datalen == 0) + imsg->data = NULL; + else if ((imsg->data = malloc(datalen)) == NULL) + return (-1); + + if (imsg->hdr.flags & IMSGF_HASFD) + imsg->fd = imsg_get_fd(ibuf); + else + imsg->fd = -1; + + memcpy(imsg->data, ibuf->r.rptr, datalen); + + if (imsg->hdr.len < av) { + left = av - imsg->hdr.len; + memmove(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left); + ibuf->r.wpos = left; + } else + ibuf->r.wpos = 0; + + return (datalen + IMSG_HEADER_SIZE); +} + +int +imsg_compose(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid, + int fd, const void *data, uint16_t datalen) +{ + struct ibuf *wbuf; + + if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL) + return (-1); + + if (imsg_add(wbuf, data, datalen) == -1) + return (-1); + + wbuf->fd = fd; + + imsg_close(ibuf, wbuf); + + return (1); +} + +int +imsg_composev(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid, + int fd, const struct iovec *iov, int iovcnt) +{ + struct ibuf *wbuf; + int i, datalen = 0; + + for (i = 0; i < iovcnt; i++) + datalen += iov[i].iov_len; + + if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL) + return (-1); + + for (i = 0; i < iovcnt; i++) + if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1) + return (-1); + + wbuf->fd = fd; + + imsg_close(ibuf, wbuf); + + return (1); +} + +/* ARGSUSED */ +struct ibuf * +imsg_create(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid, + uint16_t datalen) +{ + struct ibuf *wbuf; + struct imsg_hdr hdr; + + datalen += IMSG_HEADER_SIZE; + if (datalen > MAX_IMSGSIZE) { + errno = ERANGE; + return (NULL); + } + + hdr.type = type; + hdr.flags = 0; + hdr.peerid = peerid; + if ((hdr.pid = pid) == 0) + hdr.pid = ibuf->pid; + if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) { + return (NULL); + } + if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1) + return (NULL); + + return (wbuf); +} + +int +imsg_add(struct ibuf *msg, const void *data, uint16_t datalen) +{ + if (datalen) + if (ibuf_add(msg, data, datalen) == -1) { + ibuf_free(msg); + return (-1); + } + return (datalen); +} + +void +imsg_close(struct imsgbuf *ibuf, struct ibuf *msg) +{ + struct imsg_hdr *hdr; + + hdr = (struct imsg_hdr *)(void *)msg->buf; + + hdr->flags &= ~IMSGF_HASFD; + if (msg->fd != -1) + hdr->flags |= IMSGF_HASFD; + + hdr->len = (uint16_t)msg->wpos; + + ibuf_close(&ibuf->w, msg); +} + +void +imsg_free(struct imsg *imsg) +{ + freezero(imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE); +} + +static int +imsg_get_fd(struct imsgbuf *ibuf) +{ + int fd; + struct imsg_fd *ifd; + + if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL) + return (-1); + + fd = ifd->fd; + TAILQ_REMOVE(&ibuf->fds, ifd, entry); + free(ifd); + + return (fd); +} + +int +imsg_flush(struct imsgbuf *ibuf) +{ + while (ibuf->w.queued) + if (msgbuf_write(&ibuf->w) <= 0) + return (-1); + return (0); +} + +void +imsg_clear(struct imsgbuf *ibuf) +{ + int fd; + + msgbuf_clear(&ibuf->w); + while ((fd = imsg_get_fd(ibuf)) != -1) + close(fd); +} diff --git a/openbsd-compat/imsg.h b/openbsd-compat/imsg.h new file mode 100644 index 00000000..5b092cfc --- /dev/null +++ b/openbsd-compat/imsg.h @@ -0,0 +1,113 @@ +/* $OpenBSD: imsg.h,v 1.5 2019/01/20 02:50:03 bcook Exp $ */ + +/* + * Copyright (c) 2006, 2007 Pierre-Yves Ritschard + * Copyright (c) 2006, 2007, 2008 Reyk Floeter + * Copyright (c) 2003, 2004 Henning Brauer + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef _IMSG_H_ +#define _IMSG_H_ + +#include + +#define IBUF_READ_SIZE 65535 +#define IMSG_HEADER_SIZE sizeof(struct imsg_hdr) +#define MAX_IMSGSIZE 16384 + +struct ibuf { + TAILQ_ENTRY(ibuf) entry; + unsigned char *buf; + size_t size; + size_t max; + size_t wpos; + size_t rpos; + int fd; +}; + +struct msgbuf { + TAILQ_HEAD(, ibuf) bufs; + uint32_t queued; + int fd; +}; + +struct ibuf_read { + unsigned char buf[IBUF_READ_SIZE]; + unsigned char *rptr; + size_t wpos; +}; + +struct imsg_fd { + TAILQ_ENTRY(imsg_fd) entry; + int fd; +}; + +struct imsgbuf { + TAILQ_HEAD(, imsg_fd) fds; + struct ibuf_read r; + struct msgbuf w; + int fd; + pid_t pid; +}; + +#define IMSGF_HASFD 1 + +struct imsg_hdr { + uint32_t type; + uint16_t len; + uint16_t flags; + uint32_t peerid; + uint32_t pid; +}; + +struct imsg { + struct imsg_hdr hdr; + int fd; + void *data; +}; + + +/* buffer.c */ +struct ibuf *ibuf_open(size_t); +struct ibuf *ibuf_dynamic(size_t, size_t); +int ibuf_add(struct ibuf *, const void *, size_t); +void *ibuf_reserve(struct ibuf *, size_t); +void *ibuf_seek(struct ibuf *, size_t, size_t); +size_t ibuf_size(struct ibuf *); +size_t ibuf_left(struct ibuf *); +void ibuf_close(struct msgbuf *, struct ibuf *); +int ibuf_write(struct msgbuf *); +void ibuf_free(struct ibuf *); +void msgbuf_init(struct msgbuf *); +void msgbuf_clear(struct msgbuf *); +int msgbuf_write(struct msgbuf *); +void msgbuf_drain(struct msgbuf *, size_t); + +/* imsg.c */ +void imsg_init(struct imsgbuf *, int); +ssize_t imsg_read(struct imsgbuf *); +ssize_t imsg_get(struct imsgbuf *, struct imsg *); +int imsg_compose(struct imsgbuf *, uint32_t, uint32_t, pid_t, int, + const void *, uint16_t); +int imsg_composev(struct imsgbuf *, uint32_t, uint32_t, pid_t, int, + const struct iovec *, int); +struct ibuf *imsg_create(struct imsgbuf *, uint32_t, uint32_t, pid_t, uint16_t); +int imsg_add(struct ibuf *, const void *, uint16_t); +void imsg_close(struct imsgbuf *, struct ibuf *); +void imsg_free(struct imsg *); +int imsg_flush(struct imsgbuf *); +void imsg_clear(struct imsgbuf *); + +#endif diff --git a/openbsd-compat/open.c b/openbsd-compat/open.c new file mode 100644 index 00000000..b6454d16 --- /dev/null +++ b/openbsd-compat/open.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2020 Christian Weisgerber + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include + +#include "openbsd-compat.h" +#undef open +#undef openat + +/* + * POSIX mandates that open(symlink, O_NOFOLLOW) fail with errno == ELOOP. + * FreeBSD chooses to deviate from this, but Got depends on it. + */ +int +open_posix(const char *path, int flags, ...) +{ + va_list ap; + mode_t mode; + int ret; + + if (flags & O_CREAT) { + va_start(ap, flags); + mode = va_arg(ap, int); + va_end(ap); + ret = open(path, flags, mode); + } else + ret = open(path, flags); + + if (ret == -1 && (flags & O_NOFOLLOW) && errno == EMLINK) + errno = ELOOP; + + return (ret); +} + +int +openat_posix(int fd, const char *path, int flags, ...) +{ + va_list ap; + mode_t mode; + int ret; + + if (flags & O_CREAT) { + va_start(ap, flags); + mode = va_arg(ap, int); + va_end(ap); + ret = openat(fd, path, flags, mode); + } else + ret = openat(fd, path, flags); + + if (ret == -1 && (flags & O_NOFOLLOW) && errno == EMLINK) + errno = ELOOP; + + return (ret); +} diff --git a/openbsd-compat/openbsd-compat.h b/openbsd-compat/openbsd-compat.h new file mode 100644 index 00000000..160184ac --- /dev/null +++ b/openbsd-compat/openbsd-compat.h @@ -0,0 +1,87 @@ +/* + * Compatibility mappings for system headers and + * prototypes for functions in libopenbsd-compat. + */ + +#ifndef _OPENBSD_COMPAT_H_ +#define _OPENBSD_COMPAT_H_ + +/* + * + */ +#define __dead __dead2 + +/* + * + */ +#define SIMPLEQ_HEAD(name, type) \ + STAILQ_HEAD(name, type) +#define SIMPLEQ_HEAD_INITIALIZER(head) \ + STAILQ_HEAD_INITIALIZER(head) +#define SIMPLEQ_ENTRY(type) \ + STAILQ_ENTRY(type) +#define SIMPLEQ_FIRST(head) \ + STAILQ_FIRST(head) +#define SIMPLEQ_END(head) \ + NULL +#define SIMPLEQ_EMPTY(head) \ + STAILQ_EMPTY(head) +#define SIMPLEQ_NEXT(elm, field) \ + STAILQ_NEXT(elm, field) +#define SIMPLEQ_FOREACH(var, head, field) \ + STAILQ_FOREACH(var, head, field) +#define SIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \ + STAILQ_FOREACH_SAFE(var, head, field, tvar) +#define SIMPLEQ_INIT(head) \ + STAILQ_INIT(head) +#define SIMPLEQ_INSERT_HEAD(head, elm, field) \ + STAILQ_INSERT_HEAD(head, elm, field) +#define SIMPLEQ_INSERT_TAIL(head, elm, field) \ + STAILQ_INSERT_TAIL(head, elm, field) +#define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) \ + STAILQ_INSERT_AFTER(head, listelm, elm, field) +#define SIMPLEQ_REMOVE_HEAD(head, field) \ + STAILQ_REMOVE_HEAD(head, field) +#define SIMPLEQ_REMOVE_AFTER(head, elm, field) \ + STAILQ_REMOVE_AFTER(head, elm, field) +#define SIMPLEQ_CONCAT(head1, head2) \ + STAILQ_CONCAT(head1, head2) + +/* + * + */ +#define open(...) open_posix(__VA_ARGS__) +#define openat(...) openat_posix(__VA_ARGS__) + +int open_posix(const char *path, int flags, ...); +int openat_posix(int fd, const char *path, int flags, ...); + +/* + * + */ +#undef basename +#undef dirname +#define basename(path) basename_const(path) +#define dirname(path) dirname_const(path) + +char *basename(const char *); +char *dirname(const char *); + +/* + * + */ +void freezero(void *, size_t); +void *recallocarray(void *, size_t, size_t, size_t); + +/* + * + */ +int getdtablecount(void); + +/* void -> int */ +#define closefrom(fd) (closefrom(fd), 0) + +#define pledge(promises, execpromises) 0 +#define unveil(path, permissions) 0 + +#endif /* _OPENBSD_COMPAT_H_ */ diff --git a/openbsd-compat/recallocarray.c b/openbsd-compat/recallocarray.c new file mode 100644 index 00000000..da903c30 --- /dev/null +++ b/openbsd-compat/recallocarray.c @@ -0,0 +1,82 @@ +/* $OpenBSD: recallocarray.c,v 1.1 2017/03/06 18:44:21 otto Exp $ */ +/* + * Copyright (c) 2008, 2017 Otto Moerbeek + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include + +#include "openbsd-compat.h" + +/* + * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX + * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW + */ +#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) + +void * +recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size) +{ + size_t oldsize, newsize; + void *newptr; + + if (ptr == NULL) + return calloc(newnmemb, size); + + if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && + newnmemb > 0 && SIZE_MAX / newnmemb < size) { + errno = ENOMEM; + return NULL; + } + newsize = newnmemb * size; + + if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && + oldnmemb > 0 && SIZE_MAX / oldnmemb < size) { + errno = EINVAL; + return NULL; + } + oldsize = oldnmemb * size; + + /* + * Don't bother too much if we're shrinking just a bit, + * we do not shrink for series of small steps, oh well. + */ + if (newsize <= oldsize) { + size_t d = oldsize - newsize; + + if (d < oldsize / 2 && d < (size_t)getpagesize()) { + memset((char *)ptr + newsize, 0, d); + return ptr; + } + } + + newptr = malloc(newsize); + if (newptr == NULL) + return NULL; + + if (newsize > oldsize) { + memcpy(newptr, ptr, oldsize); + memset((char *)newptr + oldsize, 0, newsize - oldsize); + } else + memcpy(newptr, ptr, newsize); + + explicit_bzero(ptr, oldsize); + free(ptr); + + return newptr; +} diff --git a/openbsd-compat/sha1.h b/openbsd-compat/sha1.h new file mode 100644 index 00000000..bfa1dab9 --- /dev/null +++ b/openbsd-compat/sha1.h @@ -0,0 +1,14 @@ +#ifndef _OPENBSD_COMPAT_SHA1_H_ +#define _OPENBSD_COMPAT_SHA1_H_ + +#include + +#define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH +#define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1) + +#define SHA1_CTX SHA_CTX +#define SHA1Init SHA1_Init +#define SHA1Update SHA1_Update +#define SHA1Final SHA1_Final + +#endif diff --git a/openbsd-compat/util.h b/openbsd-compat/util.h new file mode 100644 index 00000000..d985406b --- /dev/null +++ b/openbsd-compat/util.h @@ -0,0 +1,19 @@ +#ifndef _OPENBSD_COMPAT_UTIL_H_ +#define _OPENBSD_COMPAT_UTIL_H_ + +#include + +/* + * fmt_scaled(3) specific flags. + */ +#define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */ + +#define fmt_scaled(number, result) ({ \ + int64_t num = number; \ + int r = humanize_number(result, \ + FMT_SCALED_STRSIZE - (num < 0 ? 0 : 1), \ + num, "", HN_AUTOSCALE, HN_NOSPACE | HN_B); \ + r == -1 ? -1 : 0; \ +}) + +#endif diff --git a/regress/Makefile b/regress/Makefile index 5c202631..2c237cf3 100644 --- a/regress/Makefile +++ b/regress/Makefile @@ -1,3 +1,5 @@ SUBDIR = cmdline delta idset path fetch +SUBDIR_TARGETS+= regress + .include diff --git a/regress/cmdline/Makefile b/regress/cmdline/Makefile index e32b5a14..50c2cc5c 100644 --- a/regress/cmdline/Makefile +++ b/regress/cmdline/Makefile @@ -80,4 +80,6 @@ fetch: tree: ./tree.sh -q -r "$(GOT_TEST_ROOT)" -.include +regress: ${REGRESS_TARGETS} .PHONY .SILENT + +.include diff --git a/regress/cmdline/common.sh b/regress/cmdline/common.sh index 9c914803..39f68271 100644 --- a/regress/cmdline/common.sh +++ b/regress/cmdline/common.sh @@ -25,6 +25,20 @@ export GOT_TEST_ROOT="/tmp" export MALLOC_OPTIONS=S +# compat wrapper +sed() +( + for i; do + arg=$1 + shift + case $arg in + -i) set - "$@" "$arg" '' ;; + *) set - "$@" "$arg" ;; + esac + done + exec sed "$@" +) + git_init() { git init -q "$1" diff --git a/regress/delta/Makefile b/regress/delta/Makefile index 4ce5ebd3..51fafb6d 100644 --- a/regress/delta/Makefile +++ b/regress/delta/Makefile @@ -4,11 +4,12 @@ PROG = delta_test SRCS = delta.c error.c opentemp.c path.c inflate.c sha1.c delta_test.c CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib -LDADD = -lz +LDFLAGS += -L${.OBJDIR}/../../openbsd-compat +LIBADD = z -NOMAN = yes +MAN= -run-regress-delta_test: +regress: ${PROG} .PHONY ${.OBJDIR}/delta_test -q -.include +.include diff --git a/regress/delta/delta_test.c b/regress/delta/delta_test.c index 7d362ee8..17550551 100644 --- a/regress/delta/delta_test.c +++ b/regress/delta/delta_test.c @@ -23,6 +23,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_opentemp.h" #include "got_path.h" @@ -59,7 +61,7 @@ static int delta_apply(void) { const struct got_error *err = NULL; - int i; + size_t i; FILE *result_file; result_file = got_opentemp(); diff --git a/regress/fetch/Makefile b/regress/fetch/Makefile index f9e0d6b0..5904fac1 100644 --- a/regress/fetch/Makefile +++ b/regress/fetch/Makefile @@ -7,11 +7,12 @@ SRCS = error.c privsep.c reference.c sha1.c object.c object_parse.c path.c \ fetch.c gotconfig.c fetch_test.c CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib -LDADD = -lutil -lz +LDFLAGS += -L${.OBJDIR}/../../openbsd-compat +LIBADD = util z -NOMAN = yes +MAN= -run-regress-fetch_test: +regress: ${PROG} .PHONY ${.OBJDIR}/fetch_test -q -.include +.include diff --git a/regress/fetch/fetch_test.c b/regress/fetch/fetch_test.c index f8fea68e..410ee32b 100644 --- a/regress/fetch/fetch_test.c +++ b/regress/fetch/fetch_test.c @@ -27,6 +27,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" #include "got_path.h" @@ -127,7 +129,7 @@ fetch_parse_uri(void) "ssh", "127.0.0.1", NULL, "22/git/myrepo", "myrepo", GOT_ERR_OK }, }; - int i; + size_t i; for (i = 0; i < nitems(test_data); i++) { const char *uri = test_data[i].uri; diff --git a/regress/idset/Makefile b/regress/idset/Makefile index 6dc6aa8d..6ccfda04 100644 --- a/regress/idset/Makefile +++ b/regress/idset/Makefile @@ -5,11 +5,12 @@ SRCS = error.c sha1.c object_idset.c inflate.c path.c object_parse.c \ idset_test.c CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib -LDADD = -lutil -lz +LDFLAGS += -L${.OBJDIR}/../../openbsd-compat +LIBADD = util z -NOMAN = yes +MAN= -run-regress-idset_test: +regress: ${PROG} .PHONY ${.OBJDIR}/idset_test -q -.include +.include diff --git a/regress/idset/idset_test.c b/regress/idset/idset_test.c index 64c131e2..08efd3d9 100644 --- a/regress/idset/idset_test.c +++ b/regress/idset/idset_test.c @@ -26,6 +26,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_object.h" diff --git a/regress/path/Makefile b/regress/path/Makefile index 2ea8b586..51a14f9d 100644 --- a/regress/path/Makefile +++ b/regress/path/Makefile @@ -4,11 +4,12 @@ PROG = path_test SRCS = error.c path.c sha1.c path_test.c CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib -LDADD = -lutil -lz +LDFLAGS += -L${.OBJDIR}/../../openbsd-compat +LIBADD = util z -NOMAN = yes +MAN= -run-regress-path_test: +regress: ${PROG} .PHONY ${.OBJDIR}/path_test -q -.include +.include diff --git a/regress/path/path_test.c b/regress/path/path_test.c index 9e6ab487..e370bb2a 100644 --- a/regress/path/path_test.c +++ b/regress/path/path_test.c @@ -23,6 +23,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_error.h" #include "got_path.h" @@ -79,7 +81,7 @@ path_cmp(void) { "/bar.sub.sub2", "/bar", 1 }, { "/bar/sub/sub2", "/bar.c", -1 }, }; - int i; + size_t i; for (i = 0; i < nitems(test_data); i++) { const char *path1 = test_data[i].path1; @@ -139,7 +141,7 @@ path_list(void) const struct got_error *err = NULL; struct got_pathlist_head paths; struct got_pathlist_entry *pe; - int i; + size_t i; TAILQ_INIT(&paths); for (i = 0; i < nitems(path_list_input); i++) { diff --git a/tog/Makefile b/tog/Makefile index 41af3bef..2d78c31e 100644 --- a/tog/Makefile +++ b/tog/Makefile @@ -13,20 +13,16 @@ SRCS= tog.c blame.c commit_graph.c delta.c diff.c \ MAN = ${PROG}.1 CPPFLAGS = -I${.CURDIR}/../include -I${.CURDIR}/../lib +LDFLAGS += -L${.OBJDIR}/../openbsd-compat .if defined(PROFILE) -LDADD = -lpanel_p -lncursesw_p -lutil_p -lz_p -lpthread_p -lc_p +LIBADD = panel_p ncursesw_p util_p z_p pthread_p c_p .else -LDADD = -lpanel -lncursesw -lutil -lz -lpthread +LIBADD = panel ncursesw util z pthread .endif -DPADD = ${LIBZ} ${LIBUTIL} .if ${GOT_RELEASE} != "Yes" -NOMAN = Yes +MAN= .endif -realinstall: - ${INSTALL} ${INSTALL_COPY} -o ${BINOWN} -g ${BINGRP} \ - -m ${BINMODE} ${PROG} ${BINDIR}/${PROG} - .include diff --git a/tog/tog.c b/tog/tog.c index 1b0d1ec0..9c788250 100644 --- a/tog/tog.c +++ b/tog/tog.c @@ -41,6 +41,8 @@ #include #include +#include "openbsd-compat.h" + #include "got_version.h" #include "got_error.h" #include "got_object.h" @@ -5486,7 +5488,7 @@ done: static void list_commands(FILE *fp) { - int i; + size_t i; fprintf(fp, "commands:"); for (i = 0; i < nitems(tog_commands); i++) { @@ -5674,7 +5676,7 @@ main(int argc, char *argv[]) argc = 1; cmd_argv = make_argv(argc, cmd->name); } else { - int i; + size_t i; /* Did the user specify a command? */ for (i = 0; i < nitems(tog_commands); i++) {