From owner-svn-src-head@FreeBSD.ORG Sun Apr 15 15:13:36 2012 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id D44A11065780; Sun, 15 Apr 2012 15:13:36 +0000 (UTC) (envelope-from bapt@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id B0D6D8FC08; Sun, 15 Apr 2012 15:13:36 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id q3FFDah1089697; Sun, 15 Apr 2012 15:13:36 GMT (envelope-from bapt@svn.freebsd.org) Received: (from bapt@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id q3FFDaTu089692; Sun, 15 Apr 2012 15:13:36 GMT (envelope-from bapt@svn.freebsd.org) Message-Id: <201204151513.q3FFDaTu089692@svn.freebsd.org> From: Baptiste Daroussin Date: Sun, 15 Apr 2012 15:13:36 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r234313 - in head/usr.sbin: . pkg X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 15 Apr 2012 15:13:37 -0000 Author: bapt Date: Sun Apr 15 15:13:36 2012 New Revision: 234313 URL: http://svn.freebsd.org/changeset/base/234313 Log: add usr.sbin/pkg which is a bootstrap tool for pkgng. it respects PACKAGESITE, PACKAGEROOT, and a new environment variable ABI (if a user want to use a different API from the base one for its packages) it has no man page on purpose to avoid hidding the pkg(8) man page from the pkgng package. for now uses pkgbeta.FreeBSD.org as default mirror to find its package it respects MK_PKGTOOLS Approved by: des (mentor) Added: head/usr.sbin/pkg/ head/usr.sbin/pkg/Makefile (contents, props changed) head/usr.sbin/pkg/elf_tables.h (contents, props changed) head/usr.sbin/pkg/pkg.c (contents, props changed) Modified: head/usr.sbin/Makefile Modified: head/usr.sbin/Makefile ============================================================================== --- head/usr.sbin/Makefile Sun Apr 15 15:13:29 2012 (r234312) +++ head/usr.sbin/Makefile Sun Apr 15 15:13:36 2012 (r234313) @@ -256,6 +256,7 @@ SUBDIR+= ftp-proxy .if ${MK_PKGTOOLS} != "no" SUBDIR+= pkg_install +SUBDIR+= pkg .endif # XXX MK_TOOLCHAIN? Added: head/usr.sbin/pkg/Makefile ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/usr.sbin/pkg/Makefile Sun Apr 15 15:13:36 2012 (r234313) @@ -0,0 +1,9 @@ +# $FreeBSD$ + +PROG= pkg +SRC= pkg.c + +NO_MAN= yes +LDADD= -larchive -lelf -lfetch + +.include Added: head/usr.sbin/pkg/elf_tables.h ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/usr.sbin/pkg/elf_tables.h Sun Apr 15 15:13:36 2012 (r234313) @@ -0,0 +1,79 @@ +/*- + * Copyright (c) 2012 Olivier Houchard + * Copyright (c) 2012 Baptiste Daroussin + * 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. + * + * $FreeBSD$ + */ + +#ifndef ELF_TABLES_H_ +#define ELF_TABLES_H_ +struct _elf_corres { + int elf_nb; + const char *string; +}; + +struct _elf_corres mach_corres[] = { + { EM_386, "x86" }, + { EM_AMD64, "x86" }, + { EM_ARM, "arm" }, + { EM_MIPS, "mips" }, + { EM_PPC, "powerpc" }, + { EM_PPC64, "powerpc" }, + { EM_SPARCV9, "sparc64" }, + { EM_IA_64, "ia64" }, + { -1, NULL }, +}; + +struct _elf_corres wordsize_corres[] = { + { ELFCLASS32, "32" }, + { ELFCLASS64, "64" }, + { -1, NULL}, +}; + +struct _elf_corres endian_corres[] = { + { ELFDATA2MSB, "eb" }, + { ELFDATA2LSB, "el" }, + { -1, NULL} +}; + +struct _elf_corres os_corres[] = { + { ELFOSABI_FREEBSD, "freebsd" }, + { -1, NULL } +}; + +#define EF_MIPS_ABI 0x0000F000 +#define E_MIPS_ABI_O32 0x00001000 +#define E_MIPS_ABI_N32 0x00000020 + +#define EF_ARM_NEW_ABI 0x80 +#define EF_ARM_OLD_ABI 0x100 + +#define EF_ARM_SOFT_FLOAT 0x200 +#define EF_ARM_VFP_FLOAT 0x400 + +#define NT_VERSION 1 +#define NT_ARCH 2 + +#endif /* ELF_TABLES_H_ */ Added: head/usr.sbin/pkg/pkg.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/usr.sbin/pkg/pkg.c Sun Apr 15 15:13:36 2012 (r234313) @@ -0,0 +1,383 @@ +/*- + * Copyright (c) 2012 Baptiste Daroussin + * 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 +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "elf_tables.h" + +#define _LOCALBASE "/usr/local" +#define _PKGS_URL "http://pkgbeta.FreeBSD.org" +#define _DEFAULT_TMP "/tmp" + +static const char * +elf_corres_to_string(struct _elf_corres* m, int e) +{ + int i = 0; + + for (i = 0; m[i].string != NULL; i++) + if (m[i].elf_nb == e) + return (m[i].string); + + return ("unknown"); +} + +static int +pkg_get_myabi(char *dest, size_t sz) +{ + Elf *elf; + GElf_Ehdr elfhdr; + GElf_Shdr shdr; + Elf_Data *data; + Elf_Note note; + Elf_Scn *scn; + char *src, *osname; + const char *abi; + int fd, i, ret; + uint32_t version; + + version = 0; + ret = 0; + scn = NULL; + abi = NULL; + + if (elf_version(EV_CURRENT) == EV_NONE) { + warnx("ELF library initialization failed: %s", elf_errmsg(-1)); + return -1; + } + + if ((fd = open("/bin/sh", O_RDONLY)) < 0) { + warn("open()"); + return -1; + } + + if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { + ret = -1; + warnx("elf_begin() failed: %s.", elf_errmsg(-1)); + goto cleanup; + } + + if (gelf_getehdr(elf, &elfhdr) == NULL) { + ret = -1; + warn("getehdr() failed: %s.", elf_errmsg(-1)); + goto cleanup; + } + + while ((scn = elf_nextscn(elf, scn)) != NULL) { + if (gelf_getshdr(scn, &shdr) != &shdr) { + ret = -1; + warn("getshdr() failed: %s.", elf_errmsg(-1)); + goto cleanup; + } + + if (shdr.sh_type == SHT_NOTE) + break; + } + + if (scn == NULL) { + ret = -1; + warn("fail to get the note section"); + goto cleanup; + } + + data = elf_getdata(scn, NULL); + src = data->d_buf; + while (1) { + memcpy(¬e, src, sizeof(Elf_Note)); + src += sizeof(Elf_Note); + if (note.n_type == NT_VERSION) + break; + src += note.n_namesz + note.n_descsz; + } + osname = src; + src += note.n_namesz; + if (elfhdr.e_ident[EI_DATA] == ELFDATA2MSB) + version = be32dec(src); + else + version = le32dec(src); + + for (i = 0; osname[i] != '\0'; i++) + osname[i] = (char)tolower(osname[i]); + + snprintf(dest, sz, "%s:%d:%s:%s", + osname, + version / 100000, + elf_corres_to_string(mach_corres, (int) elfhdr.e_machine), + elf_corres_to_string(wordsize_corres, + (int)elfhdr.e_ident[EI_CLASS])); + + switch (elfhdr.e_machine) { + case EM_ARM: + snprintf(dest + strlen(dest), sz - strlen(dest), + ":%s:%s:%s", + elf_corres_to_string(endian_corres, + (int) elfhdr.e_ident[EI_DATA]), + (elfhdr.e_flags & EF_ARM_NEW_ABI) > 0 ? + "eabi" : "oabi", + (elfhdr.e_flags & EF_ARM_VFP_FLOAT) > 0 ? + "softfp" : "vfp"); + break; + case EM_MIPS: + /* + * this is taken from binutils sources: + * include/elf/mips.h + * mapping is figured out from binutils: + * gas/config/tc-mips.c + */ + switch (elfhdr.e_flags & EF_MIPS_ABI) { + case E_MIPS_ABI_O32: + abi = "o32"; + break; + case E_MIPS_ABI_N32: + abi = "n32"; + break; + default: + if (elfhdr.e_ident[EI_DATA] == + ELFCLASS32) + abi = "o32"; + else if (elfhdr.e_ident[EI_DATA] == + ELFCLASS64) + abi = "n64"; + break; + } + snprintf(dest + strlen(dest), sz - strlen(dest), + ":%s:%s", + elf_corres_to_string(endian_corres, + (int) elfhdr.e_ident[EI_DATA]), + abi); + break; + } + +cleanup: + if (elf != NULL) + elf_end(elf); + + close(fd); + return (ret); +} + +static int +extract_pkg_static(int fd, char *p, int sz) +{ + struct archive *a; + struct archive_entry *ae; + char *end; + int ret, r; + + ret = 0; + a = archive_read_new(); + archive_read_support_compression_all(a); + archive_read_support_format_tar(a); + + lseek(fd, 0, 0); + + if (archive_read_open_fd(a, fd, 4096) != ARCHIVE_OK) { + warnx("archive_read_open_fd: %s", + archive_error_string(a)); + ret = -1; + goto cleanup; + } + + ae = NULL; + while ((r = archive_read_next_header(a, &ae)) == ARCHIVE_OK) { + end = strrchr(archive_entry_pathname(ae), '/'); + if (end == NULL) + continue; + + if (strcmp(end, "/pkg-static") == 0) { + r = archive_read_extract(a, ae, + ARCHIVE_EXTRACT_OWNER |ARCHIVE_EXTRACT_PERM| + ARCHIVE_EXTRACT_TIME |ARCHIVE_EXTRACT_ACL | + ARCHIVE_EXTRACT_FFLAGS|ARCHIVE_EXTRACT_XATTR); + snprintf(p, sz, archive_entry_pathname(ae)); + break; + } + } + + if (r != ARCHIVE_OK) { + warnx("fail to extract pkg-static"); + ret = -1; + } + +cleanup: + archive_read_finish(a); + return ret; + +} + +static int +install_pkg_static(char *path, char *pkgpath) +{ + int pstat; + pid_t pid; + + switch ((pid = fork())) { + case -1: + return (-1); + case 0: + execl(path, "pkg-static", "add", pkgpath, (char *)NULL); + _exit(1); /* NOT REACHED */ + default: + break; + } + + while (waitpid(pid, &pstat, 0) == -1) { + if (errno != EINTR) + return (-1); + } + + return (WEXITSTATUS(pstat)); +} + +static int +bootstrap_pkg(void) +{ + struct url_stat st; + FILE *remote; + time_t begin_dl; + time_t now; + time_t last = 0; + char url[MAXPATHLEN]; + char abi[BUFSIZ]; + char tmppkg[MAXPATHLEN]; + char buf[10240]; + char pkgstatic[MAXPATHLEN]; + int fd, retry, ret; + off_t done, r; + + done = 0; + ret = 0; + retry = 3; + remote = NULL; + + printf("Bootstraping pkg please wait\n"); + + if (pkg_get_myabi(abi, MAXPATHLEN) != 0) { + warnx("fail to determine my abi"); + return -1; + } + + if (getenv("PACKAGESITE") != NULL) { + snprintf(url, MAXPATHLEN, "%s/pkg.txz", + getenv("PACKAGESITE")); + } else { + snprintf(url, MAXPATHLEN, "%s/%s/latest/Latest/pkg.txz", + getenv("PACKAGEROOT") ? getenv("PACKAGEROOT") : _PKGS_URL, + getenv("ABI") ? getenv("ABI") : abi); + } + + snprintf(tmppkg, MAXPATHLEN, "%s/pkg.txz.XXXXXX", + getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp"); + + if ((fd = mkstemp(tmppkg)) == -1) { + warn("mkstemp()"); + return -1; + } + + while (remote == NULL) { + remote = fetchXGetURL(url, &st, ""); + if (remote == NULL) { + --retry; + if (retry == 0) { + warnx("Error fetching %s: %s", url, + fetchLastErrString); + ret = 1; + goto cleanup; + } + sleep(1); + } + } + + begin_dl = time(NULL); + while (done < st.size) { + if ((r = fread(buf, 1, sizeof(buf), remote)) < 1) + break; + + if (write(fd, buf, r) != r) { + warn("write()"); + ret = -1; + goto cleanup; + } + + done += r; + now = time(NULL); + if (now > last || done == st.size) { + last = now; + } + } + + if (ferror(remote)) { + warnx("Error fetching %s: %s", url, + fetchLastErrString); + ret = 1; + goto cleanup; + } + + if ((ret = extract_pkg_static(fd, pkgstatic, MAXPATHLEN)) == 0) + ret = install_pkg_static(pkgstatic, tmppkg); + +cleanup: + close(fd); + unlink(tmppkg); + + return 0; +} + +int +main(__unused int argc, char * argv[]) +{ + char pkgpath[MAXPATHLEN]; + + snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg", + getenv("LOCALBASE") ? getenv("LOCALBASE"): _LOCALBASE); + + if (access(pkgpath, X_OK) == -1) + bootstrap_pkg(); + + execv(pkgpath, argv); + + return (EXIT_SUCCESS); +}