2 * Copyright (c) 2019 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * This code uses concepts and configuration based on 'synth', by
8 * John R. Marino <draco@marino.st>, which was written in ada.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
20 * 3. Neither the name of The DragonFly Project nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific, prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 worker_t WorkerAry[MAXWORKERS];
42 int DynamicMaxWorkers;
44 pthread_mutex_t WorkerMutex;
45 pthread_cond_t WorkerCond;
47 static int build_find_leaves(pkg_t *parent, pkg_t *pkg, pkg_t ***build_tailp,
48 int *app, int *hasworkp, int level, int first);
49 static void build_clear_trav(pkg_t *pkg);
50 static void startbuild(pkg_t **build_listp, pkg_t ***build_tailp);
51 static int qsort_depi(const void *pkg1, const void *pkg2);
52 static int qsort_idep(const void *pkg1, const void *pkg2);
53 static void startworker(pkg_t *pkg, worker_t *work);
54 static void cleanworker(worker_t *work);
55 static void waitbuild(int whilematch, int dynamicmax);
56 static void workercomplete(worker_t *work);
57 static void *childBuilderThread(void *arg);
58 static int childInstallPkgDeps(worker_t *work);
59 static void childInstallPkgDeps_recurse(FILE *fp, pkglink_t *list, int undoit);
60 static void dophase(worker_t *work, wmsg_t *wmsg,
61 int wdog, int phaseid, const char *phase);
62 static void phaseReapAll(void);
63 static void phaseTerminateSignal(int sig);
64 static char *buildskipreason(pkglink_t *parent, pkg_t *pkg);
65 static int mptylogpoll(int ptyfd, int fdlog, wmsg_t *wmsg,
67 static int copyfile(char *src, char *dst);
69 static worker_t *SigWork;
70 static int MasterPtyFd = -1;
73 #define MPTY_FAILED -2
82 int BuildSuccessCount;
85 * Initialize the WorkerAry[]
88 DoInitBuild(int slot_override)
94 ddassert(slot_override < 0 || MaxWorkers == 1);
96 bzero(WorkerAry, MaxWorkers * sizeof(worker_t));
97 pthread_mutex_init(&WorkerMutex, NULL);
99 for (i = 0; i < MaxWorkers; ++i) {
100 work = &WorkerAry[i];
101 work->index = (slot_override >= 0) ? slot_override : i;
102 work->state = WORKER_NONE;
103 asprintf(&work->basedir, "%s/SL%02d", BuildBase, work->index);
104 pthread_cond_init(&work->cond, NULL);
109 * Create required sub-directories. The base directories must already
110 * exist as a dsynth configuration safety.
112 if (stat(RepositoryPath, &st) < 0) {
113 if (mkdir(RepositoryPath, 0755) < 0)
114 dfatal("Cannot mkdir %s\n", RepositoryPath);
117 BuildInitialized = 1;
120 * slow-start (increases at a rate of 1 per 5 seconds)
122 if (SlowStartOpt > MaxWorkers)
123 DynamicMaxWorkers = MaxWorkers;
124 else if (SlowStartOpt > 0)
125 DynamicMaxWorkers = SlowStartOpt;
127 DynamicMaxWorkers = MaxWorkers;
131 * Called by the frontend to clean-up any hanging mounts.
138 ddassert(BuildInitialized);
140 for (i = 0; i < MaxWorkers; ++i) {
141 DoWorkerUnmounts(&WorkerAry[i]);
149 pkg_t *build_list = NULL;
150 pkg_t **build_tail = &build_list;
155 for (scan = pkgs; scan; scan = scan->bnext)
159 * The pkg and pkg-static binaries are needed. If already present
160 * then assume that the template is also valid, otherwise build
163 scan = GetPkgPkg(pkgs);
166 * Create our template. The template will be missing pkg
169 if ((scan->flags & (PKGF_SUCCESS | PKGF_PACKAGED)) == 0) {
170 DoCreateTemplate(1); /* force a fresh template */
176 * This will clear the screen and set-up our gui, so sleep
177 * a little first in case the user wants to see what was
181 pthread_mutex_lock(&WorkerMutex);
185 if ((scan->flags & (PKGF_SUCCESS | PKGF_PACKAGED)) == 0) {
190 * Build pkg/pkg-static.
193 build_tail = &scan->build_next;
194 startbuild(&build_list, &build_tail);
195 while (RunningWorkers == 1)
198 if (scan->flags & PKGF_NOBUILD)
199 dfatal("Unable to build 'pkg'");
200 if (scan->flags & PKGF_ERROR)
201 dfatal("Error building 'pkg'");
202 if ((scan->flags & PKGF_SUCCESS) == 0)
203 dfatal("Error building 'pkg'");
206 * Install pkg/pkg-static into the template
210 "tar --exclude '+*' --exclude '*/man/*' "
211 "-xvzpf %s/%s > /dev/null 2>&1",
217 dfatal("Command failed: %s\n", buf);
222 * Nominal bulk build sequence
227 for (scan = pkgs; scan; scan = scan->bnext) {
228 ddprintf(0, "SCANLEAVES %08x %s\n",
229 scan->flags, scan->portdir);
230 scan->flags |= PKGF_BUILDLOOP;
232 * NOTE: We must still find dependencies if PACKAGED
233 * to fill in the gaps, as some of them may
234 * need to be rebuilt.
236 if (scan->flags & (PKGF_SUCCESS | PKGF_FAILURE |
239 ddprintf(0, "%s: already built\n",
244 build_find_leaves(NULL, scan, &build_tail,
245 &ap, &haswork, 0, first);
246 ddprintf(0, "TOPLEVEL %s %08x\n",
249 scan->flags &= ~PKGF_BUILDLOOP;
250 build_clear_trav(scan);
254 startbuild(&build_list, &build_tail);
256 if (haswork == 0 && RunningWorkers) {
257 waitbuild(RunningWorkers, 1);
261 pthread_mutex_unlock(&WorkerMutex);
265 ddprintf(0, "BuildCount %d\n", BuildCount);
269 * Traverse the packages (pkg) depends on recursively until we find
270 * a leaf to build or report as unbuildable. Calculates and assigns a
271 * dependency count. Returns all parallel-buildable packages.
273 * (pkg) itself is only added to the list if it is immediately buildable.
277 build_find_leaves(pkg_t *parent, pkg_t *pkg, pkg_t ***build_tailp,
278 int *app, int *hasworkp, int level, int first)
287 * Already on build list, possibly in-progress, tell caller that
290 ddprintf(level, "sbuild_find_leaves %d %s %08x {\n",
291 level, pkg->portdir, pkg->flags);
292 if (pkg->flags & PKGF_BUILDLIST) {
293 ddprintf(level, "} (already on build list)\n");
294 *app |= PKGF_NOTREADY;
295 return (pkg->idep_count);
302 PKGLIST_FOREACH(link, &pkg->idepon_list) {
307 ddprintf(level, "check %s %08x\t", scan->portdir, scan->flags);
310 * When accounting for a successful build, just bump
311 * idep_count by one. scan->idep_count will heavily
312 * overlap packages that we count down multiple branches.
314 * We must still recurse through PACKAGED packages as
315 * some of their dependencies might be missing.
317 if (scan->flags & PKGF_SUCCESS) {
318 ddprintf(0, "SUCCESS - OK\n");
324 * ERROR includes FAILURE, which is set in numerous situations
325 * including when NOBUILD state is processed. So check for
326 * NOBUILD state first.
328 * An ERROR in a sub-package causes a NOBUILD in packages
331 if (scan->flags & PKGF_NOBUILD) {
332 ddprintf(0, "NOBUILD - OK "
333 "(propogate failure upward)\n");
334 *app |= PKGF_NOBUILD_S;
337 if (scan->flags & PKGF_ERROR) {
338 ddprintf(0, "ERROR - OK (propogate failure upward)\n");
339 *app |= PKGF_NOBUILD_S;
344 * If already on build-list this dependency is not ready.
346 if (scan->flags & PKGF_BUILDLIST) {
347 ddprintf(0, " [BUILDLIST]");
348 *app |= PKGF_NOTREADY;
352 * If not packaged this dependency is not ready for
355 if ((scan->flags & PKGF_PACKAGED) == 0) {
356 ddprintf(0, " [NOT_PACKAGED]");
357 *app |= PKGF_NOTREADY;
361 * Reduce search complexity, if we have already processed
362 * scan in the traversal it will either already be on the
363 * build list or it will not be buildable. Either way
364 * the parent is not buildable.
366 if (scan->flags & PKGF_BUILDTRAV) {
367 ddprintf(0, " [BUILDTRAV]\n");
368 *app |= PKGF_NOTREADY;
373 * Assert on dependency loop
376 if (scan->flags & PKGF_BUILDLOOP) {
377 dfatal("pkg dependency loop %s -> %s",
378 parent->portdir, scan->portdir);
380 scan->flags |= PKGF_BUILDLOOP;
382 ddprintf(0, " SUBRECURSION {\n");
383 idep_count += build_find_leaves(pkg, scan, build_tailp,
386 scan->flags &= ~PKGF_BUILDLOOP;
388 if (apsub & PKGF_NOBUILD) {
389 ddprintf(level, "} (sub-nobuild)\n");
390 } else if (apsub & PKGF_ERROR) {
391 ddprintf(level, "} (sub-error)\n");
392 } else if (apsub & PKGF_NOTREADY) {
393 ddprintf(level, "} (sub-notready)\n");
395 ddprintf(level, "} (sub-ok)\n");
399 pkg->idep_count = idep_count;
400 pkg->flags |= PKGF_BUILDTRAV;
403 * Incorporate scan results into pkg state.
405 if ((pkg->flags & PKGF_NOBUILD) == 0 && (*app & PKGF_NOBUILD)) {
407 } else if ((pkg->flags & PKGF_ERROR) == 0 && (*app & PKGF_ERROR)) {
410 pkg->flags |= *app & ~PKGF_NOTREADY;
413 * Clear PACKAGED bit if sub-dependencies aren't clean
415 if ((pkg->flags & PKGF_PACKAGED) &&
416 (pkg->flags & (PKGF_NOTREADY|PKGF_ERROR|PKGF_NOBUILD))) {
417 pkg->flags &= ~PKGF_PACKAGED;
418 ddassert(pkg->pkgfile);
419 asprintf(&buf, "%s/%s", RepositoryPath, pkg->pkgfile);
420 if (remove(buf) < 0) {
422 "[XXX] %s DELETE-PACKAGE %s (failed)\n",
426 "[XXX] %s DELETE-PACKAGE %s "
427 "(due to dependencies)\n",
436 * Handle propagated flags
438 if (pkg->flags & PKGF_ERROR) {
440 * This can only happen if the ERROR has already been
441 * processed and accounted for.
443 ddprintf(level, "} (ERROR - %s)\n", pkg->portdir);
444 } else if (*app & PKGF_NOTREADY) {
446 * We don't set PKGF_NOTREADY in the pkg, it is strictly
447 * a transient flag propagated via build_find_leaves().
449 * Just don't add the package to the list.
451 * NOTE: Even if NOBUILD is set (meaning we could list it
452 * and let startbuild() finish it up as a skip, we
453 * don't process it to the list because we want to
454 * process all the dependencies, so someone doing a
455 * manual build can get more complete information and
456 * does not have to iterate each failed dependency one
460 } else if (pkg->flags & PKGF_SUCCESS) {
461 ddprintf(level, "} (SUCCESS - %s)\n", pkg->portdir);
462 } else if (pkg->flags & PKGF_DUMMY) {
463 ddprintf(level, "} (DUMMY/META - SUCCESS)\n");
464 pkg->flags |= PKGF_SUCCESS;
467 dlog(DLOG_ALL, "[XXX] %s META-ALREADY-BUILT\n",
471 dlog(DLOG_SUCC, "[XXX] %s meta-node complete\n",
474 } else if (pkg->flags & PKGF_PACKAGED) {
476 * We can just mark the pkg successful. If this is
477 * the first pass, we count this as an initial pruning
478 * pass and reduce BuildTotal.
480 ddprintf(level, "} (PACKAGED - SUCCESS)\n");
481 pkg->flags |= PKGF_SUCCESS;
484 dlog(DLOG_ALL, "[XXX] %s ALREADY-BUILT\n",
490 * All dependencies are successful, queue new work
491 * and indicate not-ready to the parent (since our
492 * package has to be built).
494 * NOTE: The NOBUILD case propagates to here as well
495 * and is ultimately handled by startbuild().
498 if (pkg->flags & PKGF_NOBUILD)
499 ddprintf(level, "} (ADDLIST(NOBUILD) - %s)\n",
502 ddprintf(level, "} (ADDLIST - %s)\n", pkg->portdir);
503 pkg->flags |= PKGF_BUILDLIST;
505 *build_tailp = &pkg->build_next;
506 *app |= PKGF_NOTREADY;
514 build_clear_trav(pkg_t *pkg)
519 pkg->flags &= ~PKGF_BUILDTRAV;
520 PKGLIST_FOREACH(link, &pkg->idepon_list) {
522 if (scan && (scan->flags & PKGF_BUILDTRAV))
523 build_clear_trav(scan);
528 * Take a list of pkg ready to go, sort it, and assign it to worker
529 * slots. This routine blocks in waitbuild() until it can dispose of
532 * WorkerMutex is held by the caller.
536 startbuild(pkg_t **build_listp, pkg_t ***build_tailp)
547 static int IterateWorker;
552 if (*build_listp == NULL)
559 for (pkg = *build_listp; pkg; pkg = pkg->build_next)
561 idep_ary = calloc(count, sizeof(pkg_t *));
562 depi_ary = calloc(count, sizeof(pkg_t *));
565 for (pkg = *build_listp; pkg; pkg = pkg->build_next) {
566 idep_ary[count] = pkg;
567 depi_ary[count] = pkg;
572 * idep_ary - sorted by #of dependencies this pkg has.
573 * depi_ary - sorted by #of other packages that depend on this pkg.
575 qsort(idep_ary, count, sizeof(pkg_t *), qsort_idep);
576 qsort(depi_ary, count, sizeof(pkg_t *), qsort_depi);
582 * Half the workers build based on the highest depi count,
583 * the other half build based on the highest idep count.
585 * This is an attempt to get projects which many other projects
586 * depend on built first, but to also try to build large projects
587 * (which tend to have a lot of dependencies) earlier rather than
588 * later so the end of the bulk run doesn't inefficiently build
589 * the last few huge projects.
591 * Loop until we manage to assign slots to everyone. We do not
592 * wait for build completion.
594 * This is the point where we handle DUMMY packages (these are
595 * dummy unflavored packages which 'cover' all the flavors for
596 * a package). These are not real packages are marked SUCCESS
597 * at this time because their dependencies (the flavors) have all
600 while (idep_index != count || depi_index != count) {
605 * Find candidate to start sorted by depi or idep.
608 while (idep_index < count) {
609 ipkg = idep_ary[idep_index];
611 (PKGF_SUCCESS | PKGF_FAILURE |
612 PKGF_ERROR | PKGF_RUNNING)) == 0) {
620 while (depi_index < count) {
621 pkgi = depi_ary[depi_index];
623 (PKGF_SUCCESS | PKGF_FAILURE |
624 PKGF_ERROR | PKGF_RUNNING)) == 0) {
632 * ipkg and pkgi must either both be NULL, or both
635 if (ipkg == NULL && pkgi == NULL)
637 ddassert(ipkg && pkgi);
640 * Handle the NOBUILD case right here, there's no point
641 * queueing it anywhere.
643 if (ipkg->flags & PKGF_NOBUILD) {
646 ipkg->flags |= PKGF_FAILURE;
647 ipkg->flags &= ~PKGF_BUILDLIST;
651 reason = buildskipreason(NULL, ipkg);
652 dlog(DLOG_SKIP, "[XXX] %s skipped due to %s\n",
653 ipkg->portdir, reason);
657 if (pkgi->flags & PKGF_NOBUILD) {
660 pkgi->flags |= PKGF_FAILURE;
661 pkgi->flags &= ~PKGF_BUILDLIST;
665 reason = buildskipreason(NULL, pkgi);
666 dlog(DLOG_SKIP, "[XXX] %s skipped due to %s\n",
667 pkgi->portdir, reason);
673 * Block while no slots are available. waitbuild()
674 * will clean out any DONE states.
676 while (RunningWorkers >= DynamicMaxWorkers ||
677 RunningWorkers >= MaxWorkers - FailedWorkers) {
678 waitbuild(RunningWorkers, 1);
682 * Find an available worker slot, there should be at
685 for (i = 0; i < MaxWorkers; ++i) {
686 n = IterateWorker % MaxWorkers;
687 work = &WorkerAry[n];
689 if (work->state == WORKER_DONE ||
690 work->state == WORKER_FAILED) {
691 workercomplete(work);
693 if (work->state == WORKER_NONE ||
694 work->state == WORKER_IDLE) {
695 if (n <= MaxWorkers / 2) {
696 startworker(pkgi, work);
698 startworker(ipkg, work);
705 ddassert(i != MaxWorkers);
710 * We disposed of the whole list
715 *build_tailp = build_listp;
718 typedef const pkg_t *pkg_tt;
721 qsort_idep(const void *pkg1_arg, const void *pkg2_arg)
723 const pkg_t *pkg1 = *(const pkg_tt *)pkg1_arg;
724 const pkg_t *pkg2 = *(const pkg_tt *)pkg2_arg;
726 return (pkg2->idep_count - pkg1->idep_count);
730 qsort_depi(const void *pkg1_arg, const void *pkg2_arg)
732 const pkg_t *pkg1 = *(const pkg_tt *)pkg1_arg;
733 const pkg_t *pkg2 = *(const pkg_tt *)pkg2_arg;
735 return (pkg2->depi_count - pkg1->depi_count);
739 * Frontend starts a pkg up on a worker
741 * WorkerMutex must be held.
744 startworker(pkg_t *pkg, worker_t *work)
746 switch(work->state) {
748 pthread_create(&work->td, NULL, childBuilderThread, work);
749 work->state = WORKER_IDLE;
752 dlog(DLOG_ALL, "[%03d] %s START\n", work->index, pkg->portdir);
754 pkg->flags |= PKGF_RUNNING;
756 pthread_cond_signal(&work->cond);
767 dfatal("startworker: Unexpected state %d for worker %d",
768 work->state, work->index);
774 cleanworker(worker_t *work)
776 work->state = WORKER_PENDING;
778 work->accum_error = 0;
779 work->start_time = time(NULL);
783 * Frontend finishes up a completed pkg on a worker.
785 * If the worker is in a FAILED state we clean the pkg out but (for now)
786 * leave it in its failed state so we can debug. At this point
787 * workercomplete() will be called every once in a while on the state
788 * and we have to deal with the NULL pkg.
790 * WorkerMutex must be held.
793 workercomplete(worker_t *work)
798 * Steady state FAILED case.
800 if (work->state == WORKER_FAILED) {
801 if (work->pkg == NULL)
806 * Process pkg out of the worker
809 if (pkg->flags & (PKGF_ERROR|PKGF_NOBUILD)) {
810 pkg->flags |= PKGF_FAILURE;
813 * This NOBUILD condition XXX can occur if the package is
814 * not allowed to be built.
816 if (pkg->flags & PKGF_NOBUILD) {
820 reason = buildskipreason(NULL, pkg);
821 dlog(DLOG_SKIP, "[%03d] %s skipped due to %s\n",
822 work->index, pkg->portdir, reason);
826 dlog(DLOG_FAIL, "[%03d] %s build-failed\n",
827 work->index, pkg->portdir);
835 t = time(NULL) - work->start_time;
841 "[%03d] %s\tbuild-succeded in %02d:%02d:%02d\n",
842 work->index, pkg->portdir, h, m, s);
843 pkg->flags |= PKGF_SUCCESS;
847 pkg->flags &= ~PKGF_BUILDLIST;
848 dlog(DLOG_ALL, "[%03d] %s FINISHED (%s)\n",
851 ((pkg->flags & PKGF_SUCCESS) ? "success" : "failure"));
852 pkg->flags &= ~PKGF_RUNNING;
856 if (work->state == WORKER_FAILED) {
857 dlog(DLOG_ALL, "[%03d] XXX/XXX WORKER IS IN A FAILED STATE\n",
860 } else if (work->flags & WORKERF_FREEZE) {
861 dlog(DLOG_ALL, "[%03d] XXX/XXX WORKER FROZEN BY REQUEST\n",
863 work->state = WORKER_FROZEN;
865 work->state = WORKER_IDLE;
870 * Wait for one or more workers to complete.
872 * WorkerMutex must be held.
875 waitbuild(int whilematch, int dynamicmax)
877 static time_t wblast_time;
886 while (RunningWorkers == whilematch) {
887 for (i = 0; i < MaxWorkers; ++i) {
888 work = &WorkerAry[i];
889 if (work->state == WORKER_DONE ||
890 work->state == WORKER_FAILED) {
891 workercomplete(work);
893 pthread_cond_signal(&work->cond);
899 if (RunningWorkers == whilematch) {
900 clock_gettime(CLOCK_REALTIME, &ts);
903 pthread_cond_timedwait(&WorkerCond, &WorkerMutex, &ts);
907 * Dynamically reduce MaxWorkers based on the load. When
908 * the load exceeds 2 x ncpus we reduce the number of workers
909 * up to 75% of MaxWorkers @ (5 x ncpus) load.
911 * Dynamically reduce MaxWorkers based on swap use, starting
912 * at 10% swap and up to 75% of MaxWorkers at 40% swap.
914 * NOTE! Generally speaking this allows more workers to be
915 * configured which helps in two ways. First it allows
916 * a higher build rate for smaller packages. Second
917 * it allows dsynth to ratchet-down the number of slots
918 * when large packages are forcing the load up.
920 * A high load doesn't hurt efficiency, but swap usage
921 * due to loading the tmpfs in lots of worker slots up
922 * with tons of pkg install's (pre-reqs for a build)
923 * does. Reducing the number of worker slots has a
924 * huge beneficial effect on reducing swap use / paging.
927 if (dynamicmax && (wblast_time == 0 ||
928 (unsigned)(t - wblast_time) >= 5)) {
929 double min_load = 1.5 * NumCores;
930 double max_load = 5.0 * NumCores;
931 double min_swap = 0.10;
932 double max_swap = 0.40;
941 getloadavg(dload, 3);
942 dswap = getswappct(&noswap);
944 if (dload[0] < min_load) {
946 } else if (dload[0] <= max_load) {
947 reduce1 = 75 * (dload[0] - min_load) /
948 (max_load - min_load);
950 reduce1 = MaxWorkers * 75 / 100;
953 if (dswap < min_swap) {
955 } else if (dswap <= max_swap) {
956 reduce2 = 75 * (dswap - min_swap) /
957 (max_swap - min_swap);
959 reduce2 = MaxWorkers * 75 / 100;
963 * Priority reduction, convert to DynamicMaxWorkers
965 if (reduce1 > reduce2)
969 reduce = MaxWorkers - reduce;
972 if (reduce > MaxWorkers)
978 if (reduce > DynamicMaxWorkers + 1) {
979 reduce = DynamicMaxWorkers + 1;
983 * Stop waiting if DynamicMaxWorkers is going
986 if (DynamicMaxWorkers < reduce)
992 if (DynamicMaxWorkers != reduce) {
994 "[XXX] Load=%-6.2f(-%-2d) "
995 "Swappct=%-3.2f(-%-2d) "
999 dswap * 100.0, reduce2,
1002 DynamicMaxWorkers = reduce;
1010 * Worker pthread (WorkerAry)
1012 * This thread belongs to the dsynth master process and handled a worker slot.
1013 * (this_thread) -> WORKER fork/exec (WorkerPocess) -> (pty) -> sub-processes.
1016 childBuilderThread(void *arg)
1018 char *envary[1] = { NULL };
1019 worker_t *work = arg;
1024 volatile int dowait;
1029 pthread_mutex_lock(&WorkerMutex);
1030 while (work->terminate == 0) {
1033 switch(work->state) {
1036 case WORKER_PENDING:
1038 * Fork the management process for the pkg operation
1039 * on this worker slot.
1041 * This process will set up the environment, do the
1042 * mounts, will become the reaper, and will process
1043 * pipe commands and handle chroot operations.
1045 * NOTE: If SOCK_CLOEXEC is not supported WorkerMutex
1046 * is sufficient to interlock F_SETFD FD_CLOEXEC
1049 ddassert(work->pkg);
1050 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
1051 PF_UNSPEC, work->fds)) {
1052 dfatal_errno("socketpair() during worker fork");
1054 snprintf(slotbuf, sizeof(slotbuf),
1056 snprintf(fdbuf, sizeof(fdbuf),
1058 snprintf(flagsbuf, sizeof(flagsbuf),
1059 "%d", DebugStopMode);
1065 * We pass the salve descriptor in fd 3 and close all
1066 * other descriptors for security.
1068 pthread_mutex_unlock(&WorkerMutex);
1071 close(work->fds[0]);
1072 dup2(work->fds[1], 3);
1074 fcntl(3, F_SETFD, 0);
1075 execle(DSynthExecPath, DSynthExecPath,
1076 "WORKER", slotbuf, fdbuf,
1077 work->pkg->portdir, work->pkg->pkgfile,
1080 write(2, "EXECLE FAILURE\n", 15);
1083 pthread_mutex_lock(&WorkerMutex);
1084 close(work->fds[1]);
1085 work->phase = PHASE_PENDING;
1088 work->state = WORKER_RUNNING;
1090 case WORKER_RUNNING:
1092 * Poll for status updates, if NULL is returned
1093 * and status is non-zero, the communications link
1094 * failed unexpectedly.
1097 pthread_mutex_unlock(&WorkerMutex);
1098 status = ipcreadmsg(work->fds[0], &wmsg);
1099 pthread_mutex_lock(&WorkerMutex);
1103 * Normal message, can include normal
1104 * termination which changes us over
1109 case WMSG_CMD_INSTALL_PKGS:
1110 wmsg.cmd = WMSG_RES_INSTALL_PKGS;
1111 wmsg.status = childInstallPkgDeps(work);
1112 pthread_mutex_unlock(&WorkerMutex);
1113 ipcwritemsg(work->fds[0], &wmsg);
1114 pthread_mutex_lock(&WorkerMutex);
1116 case WMSG_CMD_STATUS_UPDATE:
1117 work->phase = wmsg.phase;
1118 work->lines = wmsg.lines;
1120 case WMSG_CMD_SUCCESS:
1121 work->flags |= WORKERF_SUCCESS;
1123 case WMSG_CMD_FAILURE:
1124 work->flags |= WORKERF_FAILURE;
1126 case WMSG_CMD_FREEZEWORKER:
1127 work->flags |= WORKERF_FREEZE;
1135 close(work->fds[0]);
1136 pthread_mutex_unlock(&WorkerMutex);
1137 while (waitpid(work->pid, &status, 0) < 0 &&
1141 pthread_mutex_lock(&WorkerMutex);
1143 if (work->flags & WORKERF_SUCCESS) {
1144 pkg->flags |= PKGF_SUCCESS;
1145 work->state = WORKER_DONE;
1146 } else if (work->flags & WORKERF_FAILURE) {
1147 pkg->flags |= PKGF_FAILURE;
1148 work->state = WORKER_DONE;
1150 pkg->flags |= PKGF_FAILURE;
1151 work->state = WORKER_FAILED;
1153 work->flags |= WORKERF_STATUS_UPDATE;
1154 pthread_cond_signal(&WorkerCond);
1159 * pkg remains attached until frontend processes the
1160 * completion. The frontend will then set the state
1166 * A worker failure means that the worker did not
1167 * send us a WMSG_CMD_SUCCESS or WMSG_CMD_FAILURE
1168 * ipc before terminating.
1170 * We just sit in this state until the front-end
1171 * does something about it.
1175 dfatal("worker: Unexpected state %d for worker %d",
1176 work->state, work->index);
1182 * The dsynth frontend will poll us approximately once
1183 * a second (its variable).
1186 pthread_cond_wait(&work->cond, &WorkerMutex);
1190 * Scrap the comm socket if running, this should cause the worker
1191 * process to kill its sub-programs and cleanup.
1193 if (work->state == WORKER_RUNNING) {
1194 pthread_mutex_unlock(&WorkerMutex);
1195 close(work->fds[0]);
1196 while (waitpid(work->pid, &status, 0) < 0 &&
1198 pthread_mutex_lock(&WorkerMutex);
1204 work->state = WORKER_EXITING;
1205 pthread_cond_signal(&WorkerCond);
1206 pthread_mutex_unlock(&WorkerMutex);
1212 * Install all the binary packages (we have already built them) that
1213 * the current work package depends on, without duplicates, in a script
1214 * which will be run from within the specified work jail.
1216 * Locked by WorkerMutex (global)
1219 childInstallPkgDeps(worker_t *work)
1224 if (PKGLIST_EMPTY(&work->pkg->idepon_list))
1227 asprintf(&buf, "%s/tmp/dsynth_install_pkgs", work->basedir);
1228 fp = fopen(buf, "w");
1229 ddassert(fp != NULL);
1230 fprintf(fp, "#!/bin/sh\n");
1232 fchmod(fileno(fp), 0755);
1234 childInstallPkgDeps_recurse(fp, &work->pkg->idepon_list, 0);
1235 childInstallPkgDeps_recurse(fp, &work->pkg->idepon_list, 1);
1236 fprintf(fp, "\nexit 0\n");
1244 childInstallPkgDeps_recurse(FILE *fp, pkglink_t *list, int undoit)
1249 PKGLIST_FOREACH(link, list) {
1253 if (pkg->dsynth_install_flg == 1) {
1254 pkg->dsynth_install_flg = 0;
1255 childInstallPkgDeps_recurse(fp,
1261 if (pkg->dsynth_install_flg) {
1262 if (DebugOpt && pkg->pkgfile) {
1263 fprintf(fp, "echo 'AlreadyHave %s'\n",
1269 childInstallPkgDeps_recurse(fp, &pkg->idepon_list, undoit);
1270 if (pkg->dsynth_install_flg)
1272 pkg->dsynth_install_flg = 1;
1275 * If this is a dummy node with no package, the originator
1276 * is requesting a flavored package. We select the default
1277 * flavor which we presume is the first one.
1279 if (pkg->pkgfile == NULL && (pkg->flags & PKGF_DUMMY)) {
1280 pkg_t *spkg = pkg->idepon_list.next->pkg;
1285 "echo 'DUMMY use %s (%p)'\n",
1286 pkg->portdir, pkg->pkgfile);
1289 "echo 'CANNOT FIND DEFAULT FLAVOR "
1296 * Generate package installation command
1299 fprintf(fp, "echo 'Installing /packages/All/%s'\n",
1301 fprintf(fp, "pkg install -q -y /packages/All/%s "
1305 fprintf(fp, "echo 'CANNOT FIND PKG FOR %s'\n",
1312 * Worker process interactions.
1314 * The worker process is responsible for managing the build of a single
1315 * package. It is exec'd by the master dsynth and only loads the
1318 * This process does not run in the chroot. It will become the reaper for
1319 * all sub-processes and it will enter the chroot to execute various phases.
1320 * It catches SIGINTR, SIGHUP, and SIGPIPE and will iterate, terminate, and
1321 * reap all sub-process upon kill or exit.
1323 * The command line forwarded to this function is:
1325 * WORKER slot# socketfd portdir/subdir
1331 * SSL_NO_VERIFY_PEER=1
1332 * USE_PACKAGE_DEPENDS_ONLY=1 (exec_phase_depends)
1334 * PORT_DBDIR=/options For ports options
1335 * PACKAGE_BUILDING=yes Don't build packages that aren't legally
1336 * buildable for a binary repo.
1337 * PKG_DBDIR=/var/db/pkg
1338 * PKG_CACHEDIR=/var/cache/pkg
1339 * PKG_CREATE_VERBOSE=yes Ensure periodic output during packaging
1340 * (custom environment)
1341 * PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
1342 * UNAME_s=DragonFly (example)
1343 * UNAME_v=DragonFly 5.7-SYNTH (example)
1344 * UNAME_p=x86_64 (example)
1345 * UNAME_m=x86_64 (example)
1346 * UNAME_r=5.7-SYNTH (example)
1347 * NO_DEPENDS=yes (exec_phase)
1348 * DISTDIR=/distfiles
1349 * WRKDIRPREFIX=/construction
1351 * MAKE_JOBS_NUMBER=n
1355 * /usr/local/sbin/pkg-static install /packages/All/<the pkg pkg>
1356 * /usr/local/sbin/pkg-static install /packages/All/<pkg>
1357 * (for all dependencies)
1359 * PHASES: make -C path FLAVOR=flavor <phase>
1377 * package e.g. /construction/lang/perl5.28/pkg/perl5-5.28.2.txz
1378 * install-mtree (skipped)
1380 * deinstall (skipped)
1384 WorkerProcess(int ac, char **av)
1393 int do_install_phase;
1406 dlog(DLOG_ALL, "WORKER PROCESS %d- bad arguments\n", getpid());
1409 slot = strtol(av[1], NULL, 0);
1410 fd = strtol(av[2], NULL, 0); /* master<->slave messaging */
1413 flavor = strchr(portdir, '@');
1416 DebugStopMode = strtol(av[5], NULL, 0);
1418 bzero(&wmsg, sizeof(wmsg));
1420 setproctitle("WORKER [%02d] STARTUP %s", slot, portdir);
1422 if (strcmp(portdir, "ports-mgmt/pkg") == 0)
1425 signal(SIGTERM, phaseTerminateSignal);
1426 signal(SIGINT, phaseTerminateSignal);
1427 signal(SIGHUP, phaseTerminateSignal);
1430 * Set up the environment
1432 setenv("TERM", "dumb", 1);
1433 setenv("USER", "root", 1);
1434 setenv("HOME", "/root", 1);
1435 setenv("LANG", "C", 1);
1436 setenv("SSL_NO_VERIFY_PEER", "1", 1);
1437 setenv("USE_PACKAGE_DEPENDS_ONLY", "1", 1);
1438 setenv("PORTSDIR", "/xports", 1);
1439 setenv("PORT_DBDIR", "/options", 1);
1440 setenv("PKG_DBDIR", "/var/db/pkg", 1);
1441 setenv("PKG_CACHEDIR", "/var/cache/pkg", 1);
1442 setenv("PKG_SUFX", USE_PKG_SUFX, 1);
1446 setenv("_PERL5_FROM_BIN", "5.28.2", 1);
1447 setenv("OPSYS", OperatingSystemName, 1);
1450 setenv("DFLYVERSION", "5.7.0", 1);
1451 setenv("OSVERSION", "9999999", 1);
1455 "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin",
1458 setenv("UNAME_s", OperatingSystemName, 1);
1459 setenv("UNAME_v", VersionName, 1);
1460 setenv("UNAME_p", ArchitectureName, 1);
1461 setenv("UNAME_m", MachineName, 1);
1462 setenv("UNAME_r", ReleaseName, 1);
1464 setenv("NO_DEPENDS", "yes", 1);
1465 setenv("DISTDIR", "/distfiles", 1);
1466 setenv("WRKDIRPREFIX", "/construction", 1);
1467 setenv("BATCH", "yes", 1);
1470 * Special consideration
1472 * PACKAGE_BUILDING - Disallow packaging ports which do not allow
1473 * for binary distribution.
1475 * PKG_CREATE_VERBOSE - Ensure periodic output during the packaging
1476 * process to avoid a watchdog timeout.
1479 setenv("PACKAGE_BUILDING", "yes", 1);
1480 setenv("PKG_CREATE_VERBOSE", "yes", 1);
1482 asprintf(&buf, "%d", MaxJobs);
1483 setenv("MAKE_JOBS_NUMBER", buf, 1);
1487 setenv("FLAVOR", flavor, 1);
1490 * Load environment from profile
1492 for (benv = BuildEnv; benv; benv = benv->next) {
1493 if (DebugOpt >= 2) {
1494 dlog(DLOG_ALL, "[%03d] ENV %s=%s\n",
1495 slot, benv->label, benv->data);
1497 setenv(benv->label, benv->data, 1);
1503 if (procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) < 0)
1504 dfatal_errno("procctl() - Cannot become reaper");
1507 * Initialize a worker structure
1511 bzero(&pkg, sizeof(pkg));
1512 pkg.portdir = portdir; /* sans flavor */
1513 pkg.pkgfile = pkgfile;
1514 if (strchr(portdir, '/'))
1515 len = strchr(portdir, '/') - portdir;
1522 asprintf(&pkg.logfile,
1523 "%s/%*.*s___%s%s%s.log",
1524 LogsPath, len, len, portdir,
1525 ((portdir[len] == '/') ? portdir + len + 1 : portdir + len),
1526 (flavor ? "@" : ""),
1527 (flavor ? flavor : ""));
1528 tmpfd = open(pkg.logfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1530 dlog(DLOG_ALL, "[%03d] %s LOGFILE %s\n",
1531 slot, pkg.portdir, pkg.logfile);
1534 dlog(DLOG_ALL, "[%03d] LOGFILE %s (create failed)\n",
1539 * Setup the work structure. Because this is an exec'd sub-process,
1540 * there is only one work structure.
1542 work = &WorkerAry[0];
1543 work->flavor = flavor;
1546 work->start_time = time(NULL);
1549 * Do mounts and start the phases
1552 setproctitle("WORKER [%02d] MOUNTS %s", slot, portdir);
1553 DoWorkerMounts(work);
1555 wmsg.cmd = WMSG_CMD_INSTALL_PKGS;
1556 ipcwritemsg(fd, &wmsg);
1557 status = ipcreadmsg(fd, &wmsg);
1558 if (status < 0 || wmsg.cmd != WMSG_RES_INSTALL_PKGS)
1559 dfatal("pkg installation handshake failed");
1560 do_install_phase = wmsg.status;
1562 wmsg.cmd = WMSG_CMD_STATUS_UPDATE;
1563 wmsg.phase = PHASE_INSTALL_PKGS;
1566 status = ipcwritemsg(fd, &wmsg);
1569 dophase(work, &wmsg,
1570 WDOG5, PHASE_PACKAGE, "package");
1572 if (do_install_phase) {
1573 dophase(work, &wmsg,
1574 WDOG4, PHASE_INSTALL_PKGS, "setup");
1576 dophase(work, &wmsg,
1577 WDOG2, PHASE_CHECK_SANITY, "check-sanity");
1578 dophase(work, &wmsg,
1579 WDOG2, PHASE_PKG_DEPENDS, "pkg-depends");
1580 dophase(work, &wmsg,
1581 WDOG7, PHASE_FETCH_DEPENDS, "fetch-depends");
1582 dophase(work, &wmsg,
1583 WDOG7, PHASE_FETCH, "fetch");
1584 dophase(work, &wmsg,
1585 WDOG2, PHASE_CHECKSUM, "checksum");
1586 dophase(work, &wmsg,
1587 WDOG3, PHASE_EXTRACT_DEPENDS, "extract-depends");
1588 dophase(work, &wmsg,
1589 WDOG3, PHASE_EXTRACT, "extract");
1590 dophase(work, &wmsg,
1591 WDOG2, PHASE_PATCH_DEPENDS, "patch-depends");
1592 dophase(work, &wmsg,
1593 WDOG2, PHASE_PATCH, "patch");
1594 dophase(work, &wmsg,
1595 WDOG5, PHASE_BUILD_DEPENDS, "build-depends");
1596 dophase(work, &wmsg,
1597 WDOG5, PHASE_LIB_DEPENDS, "lib-depends");
1598 dophase(work, &wmsg,
1599 WDOG3, PHASE_CONFIGURE, "configure");
1600 dophase(work, &wmsg,
1601 WDOG9, PHASE_BUILD, "build");
1602 dophase(work, &wmsg,
1603 WDOG5, PHASE_RUN_DEPENDS, "run-depends");
1604 dophase(work, &wmsg,
1605 WDOG5, PHASE_STAGE, "stage");
1607 dophase(work, &wmsg,
1608 WDOG5, PHASE_TEST, "test");
1610 dophase(work, &wmsg,
1611 WDOG1, PHASE_CHECK_PLIST, "check-plist");
1612 dophase(work, &wmsg,
1613 WDOG5, PHASE_PACKAGE, "package");
1615 dophase(work, &wmsg,
1616 WDOG5, PHASE_INSTALL_MTREE, "install-mtree");
1617 dophase(work, &wmsg,
1618 WDOG5, PHASE_INSTALL, "install");
1619 dophase(work, &wmsg,
1620 WDOG5, PHASE_DEINSTALL, "deinstall");
1624 if (MasterPtyFd >= 0) {
1629 setproctitle("WORKER [%02d] CLEANUP %s", slot, portdir);
1632 * Copy the package to the repo.
1634 if (work->accum_error == 0) {
1638 asprintf(&b1, "%s/construction/%s/pkg/%s",
1639 work->basedir, pkg.portdir, pkg.pkgfile);
1640 asprintf(&b2, "%s/%s", RepositoryPath, pkg.pkgfile);
1641 if (copyfile(b1, b2)) {
1642 ++work->accum_error;
1643 dlog(DLOG_ALL, "[%03d] %s Unable to copy %s to %s\n",
1644 work->index, pkg.portdir, b1, b2);
1651 * Unmount, unless we are in DebugStopMode.
1653 if (DebugStopMode == 0)
1654 DoWorkerUnmounts(work);
1657 * Send completion status to master dsynth worker thread.
1659 if (work->accum_error) {
1660 wmsg.cmd = WMSG_CMD_FAILURE;
1662 wmsg.cmd = WMSG_CMD_SUCCESS;
1664 ipcwritemsg(fd, &wmsg);
1665 if (DebugStopMode) {
1666 wmsg.cmd = WMSG_CMD_FREEZEWORKER;
1667 ipcwritemsg(fd, &wmsg);
1672 dophase(worker_t *work, wmsg_t *wmsg, int wdog, int phaseid, const char *phase)
1674 pkg_t *pkg = work->pkg;
1688 if (work->accum_error)
1690 setproctitle("WORKER [%02d] %8.8s %s",
1691 work->index, phase, pkg->portdir);
1692 wmsg->phase = phaseid;
1693 if (ipcwritemsg(work->fds[0], wmsg) < 0) {
1694 dlog(DLOG_ALL, "[%03d] %s Lost Communication with dsynth, "
1695 "aborting worker\n",
1696 work->index, pkg->portdir);
1697 ++work->accum_error;
1702 * Execute the port make command in chroot on a pty
1706 if (MasterPtyFd >= 0) {
1709 slavefd = open(ptsname(MasterPtyFd), O_RDWR);
1710 dassert_errno(slavefd >= 0, "Cannot open slave pty");
1718 pid = forkpty(&MasterPtyFd, NULL, NULL, NULL);
1723 if (chdir(work->basedir) < 0)
1724 dfatal_errno("chdir in phase initialization");
1725 if (chroot(work->basedir) < 0)
1726 dfatal_errno("chroot in phase initialization");
1729 * We have a choice here on how to handle stdin (fd 0).
1730 * We can leave it connected to the pty in which case
1731 * the build will just block if it tries to ask a
1732 * question (and the watchdog will kill it, eventually),
1733 * or we can try to EOF the pty, or we can attach /dev/null
1739 fd = open("/dev/null", O_RDWR);
1740 dassert_errno(fd >= 0, "cannot open /dev/null");
1748 * Execute the appropriate command.
1751 case PHASE_INSTALL_PKGS:
1752 snprintf(buf, sizeof(buf), "/tmp/dsynth_install_pkgs");
1753 execl(buf, buf, NULL);
1756 snprintf(buf, sizeof(buf), "/xports/%s", pkg->portdir);
1757 execl(MAKE_BINARY, MAKE_BINARY, "-C", buf, phase, NULL);
1762 fcntl(MasterPtyFd, F_SETFL, O_NONBLOCK);
1765 dlog(DLOG_ALL, "[%03d] %s Fork Failed\n",
1766 work->index, pkg->logfile);
1767 ++work->accum_error;
1773 fdlog = open(pkg->logfile, O_RDWR|O_CREAT|O_APPEND, 0644);
1775 dlog(DLOG_ALL, "[%03d] %s Cannot open logfile '%s': %s\n",
1776 work->index, pkg->portdir,
1777 pkg->logfile, strerror(errno));
1779 start_time = time(NULL);
1780 last_time = start_time;
1781 wdog_time = start_time;
1786 ms = mptylogpoll(MasterPtyFd, fdlog, wmsg, &wdog_time);
1787 if (ms == MPTY_FAILED) {
1789 "[%03d] %s lost pty in phase %s, terminating\n",
1790 work->index, pkg->portdir, phase);
1797 * Generally speaking update status once a second.
1798 * This also allows us to detect if the management
1799 * dsynth process has gone away.
1801 next_time = time(NULL);
1802 if (next_time != last_time) {
1808 * Send status update to the worker management thread
1809 * in the master dsynth process. Remember, *WE* are
1810 * the worker management process sub-fork.
1812 if (ipcwritemsg(work->fds[0], wmsg) < 0)
1814 last_time = next_time;
1819 getloadavg(dload, 3);
1820 dv = dload[2] / NumCores;
1821 if (dv < (double)NumCores) {
1824 if (dv > 4.0 * NumCores)
1825 dv = 4.0 * NumCores;
1826 wdog_scaled = wdog * dv / NumCores;
1832 if (next_time - wdog_time >= wdog_scaled * 60) {
1833 snprintf(buf, sizeof(buf),
1835 "WATCHDOG TIMEOUT FOR %s in %s "
1836 "after %d minutes\n"
1839 pkg->portdir, phase, wdog_scaled, pid);
1841 write(fdlog, buf, strlen(buf));
1843 "[%03d] WATCHDOG TIMEOUT FOR %s in %s "
1844 "after %d minutes (%d min scaled)\n",
1845 work->index, pkg->portdir, phase,
1848 ++work->accum_error;
1854 * Check process exit. Normally the pty will EOF
1855 * but if background processes remain we need to
1856 * check here to see if our primary exec is done,
1857 * so we can break out and reap those processes.
1859 * Generally reap any other processes we have inherited
1860 * while we are here.
1863 wpid = wait3(&status, WNOHANG, NULL);
1864 } while (wpid > 0 && wpid != pid);
1865 if (wpid == pid && WIFEXITED(status)) {
1871 next_time = time(NULL);
1873 setproctitle("WORKER [%02d] EXITREAP %s",
1874 work->index, pkg->portdir);
1877 * We usually get here due to a mpty EOF, but not always as there
1878 * could be persistent processes still holding the slave. Finish
1879 * up getting the exit status for the main process we are waiting
1880 * on and clean out any data left on the MasterPtyFd (as it could
1881 * be blocking the exit).
1883 while (wpid_reaped == 0) {
1884 (void)mptylogpoll(MasterPtyFd, fdlog, wmsg, &wdog_time);
1885 wpid = waitpid(pid, &status, WNOHANG);
1886 if (wpid == pid && WIFEXITED(status)) {
1890 if (wpid < 0 && errno != EINTR) {
1895 * Safety. The normal phase waits until the fork/exec'd
1896 * pid finishes, causing a pty EOF on exit (the slave
1897 * descriptor is closed by the kernel on exit so the
1898 * process should already have exited).
1900 * However, it is also possible to get here if the pty fails
1901 * for some reason. In this case, make sure that the process
1908 * Clean out anything left on the pty but don't wait around
1909 * because there could be background processes preventing the
1910 * slave side from closing.
1912 while (mptylogpoll(MasterPtyFd, fdlog, wmsg, &wdog_time) == MPTY_DATA)
1916 * Report on the exit condition. If the pid was somehow lost
1917 * (probably due to someone gdb'ing the process), assume an error.
1920 if (WEXITSTATUS(status)) {
1921 dlog(DLOG_ALL, "[%03d] %s Build phase '%s' "
1923 work->index, pkg->portdir, phase,
1924 WEXITSTATUS(status));
1925 ++work->accum_error;
1928 dlog(DLOG_ALL, "[%03d] %s Build phase '%s' failed - lost pid\n",
1929 work->index, pkg->portdir, phase);
1930 ++work->accum_error;
1934 * Kill any processes still running (sometimes processes end up in
1935 * the background during a dports build), and clean up any other
1936 * children that we have inherited.
1949 last_time = next_time - start_time;
1951 m = last_time / 60 % 60;
1952 h = last_time / 3600;
1954 fp = fdopen(fdlog, "a");
1956 dlog(DLOG_ALL, "[%03d] %s Cannot fdopen fdlog: %s %d\n",
1957 work->index, pkg->portdir,
1958 strerror(errno), fstat(fdlog, &st));
1963 fprintf(fp, "--------\n");
1964 if (work->accum_error) {
1965 fprintf(fp, "PHASE %s FAILED %02d:%02d:%02d\n",
1968 fprintf(fp, "PHASE %s SUCCEEDED %02d:%02d:%02d\n",
1971 last_time = next_time - work->start_time;
1973 m = last_time / 60 % 60;
1974 h = last_time / 3600;
1975 if (phaseid == PHASE_PACKAGE) {
1976 fprintf(fp, "TOTAL TIME %02d:%02d:%02d\n", h, m, s);
1978 fprintf(fp, "--------\n");
1989 struct reaper_status rs;
1992 while (procctl(P_PID, getpid(), PROC_REAP_STATUS, &rs) == 0) {
1993 if ((rs.flags & PROC_REAP_ACQUIRE) == 0)
1995 if (rs.pid_head < 0)
1997 if (kill(rs.pid_head, SIGKILL) == 0) {
1998 while (waitpid(rs.pid_head, &status, 0) < 0)
2002 while (wait3(&status, 0, NULL) > 0)
2007 phaseTerminateSignal(int sig __unused)
2009 if (MasterPtyFd >= 0)
2012 kill(SigPid, SIGKILL);
2015 DoWorkerUnmounts(SigWork);
2021 buildskipreason(pkglink_t *parent, pkg_t *pkg)
2025 char *reason = NULL;
2032 PKGLIST_FOREACH(link, &pkg->idepon_list) {
2036 if ((scan->flags & (PKGF_ERROR | PKGF_NOBUILD)) == 0)
2038 if (scan->flags & PKGF_NOBUILD) {
2040 stack.next = parent;
2041 ptr = buildskipreason(&stack, scan);
2042 len = strlen(scan->portdir) + strlen(ptr) + 8;
2043 reason = realloc(reason, tot + len);
2044 snprintf(reason + tot, len, "%s->%s",
2045 scan->portdir, ptr);
2048 len = strlen(scan->portdir) + 8;
2049 reason = realloc(reason, tot + len);
2050 snprintf(reason + tot, len, "%s", scan->portdir);
2054 * Don't try to print the entire graph
2058 tot += strlen(reason + tot);
2059 reason[tot++] = ' ';
2066 * The master ptyfd is in non-blocking mode. Drain up to 1024 bytes
2067 * and update wmsg->lines and *wdog_timep as appropriate.
2069 * This function will poll, stalling up to 1 second.
2072 mptylogpoll(int ptyfd, int fdlog, wmsg_t *wmsg, time_t *wdog_timep)
2079 pfd.events = POLLIN;
2082 poll(&pfd, 1, 1000);
2084 r = read(ptyfd, buf, sizeof(buf));
2086 *wdog_timep = time(NULL);
2087 if (r > 0 && fdlog >= 0)
2088 write(fdlog, buf, r);
2096 if (errno != EINTR && errno != EAGAIN)
2099 } else if (r == 0) {
2107 * Copy a (package) file from (src) to (dst), use an intermediate file and
2108 * rename to ensure that interruption does not leave us with a corrupt
2111 * This is called by the WORKER process.
2113 * (dsynth management thread -> WORKER process -> sub-processes)
2115 #define COPYBLKSIZE 32768
2118 copyfile(char *src, char *dst)
2127 asprintf(&tmp, "%s.new", dst);
2128 buf = malloc(COPYBLKSIZE);
2130 fd1 = open(src, O_RDONLY|O_CLOEXEC);
2131 fd2 = open(tmp, O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0644);
2132 while ((r = read(fd1, buf, COPYBLKSIZE)) > 0) {
2133 if (write(fd2, buf, r) != r)
2143 if (rename(tmp, dst)) {