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
38 #include <sys/types.h>
41 #include <sys/sysctl.h>
42 #include <sys/socket.h>
43 #include <sys/mount.h>
44 #include <sys/procctl.h>
62 * More esoteric headers
64 #include <libutil.h> /* forkpty() */
65 #include <arpa/inet.h> /* ntohl() */
66 #include <elf.h> /* try to get elf info */
70 #define DSYNTH_VERSION "1.01"
71 #define MAXWORKERS 1024
72 #define MAXLOGLINES 1024
73 #define MAXJOBS 8192 /* just used for -j sanity */
74 #define MAXBULK MAXWORKERS
76 #define MAKE_BINARY "/usr/bin/make"
77 #define PKG_BINARY "/usr/local/sbin/pkg"
78 #define MOUNT_BINARY "/sbin/mount"
79 #define MOUNT_NULLFS_BINARY "/sbin/mount_null"
80 #define MOUNT_TMPFS_BINARY "/sbin/mount_tmpfs"
81 #define MOUNT_DEVFS_BINARY "/sbin/mount_devfs"
82 #define MOUNT_PROCFS_BINARY "/sbin/mount_procfs"
83 #define UMOUNT_BINARY "/sbin/umount"
85 #define STATS_FILE "monitor.dat" /* under LogsPath */
86 #define STATS_LOCKFILE "monitor.lk" /* under LogsPath */
88 #define ONEGB (1024L * 1024 * 1024)
89 #define DISABLED_STR "disabled"
92 * This can be ".tar", ".tgz", ".txz", or ".tbz".
94 * .tar - very fast but you'll need 1TB+ of storage just for the package files.
95 * .txz - very compact but decompression speed is horrible.
96 * .tgz - reasonable compression, extremely fast decompression. Roughly
97 * 1.1x to 2.0x the size of a .txz, but decompresses 10x faster.
98 * .tbz - worse than .tgz generally
100 #define USE_PKG_SUFX ".tgz"
105 typedef struct pkglink {
106 struct pkglink *next;
107 struct pkglink *prev;
112 #define DEP_TYPE_FETCH 1
113 #define DEP_TYPE_EXT 2
114 #define DEP_TYPE_PATCH 3
115 #define DEP_TYPE_BUILD 4
116 #define DEP_TYPE_LIB 5
117 #define DEP_TYPE_RUN 6
120 * Describes a [flavored] package
123 struct pkg *build_next; /* topology inversion build list */
124 struct pkg *bnext; /* linked list from bulk return */
125 struct pkg *hnext1; /* hash based on portdir */
126 struct pkg *hnext2; /* hash based on pkgfile */
127 pkglink_t idepon_list; /* I need these pkgs */
128 pkglink_t deponi_list; /* pkgs which depend on me */
129 char *portdir; /* origin name e.g. www/chromium[@flavor] */
130 char *logfile; /* relative logfile path */
131 char *version; /* PKGVERSION - e.g. 3.5.0_1 */
132 char *pkgfile; /* PKGFILE - e.g. flav-blah-3.5.0_1.txz */
133 char *distfiles; /* DISTFILES - e.g. blah-68.0.source.tar.xz */
134 char *distsubdir; /* DIST_SUBDIR- e.g. cabal */
135 char *ignore; /* IGNORE (also covers BROKEN) */
136 char *fetch_deps; /* FETCH_DEPENDS */
137 char *ext_deps; /* EXTRACT_DEPENDS */
138 char *patch_deps; /* PATCH_DEPENDS */
139 char *build_deps; /* BUILD_DEPENDS */
140 char *lib_deps; /* LIB_DEPENDS */
141 char *run_deps; /* RUN_DEPENDS */
142 char *pos_options; /* SELECTED_OPTIONS */
143 char *neg_options; /* DESELECTED_OPTIONS */
144 char *flavors; /* FLAVORS - e.g. py36 py27 */
145 char *uses; /* USES (metaport test) */
146 int make_jobs_number; /* MAKE_JOBS_NUMBER */
147 int use_linux; /* USE_LINUX */
148 int idep_count; /* count recursive idepon build deps */
149 int depi_count; /* count recursive deponi build deps */
150 int depi_depth; /* tree depth who depends on me */
151 int dsynth_install_flg; /* locked with WorkerMutex */
153 int rscan; /* recursive scan flag (serialized use) */
154 size_t pkgfile_size; /* size of pkgfile */
157 #define PKGF_PACKAGED 0x00000001 /* has a repo package */
158 #define PKGF_DUMMY 0x00000002 /* generic root for flavors */
159 #define PKGF_NOTFOUND 0x00000004 /* dport not found */
160 #define PKGF_CORRUPT 0x00000008 /* dport corrupt */
161 #define PKGF_PLACEHOLD 0x00000010 /* pre-entered */
162 #define PKGF_BUILDLIST 0x00000020 /* on build_list */
163 #define PKGF_BUILDLOOP 0x00000040 /* traversal loop test */
164 #define PKGF_BUILDTRAV 0x00000080 /* traversal optimization */
165 #define PKGF_NOBUILD_D 0x00000100 /* can't build - dependency problem */
166 #define PKGF_NOBUILD_S 0x00000200 /* can't build - skipped */
167 #define PKGF_NOBUILD_F 0x00000400 /* can't build - failed */
168 #define PKGF_NOBUILD_I 0x00000800 /* can't build - ignored or broken */
169 #define PKGF_SUCCESS 0x00001000 /* build complete */
170 #define PKGF_FAILURE 0x00002000 /* build complete */
171 #define PKGF_RUNNING 0x00004000 /* build complete */
172 #define PKGF_PKGPKG 0x00008000 /* pkg/pkg-static special */
173 #define PKGF_NOTREADY 0x00010000 /* build_find_leaves() only */
174 #define PKGF_MANUALSEL 0x00020000 /* manually specified */
175 #define PKGF_META 0x00040000 /* USES contains 'metaport' */
176 #define PKGF_ERROR (PKGF_PLACEHOLD | PKGF_CORRUPT | PKGF_NOTFOUND | \
178 #define PKGF_NOBUILD (PKGF_NOBUILD_D | PKGF_NOBUILD_S | PKGF_NOBUILD_F | \
181 #define PKGLIST_EMPTY(pkglink) ((pkglink)->next == (pkglink))
182 #define PKGLIST_FOREACH(var, head) \
183 for (var = (head)->next; var != (head); var = (var)->next)
185 typedef struct bulk {
190 enum { UNLISTED, ONSUBMIT, ONRUN, ISRUNNING, ONRESPONSE } state;
199 pkg_t *list; /* pkgs linked by bnext */
203 * Worker state (up to MAXWORKERS). Each worker operates within a
204 * chroot or jail. A system mirror is setup and the template
209 * /sbin - nullfs (ro)
211 * /libexec - nullfs (ro)
212 * /usr/bin - nullfs (ro)
213 * /usr/include - nullfs (ro)
214 * /usr/lib - nullfs (ro)
215 * /usr/libdata - nullfs (ro)
216 * /usr/libexec - nullfs (ro)
217 * /usr/sbin - nullfs (ro)
218 * /usr/share - nullfs (ro)
219 * /xports - nullfs (ro)
220 * /options - nullfs (ro)
221 * /packages - nullfs (ro)
222 * /distfiles - nullfs (ro)
223 * construction - tmpfs
225 * /boot - nullfs (ro)
226 * /boot/modules.local - tmpfs
227 * /usr/games - nullfs (ro)
228 * /usr/src - nullfs (ro)
231 enum worker_state { WORKER_NONE, WORKER_IDLE, WORKER_PENDING,
232 WORKER_RUNNING, WORKER_DONE, WORKER_FAILED,
233 WORKER_FROZEN, WORKER_EXITING };
234 typedef enum worker_state worker_state_t;
236 enum worker_phase { PHASE_PENDING,
243 PHASE_EXTRACT_DEPENDS,
261 typedef enum worker_phase worker_phase_t;
264 * Watchdog timeouts, in minutes, baseline, scales up with load/ncpus but
265 * does not scale down.
272 #define WDOG6 (60 + 30)
273 #define WDOG7 (60 * 2)
274 #define WDOG8 (60 * 2 + 30)
275 #define WDOG9 (60 * 3)
277 typedef struct worker {
278 int index; /* worker number 0..N-1 */
280 int accum_error; /* cumulative error */
281 int mount_error; /* mount and unmount error */
282 int terminate : 1; /* request sub-thread to terminate */
283 char *basedir; /* base directory including id */
285 pthread_t td; /* pthread */
286 pthread_cond_t cond; /* interlock cond (w/ WorkerMutex) */
288 worker_state_t state; /* general worker state */
289 worker_phase_t phase; /* phase control in childBuilderThread */
294 int fds[2]; /* forked environment process */
296 size_t pkg_dep_size; /* pkg dependency size(s) */
299 #define WORKERF_STATUS_UPDATE 0x0001 /* display update */
300 #define WORKERF_SUCCESS 0x0002 /* completion flag */
301 #define WORKERF_FAILURE 0x0004 /* completion flag */
302 #define WORKERF_FREEZE 0x0008 /* freeze the worker */
304 #define MOUNT_TYPE_MASK 0x000F
305 #define MOUNT_TYPE_TMPFS 0x0001
306 #define MOUNT_TYPE_NULLFS 0x0002
307 #define MOUNT_TYPE_DEVFS 0x0003
308 #define MOUNT_TYPE_PROCFS 0x0004
309 #define MOUNT_TYPE_RW 0x0010
310 #define MOUNT_TYPE_BIG 0x0020
311 #define MOUNT_TYPE_TMP 0x0040
313 #define NULLFS_RO (MOUNT_TYPE_NULLFS)
314 #define NULLFS_RW (MOUNT_TYPE_NULLFS | MOUNT_TYPE_RW)
315 #define PROCFS_RO (MOUNT_TYPE_PROCFS)
316 #define TMPFS_RW (MOUNT_TYPE_TMPFS | MOUNT_TYPE_RW)
317 #define TMPFS_RW_BIG (MOUNT_TYPE_TMPFS | MOUNT_TYPE_RW | \
319 #define DEVFS_RW (MOUNT_TYPE_DEVFS | MOUNT_TYPE_RW)
322 * IPC messages between the worker support thread and the worker process.
324 typedef struct wmsg {
329 worker_phase_t phase;
332 #define WMSG_CMD_STATUS_UPDATE 0x0001
333 #define WMSG_CMD_SUCCESS 0x0002
334 #define WMSG_CMD_FAILURE 0x0003
335 #define WMSG_CMD_INSTALL_PKGS 0x0004
336 #define WMSG_RES_INSTALL_PKGS 0x0005
337 #define WMSG_CMD_FREEZEWORKER 0x0006
340 * Make variables and build environment
342 typedef struct buildenv {
343 struct buildenv *next;
346 char *a1; /* allocations */
347 char *a2; /* allocations */
352 * Operating systems recognized by dsynth
355 OS_UNKNOWN, OS_DRAGONFLY, OS_FREEBSD, OS_NETBSD, OS_LINUX
358 typedef enum os_id os_id_t;
363 #define DLOG_ALL 0 /* Usually stdout when curses disabled */
364 #define DLOG_SUCC 1 /* success_list.log */
365 #define DLOG_FAIL 2 /* failure_list.log */
366 #define DLOG_IGN 3 /* ignored_list.log */
367 #define DLOG_SKIP 4 /* skipped_list.log */
368 #define DLOG_ABN 5 /* abnormal_command_output */
369 #define DLOG_OBS 6 /* obsolete_packages.log */
370 #define DLOG_COUNT 7 /* total number of DLOGs */
371 #define DLOG_MASK 0x0FF
373 #define DLOG_FILTER 0x100 /* Filter out of stdout in non-curses mode */
374 #define DLOG_RED 0x200 /* Print in color */
375 #define DLOG_GRN 0x400 /* Print in color */
376 #define DLOG_STDOUT 0x800 /* And stdout */
378 #define dassert(exp, fmt, ...) \
379 if (!(exp)) dpanic(fmt, ## __VA_ARGS__)
381 #define ddassert(exp) \
382 dassert((exp), "\"%s\" line %d", __FILE__, __LINE__)
384 #define dassert_errno(exp, fmt, ...) \
385 if (!(exp)) dpanic_errno(fmt, ## __VA_ARGS__)
387 #define dlog(which, fmt, ...) \
388 _dlog(which, fmt, ## __VA_ARGS__)
390 #define dlog_tsnl(which, fmt, ...) \
391 _dlog(which, fmt, ## __VA_ARGS__)
393 #define dfatal(fmt, ...) \
394 _dfatal(__FILE__, __LINE__, __func__, 0, fmt, ## __VA_ARGS__)
396 #define dpanic(fmt, ...) \
397 _dfatal(__FILE__, __LINE__, __func__, 2, fmt, ## __VA_ARGS__)
399 #define dfatal_errno(fmt, ...) \
400 _dfatal(__FILE__, __LINE__, __func__, 1, fmt, ## __VA_ARGS__)
402 #define dpanic_errno(fmt, ...) \
403 _dfatal(__FILE__, __LINE__, __func__, 3, fmt, ## __VA_ARGS__)
405 #define ddprintf(tab, fmt, ...) \
406 do { if (DebugOpt >= 2) _ddprintf(tab, fmt, ## __VA_ARGS__); } while(0)
409 * addbuildenv() types
411 #define BENV_ENVIRONMENT 1
412 #define BENV_MAKECONF 2
413 #define BENV_CMDMASK 0x000F
415 #define BENV_PKGLIST 0x0010
418 * WORKER process flags
420 #define WORKER_PROC_DEBUGSTOP 0x0001
421 #define WORKER_PROC_DEVELOPER 0x0002
426 #define DOSTRING(label) #label
427 #define SCRIPTPATH(x) DOSTRING(x)
431 * RunStats satellite modules
433 typedef struct topinfo {
451 typedef struct runstats {
452 struct runstats *next;
456 void (*update)(worker_t *work, const char *portdir);
457 void (*updateTop)(topinfo_t *info);
458 void (*updateLogs)(void);
462 typedef struct monitorlog {
468 int buf_discard_mode;
472 extern runstats_t NCursesRunStats;
473 extern runstats_t MonitorRunStats;
474 extern runstats_t HtmlRunStats;
476 extern int BuildCount;
477 extern int BuildTotal;
478 extern int BuildFailCount;
479 extern int BuildSkipCount;
480 extern int BuildIgnoreCount;
481 extern int BuildSuccessCount;
482 extern int DynamicMaxWorkers;
484 extern buildenv_t *BuildEnv;
485 extern int WorkerProcFlags;
488 extern int SlowStartOpt;
490 extern int NullStdinOpt;
491 extern int UseCCache;
492 extern int UseUsrSrc;
496 extern long PkgDepMemoryTarget;
498 extern int MaxWorkers;
500 extern int UseTmpfsWork;
501 extern int UseTmpfsBase;
502 extern int UseNCurses;
503 extern int LeveragePrebuilt;
504 extern char *DSynthExecPath;
507 extern const char *OperatingSystemName;
508 extern const char *ArchitectureName;
509 extern const char *MachineName;
510 extern const char *ReleaseName;
511 extern const char *VersionName;
512 extern const char *VersionOnlyName;
513 extern const char *VersionFromParamHeader;
515 extern const char *ConfigBase1;
516 extern const char *ConfigBase2;
517 extern const char *ConfigBase;
518 extern const char *DPortsPath;
519 extern const char *CCachePath;
520 extern const char *SynthConfig;
521 extern const char *PackagesPath;
522 extern const char *RepositoryPath;
523 extern const char *OptionsPath;
524 extern const char *DistFilesPath;
525 extern const char *BuildBase;
526 extern const char *LogsPath;
527 extern const char *SystemPath;
528 extern const char *UsePkgSufx;
529 extern const char *Profile;
530 extern char *StatsBase;
531 extern char *StatsFilePath;
532 extern char *StatsLockPath;
534 extern int UsingHooks;
535 extern const char *HookRunStart;
536 extern const char *HookRunEnd;
537 extern const char *HookPkgSuccess;
538 extern const char *HookPkgFailure;
539 extern const char *HookPkgIgnored;
540 extern const char *HookPkgSkipped;
542 void _dfatal(const char *file, int line, const char *func, int do_errno,
543 const char *fmt, ...);
544 void _ddprintf(int tab, const char *fmt, ...);
545 void _dlog(int which, const char *fmt, ...);
546 char *strdup_or_null(char *str);
547 void dlogreset(void);
549 void addbuildenv(const char *label, const char *data, int type);
550 void delbuildenv(const char *label);
551 int readlogline(monitorlog_t *log, char **bufp);
553 void initbulk(void (*func)(bulk_t *bulk), int jobs);
554 void queuebulk(const char *s1, const char *s2, const char *s3,
556 bulk_t *getbulk(void);
558 void freebulk(bulk_t *bulk);
559 void freestrp(char **strp);
560 void dupstrp(char **strp);
561 int askyn(const char *ctl, ...);
562 double getswappct(int *noswapp);
563 FILE *dexec_open(const char **cav, int cac, pid_t *pidp, buildenv_t *xenv,
564 int with_env, int with_mvars);
565 int dexec_close(FILE *fp, pid_t pid);
566 const char *getphasestr(worker_phase_t phase);
568 void ParseConfiguration(int isworker);
569 pkg_t *ParsePackageList(int ac, char **av);
570 void FreePackageList(pkg_t *pkgs);
571 pkg_t *GetLocalPackageList(void);
572 pkg_t *GetFullPackageList(void);
573 pkg_t *GetPkgPkg(pkg_t *list);
575 void DoConfigure(void);
576 void DoStatus(pkg_t *pkgs);
577 void DoBuild(pkg_t *pkgs);
578 void DoInitBuild(int slot_override);
579 void DoCleanBuild(int resetlogs);
580 void OptimizeEnv(void);
581 void WorkerProcess(int ac, char **av);
583 int DoCreateTemplate(int force);
584 void DoDestroyTemplate(void);
585 void DoWorkerMounts(worker_t *work);
586 void DoWorkerUnmounts(worker_t *work);
587 void DoRebuildRepo(int ask);
588 void DoUpgradePkgs(pkg_t *pkgs, int ask);
589 void RemovePackages(pkg_t *pkgs);
590 void PurgeDistfiles(pkg_t *pkgs);
592 void RunStatsInit(void);
593 void RunStatsDone(void);
594 void RunStatsReset(void);
595 void RunStatsUpdate(worker_t *work, const char *portdir);
596 void RunStatsUpdateTop(void);
597 void RunStatsUpdateLogs(void);
598 void RunStatsSync(void);
600 int ipcreadmsg(int fd, wmsg_t *msg);
601 int ipcwritemsg(int fd, wmsg_t *msg);
602 extern void MonitorDirective(const char *datfile, const char *lkfile);