2 * Copyright (c) 2012 Baptiste Daroussin <bapt@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/elf_common.h>
32 #include <sys/endian.h>
36 #include <archive_entry.h>
50 #include "elf_tables.h"
51 #include "dns_utils.h"
53 #define _LOCALBASE "/usr/local"
54 #define _PKGS_URL "http://pkg.FreeBSD.org"
57 elf_corres_to_string(struct _elf_corres *m, int e)
61 for (i = 0; m[i].string != NULL; i++)
69 pkg_get_myabi(char *dest, size_t sz)
87 if (elf_version(EV_CURRENT) == EV_NONE) {
88 warnx("ELF library initialization failed: %s",
93 if ((fd = open("/bin/sh", O_RDONLY)) < 0) {
98 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
100 warnx("elf_begin() failed: %s.", elf_errmsg(-1));
104 if (gelf_getehdr(elf, &elfhdr) == NULL) {
106 warn("getehdr() failed: %s.", elf_errmsg(-1));
110 while ((scn = elf_nextscn(elf, scn)) != NULL) {
111 if (gelf_getshdr(scn, &shdr) != &shdr) {
113 warn("getshdr() failed: %s.", elf_errmsg(-1));
117 if (shdr.sh_type == SHT_NOTE)
123 warn("failed to get the note section");
127 data = elf_getdata(scn, NULL);
130 memcpy(¬e, src, sizeof(Elf_Note));
131 src += sizeof(Elf_Note);
132 if (note.n_type == NT_VERSION)
134 src += note.n_namesz + note.n_descsz;
137 src += note.n_namesz;
138 if (elfhdr.e_ident[EI_DATA] == ELFDATA2MSB)
139 version = be32dec(src);
141 version = le32dec(src);
143 for (i = 0; osname[i] != '\0'; i++)
144 osname[i] = (char)tolower(osname[i]);
146 snprintf(dest, sz, "%s:%d:%s:%s",
147 osname, version / 100000,
148 elf_corres_to_string(mach_corres, (int)elfhdr.e_machine),
149 elf_corres_to_string(wordsize_corres,
150 (int)elfhdr.e_ident[EI_CLASS]));
154 switch (elfhdr.e_machine) {
156 snprintf(dest + strlen(dest), sz - strlen(dest),
157 ":%s:%s:%s", elf_corres_to_string(endian_corres,
158 (int)elfhdr.e_ident[EI_DATA]),
159 (elfhdr.e_flags & EF_ARM_NEW_ABI) > 0 ?
161 (elfhdr.e_flags & EF_ARM_VFP_FLOAT) > 0 ?
166 * this is taken from binutils sources:
168 * mapping is figured out from binutils:
169 * gas/config/tc-mips.c
171 switch (elfhdr.e_flags & EF_MIPS_ABI) {
179 if (elfhdr.e_ident[EI_DATA] ==
182 else if (elfhdr.e_ident[EI_DATA] ==
187 snprintf(dest + strlen(dest), sz - strlen(dest),
188 ":%s:%s", elf_corres_to_string(endian_corres,
189 (int)elfhdr.e_ident[EI_DATA]), abi);
202 extract_pkg_static(int fd, char *p, int sz)
205 struct archive_entry *ae;
210 a = archive_read_new();
212 warn("archive_read_new");
215 archive_read_support_compression_all(a);
216 archive_read_support_format_tar(a);
218 if (lseek(fd, 0, 0) == -1) {
223 if (archive_read_open_fd(a, fd, 4096) != ARCHIVE_OK) {
224 warnx("archive_read_open_fd: %s", archive_error_string(a));
229 while ((r = archive_read_next_header(a, &ae)) == ARCHIVE_OK) {
230 end = strrchr(archive_entry_pathname(ae), '/');
234 if (strcmp(end, "/pkg-static") == 0) {
235 r = archive_read_extract(a, ae,
236 ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM |
237 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_ACL |
238 ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_XATTR);
239 strlcpy(p, archive_entry_pathname(ae), sz);
247 warnx("fail to extract pkg-static");
250 archive_read_finish(a);
256 install_pkg_static(char *path, char *pkgpath)
261 switch ((pid = fork())) {
265 execl(path, "pkg-static", "add", pkgpath, (char *)NULL);
271 while (waitpid(pid, &pstat, 0) == -1)
275 if (WEXITSTATUS(pstat))
276 return (WEXITSTATUS(pstat));
277 else if (WIFSIGNALED(pstat))
278 return (128 & (WTERMSIG(pstat)));
289 struct dns_srvinfo *mirrors, *current;
290 /* To store _https._tcp. + hostname + \0 */
291 char zone[MAXHOSTNAMELEN + 13];
292 char url[MAXPATHLEN];
293 char conf[MAXPATHLEN];
295 char tmppkg[MAXPATHLEN];
297 char pkgstatic[MAXPATHLEN];
298 int fd, retry, ret, max_retry;
310 current = mirrors = NULL;
312 printf("Bootstrapping pkg please wait\n");
314 if (pkg_get_myabi(abi, MAXPATHLEN) != 0) {
315 warnx("failed to determine the system ABI");
319 if (getenv("PACKAGESITE") != NULL)
320 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz", getenv("PACKAGESITE"));
322 snprintf(url, MAXPATHLEN, "%s/%s/latest/Latest/pkg.txz",
323 getenv("PACKAGEROOT") ? getenv("PACKAGEROOT") : _PKGS_URL,
324 getenv("ABI") ? getenv("ABI") : abi);
326 snprintf(tmppkg, MAXPATHLEN, "%s/pkg.txz.XXXXXX",
327 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
329 if ((fd = mkstemp(tmppkg)) == -1) {
336 u = fetchParseURL(url);
337 while (remote == NULL) {
338 if (retry == max_retry) {
339 if (strcmp(u->scheme, "file") != 0) {
340 snprintf(zone, sizeof(zone),
341 "_%s._tcp.%s", u->scheme, u->host);
342 printf("%s\n", zone);
343 mirrors = dns_getsrvinfo(zone);
349 strlcpy(u->host, current->host, sizeof(u->host));
351 remote = fetchXGet(u, &st, "");
352 if (remote == NULL) {
356 if (mirrors == NULL) {
359 current = current->next;
369 while (done < st.size) {
370 if ((r = fread(buf, 1, sizeof(buf), remote)) < 1)
373 if (write(fd, buf, r) != r) {
380 if (now > last || done == st.size)
387 if ((ret = extract_pkg_static(fd, pkgstatic, MAXPATHLEN)) == 0)
388 ret = install_pkg_static(pkgstatic, tmppkg);
390 snprintf(conf, MAXPATHLEN, "%s/etc/pkg.conf",
391 getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE);
393 if (access(conf, R_OK) == -1) {
394 site = strrchr(url, '/');
398 site = strrchr(url, '/');
403 config = fopen(conf, "w+");
406 fprintf(config, "packagesite: %s\n", url);
413 warnx("Error fetching %s: %s", url, fetchLastErrString);
424 static const char confirmation_message[] =
425 "The package management tool is not yet installed on your system.\n"
426 "Do you want to fetch and install it now? [y/N]: ";
429 pkg_query_yes_no(void)
435 if (c == 'y' || c == 'Y')
440 while (c != '\n' && c != EOF)
447 main(__unused int argc, char *argv[])
449 char pkgpath[MAXPATHLEN];
451 snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg",
452 getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE);
454 if (access(pkgpath, X_OK) == -1) {
456 * To allow 'pkg -N' to be used as a reliable test for whether
457 * a system is configured to use pkg, don't bootstrap pkg
458 * when that argument is given as argv[1].
460 if (argv[1] != NULL && strcmp(argv[1], "-N") == 0)
461 errx(EXIT_FAILURE, "pkg is not installed");
464 * Do not ask for confirmation if either of stdin or stdout is
465 * not tty. Check the environment to see if user has answer
466 * tucked in there already.
468 if (getenv("ASSUME_ALWAYS_YES") == NULL) {
469 printf("%s", confirmation_message);
470 if (!isatty(fileno(stdin)))
473 if (pkg_query_yes_no() == 0)
476 if (bootstrap_pkg() != 0)
480 execv(pkgpath, argv);
483 return (EXIT_FAILURE);