2 * Copyright (c) 2012-2013 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>
34 #include <archive_entry.h>
46 #include "dns_utils.h"
50 extract_pkg_static(int fd, char *p, int sz)
53 struct archive_entry *ae;
58 a = archive_read_new();
60 warn("archive_read_new");
63 archive_read_support_filter_all(a);
64 archive_read_support_format_tar(a);
66 if (lseek(fd, 0, 0) == -1) {
71 if (archive_read_open_fd(a, fd, 4096) != ARCHIVE_OK) {
72 warnx("archive_read_open_fd: %s", archive_error_string(a));
77 while ((r = archive_read_next_header(a, &ae)) == ARCHIVE_OK) {
78 end = strrchr(archive_entry_pathname(ae), '/');
82 if (strcmp(end, "/pkg-static") == 0) {
83 r = archive_read_extract(a, ae,
84 ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM |
85 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_ACL |
86 ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_XATTR);
87 strlcpy(p, archive_entry_pathname(ae), sz);
95 warnx("fail to extract pkg-static");
104 install_pkg_static(char *path, char *pkgpath)
109 switch ((pid = fork())) {
113 execl(path, "pkg-static", "add", pkgpath, (char *)NULL);
119 while (waitpid(pid, &pstat, 0) == -1)
123 if (WEXITSTATUS(pstat))
124 return (WEXITSTATUS(pstat));
125 else if (WIFSIGNALED(pstat))
126 return (128 & (WTERMSIG(pstat)));
137 struct dns_srvinfo *mirrors, *current;
138 /* To store _https._tcp. + hostname + \0 */
139 char zone[MAXHOSTNAMELEN + 13];
140 char url[MAXPATHLEN];
141 char conf[MAXPATHLEN];
142 char tmppkg[MAXPATHLEN];
143 const char *packagesite, *mirror_type;
145 char pkgstatic[MAXPATHLEN];
146 int fd, retry, ret, max_retry;
158 current = mirrors = NULL;
160 printf("Bootstrapping pkg please wait\n");
162 if (config_string(PACKAGESITE, &packagesite) != 0) {
163 warnx("No PACKAGESITE defined");
166 if (config_string(MIRROR_TYPE, &mirror_type) != 0) {
167 warnx("No MIRROR_TYPE defined");
170 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz", packagesite);
172 snprintf(tmppkg, MAXPATHLEN, "%s/pkg.txz.XXXXXX",
173 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
175 if ((fd = mkstemp(tmppkg)) == -1) {
182 u = fetchParseURL(url);
183 while (remote == NULL) {
184 if (retry == max_retry) {
185 if (strcmp(u->scheme, "file") != 0 &&
186 strcasecmp(mirror_type, "srv") == 0) {
187 snprintf(zone, sizeof(zone),
188 "_%s._tcp.%s", u->scheme, u->host);
189 mirrors = dns_getsrvinfo(zone);
195 strlcpy(u->host, current->host, sizeof(u->host));
197 remote = fetchXGet(u, &st, "");
198 if (remote == NULL) {
202 if (mirrors == NULL) {
205 current = current->next;
215 while (done < st.size) {
216 if ((r = fread(buf, 1, sizeof(buf), remote)) < 1)
219 if (write(fd, buf, r) != r) {
226 if (now > last || done == st.size)
233 if ((ret = extract_pkg_static(fd, pkgstatic, MAXPATHLEN)) == 0)
234 ret = install_pkg_static(pkgstatic, tmppkg);
236 snprintf(conf, MAXPATHLEN, "%s/etc/pkg.conf",
237 getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE);
239 if (access(conf, R_OK) == -1) {
240 site = strrchr(url, '/');
244 site = strrchr(url, '/');
249 config = fopen(conf, "w+");
252 fprintf(config, "packagesite: %s\n", url);
259 warnx("Error fetching %s: %s", url, fetchLastErrString);
260 fprintf(stderr, "A pre-built version of pkg could not be found for your system.\n");
261 fprintf(stderr, "Consider changing PACKAGESITE or installing it from ports: 'ports-mgmt/pkg'.\n");
272 static const char confirmation_message[] =
273 "The package management tool is not yet installed on your system.\n"
274 "Do you want to fetch and install it now? [y/N]: ";
277 pkg_query_yes_no(void)
283 if (c == 'y' || c == 'Y')
288 while (c != '\n' && c != EOF)
295 main(__unused int argc, char *argv[])
297 char pkgpath[MAXPATHLEN];
300 snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg",
301 getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE);
303 if (access(pkgpath, X_OK) == -1) {
305 * To allow 'pkg -N' to be used as a reliable test for whether
306 * a system is configured to use pkg, don't bootstrap pkg
307 * when that argument is given as argv[1].
309 if (argv[1] != NULL && strcmp(argv[1], "-N") == 0)
310 errx(EXIT_FAILURE, "pkg is not installed");
313 * Do not ask for confirmation if either of stdin or stdout is
314 * not tty. Check the environment to see if user has answer
315 * tucked in there already.
318 config_bool(ASSUME_ALWAYS_YES, &yes);
320 printf("%s", confirmation_message);
321 if (!isatty(fileno(stdin)))
324 if (pkg_query_yes_no() == 0)
327 if (bootstrap_pkg() != 0)
332 execv(pkgpath, argv);
335 return (EXIT_FAILURE);