2012-04-15 15:13:36 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2012 Baptiste Daroussin <bapt@FreeBSD.org>
|
|
|
|
* 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 <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/elf_common.h>
|
|
|
|
#include <sys/endian.h>
|
2012-04-15 18:32:14 +00:00
|
|
|
#include <sys/wait.h>
|
2012-04-15 15:13:36 +00:00
|
|
|
|
|
|
|
#include <archive.h>
|
|
|
|
#include <archive_entry.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2012-04-15 18:32:14 +00:00
|
|
|
#include <fetch.h>
|
2012-04-15 15:13:36 +00:00
|
|
|
#include <gelf.h>
|
2012-04-16 20:41:25 +00:00
|
|
|
#include <paths.h>
|
2012-04-15 15:13:36 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "elf_tables.h"
|
2012-12-05 11:35:50 +00:00
|
|
|
#include "dns_utils.h"
|
2012-04-15 15:13:36 +00:00
|
|
|
|
|
|
|
#define _LOCALBASE "/usr/local"
|
2012-12-05 11:35:50 +00:00
|
|
|
#define _PKGS_URL "http://pkg.FreeBSD.org"
|
2012-04-15 15:13:36 +00:00
|
|
|
|
|
|
|
static const char *
|
2012-04-15 18:32:14 +00:00
|
|
|
elf_corres_to_string(struct _elf_corres *m, int e)
|
2012-04-15 15:13:36 +00:00
|
|
|
{
|
2012-04-15 16:00:32 +00:00
|
|
|
int i;
|
2012-04-15 15:13:36 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
Elf_Data *data;
|
|
|
|
Elf_Note note;
|
|
|
|
Elf_Scn *scn;
|
|
|
|
char *src, *osname;
|
|
|
|
const char *abi;
|
2012-04-16 20:41:25 +00:00
|
|
|
GElf_Ehdr elfhdr;
|
|
|
|
GElf_Shdr shdr;
|
2012-04-15 15:13:36 +00:00
|
|
|
int fd, i, ret;
|
|
|
|
uint32_t version;
|
|
|
|
|
|
|
|
version = 0;
|
2012-04-16 20:41:25 +00:00
|
|
|
ret = -1;
|
2012-04-15 15:13:36 +00:00
|
|
|
scn = NULL;
|
|
|
|
abi = NULL;
|
|
|
|
|
|
|
|
if (elf_version(EV_CURRENT) == EV_NONE) {
|
2012-04-15 18:32:14 +00:00
|
|
|
warnx("ELF library initialization failed: %s",
|
|
|
|
elf_errmsg(-1));
|
|
|
|
return (-1);
|
2012-04-15 15:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((fd = open("/bin/sh", O_RDONLY)) < 0) {
|
|
|
|
warn("open()");
|
2012-04-15 18:32:14 +00:00
|
|
|
return (-1);
|
2012-04-15 15:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2012-04-16 20:41:25 +00:00
|
|
|
warn("failed to get the note section");
|
2012-04-15 15:13:36 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = elf_getdata(scn, NULL);
|
|
|
|
src = data->d_buf;
|
2012-04-15 16:00:32 +00:00
|
|
|
for (;;) {
|
2012-04-15 15:13:36 +00:00
|
|
|
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",
|
2012-04-15 18:32:14 +00:00
|
|
|
osname, version / 100000,
|
|
|
|
elf_corres_to_string(mach_corres, (int)elfhdr.e_machine),
|
2012-04-15 15:13:36 +00:00
|
|
|
elf_corres_to_string(wordsize_corres,
|
2012-04-15 18:32:14 +00:00
|
|
|
(int)elfhdr.e_ident[EI_CLASS]));
|
2012-04-15 15:13:36 +00:00
|
|
|
|
2012-04-16 20:41:25 +00:00
|
|
|
ret = 0;
|
|
|
|
|
2012-04-15 15:13:36 +00:00
|
|
|
switch (elfhdr.e_machine) {
|
2012-04-16 20:41:25 +00:00
|
|
|
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";
|
2012-04-15 15:13:36 +00:00
|
|
|
break;
|
2012-04-16 20:41:25 +00:00
|
|
|
default:
|
|
|
|
if (elfhdr.e_ident[EI_DATA] ==
|
|
|
|
ELFCLASS32)
|
|
|
|
abi = "o32";
|
|
|
|
else if (elfhdr.e_ident[EI_DATA] ==
|
|
|
|
ELFCLASS64)
|
|
|
|
abi = "n64";
|
2012-04-15 15:13:36 +00:00
|
|
|
break;
|
2012-04-16 20:41:25 +00:00
|
|
|
}
|
|
|
|
snprintf(dest + strlen(dest), sz - strlen(dest),
|
|
|
|
":%s:%s", elf_corres_to_string(endian_corres,
|
|
|
|
(int)elfhdr.e_ident[EI_DATA]), abi);
|
|
|
|
break;
|
2012-04-15 15:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2012-04-16 20:41:25 +00:00
|
|
|
ret = -1;
|
2012-04-15 15:13:36 +00:00
|
|
|
a = archive_read_new();
|
2012-04-16 20:41:25 +00:00
|
|
|
if (a == NULL) {
|
|
|
|
warn("archive_read_new");
|
|
|
|
return (ret);
|
|
|
|
}
|
2013-02-20 22:51:42 +00:00
|
|
|
archive_read_support_filter_all(a);
|
2012-04-15 15:13:36 +00:00
|
|
|
archive_read_support_format_tar(a);
|
|
|
|
|
2012-04-16 20:41:25 +00:00
|
|
|
if (lseek(fd, 0, 0) == -1) {
|
|
|
|
warn("lseek");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-04-15 15:13:36 +00:00
|
|
|
|
|
|
|
if (archive_read_open_fd(a, fd, 4096) != ARCHIVE_OK) {
|
2012-04-16 20:41:25 +00:00
|
|
|
warnx("archive_read_open_fd: %s", archive_error_string(a));
|
2012-04-15 15:13:36 +00:00
|
|
|
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,
|
2012-04-15 18:32:14 +00:00
|
|
|
ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM |
|
|
|
|
ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_ACL |
|
|
|
|
ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_XATTR);
|
2012-04-16 20:41:25 +00:00
|
|
|
strlcpy(p, archive_entry_pathname(ae), sz);
|
2012-04-15 15:13:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-16 20:41:25 +00:00
|
|
|
if (r == ARCHIVE_OK)
|
|
|
|
ret = 0;
|
|
|
|
else
|
2012-04-15 15:13:36 +00:00
|
|
|
warnx("fail to extract pkg-static");
|
|
|
|
|
|
|
|
cleanup:
|
2013-02-20 22:51:42 +00:00
|
|
|
archive_read_free(a);
|
2012-04-15 18:32:14 +00:00
|
|
|
return (ret);
|
2012-04-15 15:13:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
install_pkg_static(char *path, char *pkgpath)
|
|
|
|
{
|
|
|
|
int pstat;
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
switch ((pid = fork())) {
|
2012-04-16 20:41:25 +00:00
|
|
|
case -1:
|
|
|
|
return (-1);
|
|
|
|
case 0:
|
|
|
|
execl(path, "pkg-static", "add", pkgpath, (char *)NULL);
|
|
|
|
_exit(1);
|
|
|
|
default:
|
|
|
|
break;
|
2012-04-15 15:13:36 +00:00
|
|
|
}
|
|
|
|
|
2012-04-16 20:41:25 +00:00
|
|
|
while (waitpid(pid, &pstat, 0) == -1)
|
2012-04-15 15:13:36 +00:00
|
|
|
if (errno != EINTR)
|
|
|
|
return (-1);
|
|
|
|
|
2012-04-16 20:41:25 +00:00
|
|
|
if (WEXITSTATUS(pstat))
|
|
|
|
return (WEXITSTATUS(pstat));
|
|
|
|
else if (WIFSIGNALED(pstat))
|
|
|
|
return (128 & (WTERMSIG(pstat)));
|
|
|
|
return (pstat);
|
2012-04-15 15:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
bootstrap_pkg(void)
|
|
|
|
{
|
2012-12-05 11:35:50 +00:00
|
|
|
struct url *u;
|
2012-04-15 15:13:36 +00:00
|
|
|
FILE *remote;
|
2012-05-01 10:16:12 +00:00
|
|
|
FILE *config;
|
|
|
|
char *site;
|
2012-12-05 11:35:50 +00:00
|
|
|
struct dns_srvinfo *mirrors, *current;
|
|
|
|
/* To store _https._tcp. + hostname + \0 */
|
|
|
|
char zone[MAXHOSTNAMELEN + 13];
|
2012-04-15 15:13:36 +00:00
|
|
|
char url[MAXPATHLEN];
|
2012-05-01 10:16:12 +00:00
|
|
|
char conf[MAXPATHLEN];
|
2012-04-15 15:13:36 +00:00
|
|
|
char abi[BUFSIZ];
|
|
|
|
char tmppkg[MAXPATHLEN];
|
|
|
|
char buf[10240];
|
|
|
|
char pkgstatic[MAXPATHLEN];
|
2012-12-05 11:35:50 +00:00
|
|
|
int fd, retry, ret, max_retry;
|
2012-04-16 20:41:25 +00:00
|
|
|
struct url_stat st;
|
2012-04-15 15:13:36 +00:00
|
|
|
off_t done, r;
|
2012-04-16 20:41:25 +00:00
|
|
|
time_t now;
|
|
|
|
time_t last;
|
2012-04-15 15:13:36 +00:00
|
|
|
|
|
|
|
done = 0;
|
2012-04-16 20:41:25 +00:00
|
|
|
last = 0;
|
2012-12-05 11:35:50 +00:00
|
|
|
max_retry = 3;
|
2012-04-16 20:41:25 +00:00
|
|
|
ret = -1;
|
2012-04-15 15:13:36 +00:00
|
|
|
remote = NULL;
|
2012-05-01 10:16:12 +00:00
|
|
|
config = NULL;
|
2012-12-05 11:35:50 +00:00
|
|
|
current = mirrors = NULL;
|
2012-04-15 15:13:36 +00:00
|
|
|
|
2012-04-16 20:41:25 +00:00
|
|
|
printf("Bootstrapping pkg please wait\n");
|
2012-04-15 15:13:36 +00:00
|
|
|
|
|
|
|
if (pkg_get_myabi(abi, MAXPATHLEN) != 0) {
|
2012-04-16 20:41:25 +00:00
|
|
|
warnx("failed to determine the system ABI");
|
2012-04-15 18:32:14 +00:00
|
|
|
return (-1);
|
2012-04-15 15:13:36 +00:00
|
|
|
}
|
|
|
|
|
2012-04-16 20:41:25 +00:00
|
|
|
if (getenv("PACKAGESITE") != NULL)
|
2012-05-01 10:16:12 +00:00
|
|
|
snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz", getenv("PACKAGESITE"));
|
2012-04-16 20:41:25 +00:00
|
|
|
else
|
2012-04-15 15:13:36 +00:00
|
|
|
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",
|
2012-04-16 20:41:25 +00:00
|
|
|
getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
|
2012-04-15 15:13:36 +00:00
|
|
|
|
|
|
|
if ((fd = mkstemp(tmppkg)) == -1) {
|
|
|
|
warn("mkstemp()");
|
2012-04-15 18:32:14 +00:00
|
|
|
return (-1);
|
2012-04-15 15:13:36 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 11:35:50 +00:00
|
|
|
retry = max_retry;
|
|
|
|
|
|
|
|
u = fetchParseURL(url);
|
|
|
|
while (remote == NULL) {
|
|
|
|
if (retry == max_retry) {
|
|
|
|
if (strcmp(u->scheme, "file") != 0) {
|
|
|
|
snprintf(zone, sizeof(zone),
|
|
|
|
"_%s._tcp.%s", u->scheme, u->host);
|
|
|
|
printf("%s\n", zone);
|
|
|
|
mirrors = dns_getsrvinfo(zone);
|
|
|
|
current = mirrors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mirrors != NULL)
|
|
|
|
strlcpy(u->host, current->host, sizeof(u->host));
|
|
|
|
|
|
|
|
remote = fetchXGet(u, &st, "");
|
|
|
|
if (remote == NULL) {
|
|
|
|
--retry;
|
|
|
|
if (retry <= 0)
|
|
|
|
goto fetchfail;
|
|
|
|
if (mirrors == NULL) {
|
|
|
|
sleep(1);
|
|
|
|
} else {
|
|
|
|
current = current->next;
|
|
|
|
if (current == NULL)
|
|
|
|
current = mirrors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-16 20:41:25 +00:00
|
|
|
|
|
|
|
if (remote == NULL)
|
|
|
|
goto fetchfail;
|
2012-04-15 15:13:36 +00:00
|
|
|
|
|
|
|
while (done < st.size) {
|
|
|
|
if ((r = fread(buf, 1, sizeof(buf), remote)) < 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (write(fd, buf, r) != r) {
|
|
|
|
warn("write()");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
done += r;
|
|
|
|
now = time(NULL);
|
2012-04-16 20:41:25 +00:00
|
|
|
if (now > last || done == st.size)
|
2012-04-15 15:13:36 +00:00
|
|
|
last = now;
|
|
|
|
}
|
|
|
|
|
2012-04-16 20:41:25 +00:00
|
|
|
if (ferror(remote))
|
|
|
|
goto fetchfail;
|
2012-04-15 15:13:36 +00:00
|
|
|
|
|
|
|
if ((ret = extract_pkg_static(fd, pkgstatic, MAXPATHLEN)) == 0)
|
|
|
|
ret = install_pkg_static(pkgstatic, tmppkg);
|
|
|
|
|
2012-05-01 10:16:12 +00:00
|
|
|
snprintf(conf, MAXPATHLEN, "%s/etc/pkg.conf",
|
|
|
|
getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE);
|
|
|
|
|
|
|
|
if (access(conf, R_OK) == -1) {
|
|
|
|
site = strrchr(url, '/');
|
|
|
|
if (site == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
site[0] = '\0';
|
|
|
|
site = strrchr(url, '/');
|
|
|
|
if (site == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
site[0] = '\0';
|
|
|
|
|
|
|
|
config = fopen(conf, "w+");
|
|
|
|
if (config == NULL)
|
|
|
|
goto cleanup;
|
2012-05-21 15:45:18 +00:00
|
|
|
fprintf(config, "packagesite: %s\n", url);
|
2012-05-01 10:16:12 +00:00
|
|
|
fclose(config);
|
|
|
|
}
|
|
|
|
|
2012-04-16 20:41:25 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
fetchfail:
|
|
|
|
warnx("Error fetching %s: %s", url, fetchLastErrString);
|
2013-02-14 12:42:09 +00:00
|
|
|
fprintf(stderr, "A pre-built version of pkg could not be found for your system.\n");
|
|
|
|
fprintf(stderr, "Consider changing PACKAGESITE or installing it from ports: 'ports-mgmt/pkg'.\n");
|
2012-04-16 20:41:25 +00:00
|
|
|
|
2012-04-15 15:13:36 +00:00
|
|
|
cleanup:
|
2012-05-01 10:16:12 +00:00
|
|
|
if (remote != NULL)
|
|
|
|
fclose(remote);
|
2012-04-15 15:13:36 +00:00
|
|
|
close(fd);
|
|
|
|
unlink(tmppkg);
|
|
|
|
|
2012-04-16 20:41:25 +00:00
|
|
|
return (ret);
|
2012-04-15 15:13:36 +00:00
|
|
|
}
|
|
|
|
|
2012-07-15 04:15:27 +00:00
|
|
|
static const char confirmation_message[] =
|
|
|
|
"The package management tool is not yet installed on your system.\n"
|
|
|
|
"Do you want to fetch and install it now? [y/N]: ";
|
|
|
|
|
|
|
|
static int
|
|
|
|
pkg_query_yes_no(void)
|
|
|
|
{
|
|
|
|
int ret, c;
|
|
|
|
|
|
|
|
c = getchar();
|
|
|
|
|
|
|
|
if (c == 'y' || c == 'Y')
|
|
|
|
ret = 1;
|
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
while (c != '\n' && c != EOF)
|
|
|
|
c = getchar();
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2012-04-15 15:13:36 +00:00
|
|
|
int
|
2012-04-15 18:32:14 +00:00
|
|
|
main(__unused int argc, char *argv[])
|
2012-04-15 15:13:36 +00:00
|
|
|
{
|
|
|
|
char pkgpath[MAXPATHLEN];
|
|
|
|
|
|
|
|
snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg",
|
2012-04-15 18:32:14 +00:00
|
|
|
getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE);
|
2012-04-15 15:13:36 +00:00
|
|
|
|
2012-07-15 04:15:27 +00:00
|
|
|
if (access(pkgpath, X_OK) == -1) {
|
2012-12-21 20:01:13 +00:00
|
|
|
/*
|
2012-12-22 15:13:16 +00:00
|
|
|
* To allow 'pkg -N' to be used as a reliable test for whether
|
2012-12-21 20:01:13 +00:00
|
|
|
* a system is configured to use pkg, don't bootstrap pkg
|
|
|
|
* when that argument is given as argv[1].
|
|
|
|
*/
|
2012-12-23 20:39:03 +00:00
|
|
|
if (argv[1] != NULL && strcmp(argv[1], "-N") == 0)
|
|
|
|
errx(EXIT_FAILURE, "pkg is not installed");
|
2012-12-21 20:01:13 +00:00
|
|
|
|
2012-07-15 04:15:27 +00:00
|
|
|
/*
|
|
|
|
* Do not ask for confirmation if either of stdin or stdout is
|
|
|
|
* not tty. Check the environment to see if user has answer
|
|
|
|
* tucked in there already.
|
|
|
|
*/
|
2012-08-24 21:08:56 +00:00
|
|
|
if (getenv("ASSUME_ALWAYS_YES") == NULL) {
|
2012-07-15 04:15:27 +00:00
|
|
|
printf("%s", confirmation_message);
|
2012-08-24 21:45:52 +00:00
|
|
|
if (!isatty(fileno(stdin)))
|
2012-08-24 21:08:56 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2012-08-24 21:45:52 +00:00
|
|
|
|
|
|
|
if (pkg_query_yes_no() == 0)
|
2012-07-15 04:15:27 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2012-04-16 20:41:25 +00:00
|
|
|
if (bootstrap_pkg() != 0)
|
|
|
|
exit(EXIT_FAILURE);
|
2012-07-15 04:15:27 +00:00
|
|
|
}
|
2012-04-15 15:13:36 +00:00
|
|
|
|
|
|
|
execv(pkgpath, argv);
|
|
|
|
|
2012-04-16 20:41:25 +00:00
|
|
|
/* NOT REACHED */
|
2012-04-15 18:32:14 +00:00
|
|
|
return (EXIT_FAILURE);
|
2012-04-15 15:13:36 +00:00
|
|
|
}
|