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>
64 #define DSYNTH_VERSION "1.01"
65 #define MAXWORKERS 1024
66 #define MAXJOBS 8192 /* just used for -j sanity */
67 #define MAXBULK MAXWORKERS
69 #define MAKE_BINARY "/usr/bin/make"
70 #define PKG_BINARY "/usr/local/sbin/pkg"
71 #define MOUNT_BINARY "/sbin/mount"
72 #define MOUNT_NULLFS_BINARY "/sbin/mount_null"
73 #define MOUNT_TMPFS_BINARY "/sbin/mount_tmpfs"
74 #define MOUNT_DEVFS_BINARY "/sbin/mount_devfs"
75 #define MOUNT_PROCFS_BINARY "/sbin/mount_procfs"
76 #define UMOUNT_BINARY "/sbin/umount"
78 #define ONEGB (1024L * 1024 * 1024)
81 * This can be ".tar", ".tgz", ".txz", or ".tbz".
83 * .tar - very fast but you'll need 1TB+ of storage just for the package files.
84 * .txz - very compact but decompression speed is horrible.
85 * .tgz - reasonable compression, extremely fast decompression. Roughly
86 * 1.1x to 2.0x the size of a .txz, but decompresses 10x faster.
87 * .tbz - worse than .tgz generally
89 #define USE_PKG_SUFX ".tgz"
94 typedef struct pkglink {
101 #define DEP_TYPE_FETCH 1
102 #define DEP_TYPE_EXT 2
103 #define DEP_TYPE_PATCH 3
104 #define DEP_TYPE_BUILD 4
105 #define DEP_TYPE_LIB 5
106 #define DEP_TYPE_RUN 6
109 * Describes a [flavored] package
112 struct pkg *build_next; /* topology inversion build list */
113 struct pkg *bnext; /* linked list from bulk return */
114 struct pkg *hnext1; /* hash based on portdir */
115 struct pkg *hnext2; /* hash based on pkgfile */
116 pkglink_t idepon_list; /* I need these pkgs */
117 pkglink_t deponi_list; /* pkgs which depend on me */
118 char *portdir; /* origin name e.g. www/chromium[@flavor] */
119 char *logfile; /* relative logfile path */
120 char *version; /* PKGVERSION - e.g. 3.5.0_1 */
121 char *pkgfile; /* PKGFILE - e.g. flav-blah-3.5.0_1.txz */
122 char *distfiles; /* DISTFILES - e.g. blah-68.0.source.tar.xz */
123 char *distsubdir; /* DIST_SUBDIR- e.g. cabal */
124 char *ignore; /* IGNORE (also covers BROKEN) */
125 char *fetch_deps; /* FETCH_DEPENDS */
126 char *ext_deps; /* EXTRACT_DEPENDS */
127 char *patch_deps; /* PATCH_DEPENDS */
128 char *build_deps; /* BUILD_DEPENDS */
129 char *lib_deps; /* LIB_DEPENDS */
130 char *run_deps; /* RUN_DEPENDS */
131 char *pos_options; /* SELECTED_OPTIONS */
132 char *neg_options; /* DESELECTED_OPTIONS */
133 char *flavors; /* FLAVORS - e.g. py36 py27 */
134 int make_jobs_number; /* MAKE_JOBS_NUMBER */
135 int use_linux; /* USE_LINUX */
136 int idep_count; /* count recursive idepon build deps */
137 int depi_count; /* count recursive deponi build deps */
138 int depi_depth; /* tree depth who depends on me */
139 int dsynth_install_flg; /* locked with WorkerMutex */
141 size_t pkgfile_size; /* size of pkgfile */
144 #define PKGF_PACKAGED 0x00000001 /* has a repo package */
145 #define PKGF_DUMMY 0x00000002 /* generic root for flavors */
146 #define PKGF_NOTFOUND 0x00000004 /* dport not found */
147 #define PKGF_CORRUPT 0x00000008 /* dport corrupt */
148 #define PKGF_PLACEHOLD 0x00000010 /* pre-entered */
149 #define PKGF_BUILDLIST 0x00000020 /* on build_list */
150 #define PKGF_BUILDLOOP 0x00000040 /* traversal loop test */
151 #define PKGF_BUILDTRAV 0x00000080 /* traversal optimization */
152 #define PKGF_NOBUILD_D 0x00000100 /* can't build - dependency problem */
153 #define PKGF_NOBUILD_S 0x00000200 /* can't build - skipped */
154 #define PKGF_NOBUILD_F 0x00000400 /* can't build - failed */
155 #define PKGF_NOBUILD_I 0x00000800 /* can't build - ignored or broken */
156 #define PKGF_SUCCESS 0x00001000 /* build complete */
157 #define PKGF_FAILURE 0x00002000 /* build complete */
158 #define PKGF_RUNNING 0x00004000 /* build complete */
159 #define PKGF_PKGPKG 0x00008000 /* pkg/pkg-static special */
160 #define PKGF_NOTREADY 0x00010000 /* build_find_leaves() only */
161 #define PKGF_ERROR (PKGF_PLACEHOLD | PKGF_CORRUPT | PKGF_NOTFOUND | \
163 #define PKGF_NOBUILD (PKGF_NOBUILD_D | PKGF_NOBUILD_S | PKGF_NOBUILD_F | \
166 #define PKGLIST_EMPTY(pkglink) ((pkglink)->next == (pkglink))
167 #define PKGLIST_FOREACH(var, head) \
168 for (var = (head)->next; var != (head); var = (var)->next)
170 typedef struct bulk {
175 enum { UNLISTED, ONSUBMIT, ONRUN, ISRUNNING, ONRESPONSE } state;
184 pkg_t *list; /* pkgs linked by bnext */
188 * Worker state (up to MAXWORKERS). Each worker operates within a
189 * chroot or jail. A system mirror is setup and the template
194 * /sbin - nullfs (ro)
196 * /libexec - nullfs (ro)
197 * /usr/bin - nullfs (ro)
198 * /usr/include - nullfs (ro)
199 * /usr/lib - nullfs (ro)
200 * /usr/libdata - nullfs (ro)
201 * /usr/libexec - nullfs (ro)
202 * /usr/sbin - nullfs (ro)
203 * /usr/share - nullfs (ro)
204 * /xports - nullfs (ro)
205 * /options - nullfs (ro)
206 * /packages - nullfs (ro)
207 * /distfiles - nullfs (ro)
208 * construction - tmpfs
210 * /boot - nullfs (ro)
211 * /boot/modules.local - tmpfs
212 * /usr/games - nullfs (ro)
213 * /usr/src - nullfs (ro)
216 enum worker_state { WORKER_NONE, WORKER_IDLE, WORKER_PENDING,
217 WORKER_RUNNING, WORKER_DONE, WORKER_FAILED,
218 WORKER_FROZEN, WORKER_EXITING };
219 typedef enum worker_state worker_state_t;
221 enum worker_phase { PHASE_PENDING,
228 PHASE_EXTRACT_DEPENDS,
246 typedef enum worker_phase worker_phase_t;
249 * Watchdog timeouts, in minutes, baseline, scales up with load/ncpus but
250 * does not scale down.
257 #define WDOG6 (60 + 30)
258 #define WDOG7 (60 * 2)
259 #define WDOG8 (60 * 2 + 30)
260 #define WDOG9 (60 * 3)
262 typedef struct worker {
263 int index; /* worker number 0..N-1 */
265 int accum_error; /* cumulative error */
266 int mount_error; /* mount and unmount error */
267 int terminate : 1; /* request sub-thread to terminate */
268 char *basedir; /* base directory including id */
270 pthread_t td; /* pthread */
271 pthread_cond_t cond; /* interlock cond (w/ WorkerMutex) */
273 worker_state_t state; /* general worker state */
274 worker_phase_t phase; /* phase control in childBuilderThread */
278 int fds[2]; /* forked environment process */
280 size_t pkg_dep_size; /* pkg dependency size(s) */
283 #define WORKERF_STATUS_UPDATE 0x0001 /* display update */
284 #define WORKERF_SUCCESS 0x0002 /* completion flag */
285 #define WORKERF_FAILURE 0x0004 /* completion flag */
286 #define WORKERF_FREEZE 0x0008 /* freeze the worker */
288 #define MOUNT_TYPE_MASK 0x000F
289 #define MOUNT_TYPE_TMPFS 0x0001
290 #define MOUNT_TYPE_NULLFS 0x0002
291 #define MOUNT_TYPE_DEVFS 0x0003
292 #define MOUNT_TYPE_PROCFS 0x0004
293 #define MOUNT_TYPE_RW 0x0010
294 #define MOUNT_TYPE_BIG 0x0020
295 #define MOUNT_TYPE_TMP 0x0040
297 #define NULLFS_RO (MOUNT_TYPE_NULLFS)
298 #define NULLFS_RW (MOUNT_TYPE_NULLFS | MOUNT_TYPE_RW)
299 #define PROCFS_RO (MOUNT_TYPE_PROCFS)
300 #define TMPFS_RW (MOUNT_TYPE_TMPFS | MOUNT_TYPE_RW)
301 #define TMPFS_RW_BIG (MOUNT_TYPE_TMPFS | MOUNT_TYPE_RW | \
303 #define DEVFS_RW (MOUNT_TYPE_DEVFS | MOUNT_TYPE_RW)
306 * IPC messages between the worker support thread and the worker process.
308 typedef struct wmsg {
312 worker_phase_t phase;
315 #define WMSG_CMD_STATUS_UPDATE 0x0001
316 #define WMSG_CMD_SUCCESS 0x0002
317 #define WMSG_CMD_FAILURE 0x0003
318 #define WMSG_CMD_INSTALL_PKGS 0x0004
319 #define WMSG_RES_INSTALL_PKGS 0x0005
320 #define WMSG_CMD_FREEZEWORKER 0x0006
323 * Make variables and build environment
325 typedef struct buildenv {
326 struct buildenv *next;
333 * Operating systems recognized by dsynth
336 OS_UNKNOWN, OS_DRAGONFLY, OS_FREEBSD, OS_NETBSD, OS_LINUX
339 typedef enum os_id os_id_t;
344 #define DLOG_ALL 0 /* Usually stdout when curses disabled */
345 #define DLOG_SUCC 1 /* success_list.log */
346 #define DLOG_FAIL 2 /* failure_list.log */
347 #define DLOG_IGN 3 /* ignored_list.log */
348 #define DLOG_SKIP 4 /* skipped_list.log */
349 #define DLOG_ABN 5 /* abnormal_command_output */
350 #define DLOG_OBS 6 /* obsolete_packages.log */
351 #define DLOG_COUNT 7 /* total number of DLOGs */
353 #define dassert(exp, fmt, ...) \
354 if (!(exp)) dpanic(fmt, ## __VA_ARGS__)
356 #define ddassert(exp) \
357 dassert((exp), "\"%s\" line %d", __FILE__, __LINE__)
359 #define dassert_errno(exp, fmt, ...) \
360 if (!(exp)) dpanic_errno(fmt, ## __VA_ARGS__)
362 #define dlog(which, fmt, ...) \
363 _dlog(which, fmt, ## __VA_ARGS__)
365 #define dfatal(fmt, ...) \
366 _dfatal(__FILE__, __LINE__, __func__, 0, fmt, ## __VA_ARGS__)
368 #define dpanic(fmt, ...) \
369 _dfatal(__FILE__, __LINE__, __func__, 2, fmt, ## __VA_ARGS__)
371 #define dfatal_errno(fmt, ...) \
372 _dfatal(__FILE__, __LINE__, __func__, 1, fmt, ## __VA_ARGS__)
374 #define dpanic_errno(fmt, ...) \
375 _dfatal(__FILE__, __LINE__, __func__, 3, fmt, ## __VA_ARGS__)
377 #define ddprintf(tab, fmt, ...) \
378 do { if (DebugOpt >= 2) _ddprintf(tab, fmt, ## __VA_ARGS__); } while(0)
381 * addbuildenv() types
383 #define BENV_ENVIRONMENT 1
384 #define BENV_MAKECONF 2
387 * WORKER process flags
389 #define WORKER_PROC_DEBUGSTOP 0x0001
390 #define WORKER_PROC_DEVELOPER 0x0002
395 #define DOSTRING(label) #label
396 #define SCRIPTPATH(x) DOSTRING(x)
399 extern int BuildCount;
400 extern int BuildTotal;
401 extern int BuildFailCount;
402 extern int BuildSkipCount;
403 extern int BuildIgnoreCount;
404 extern int BuildSuccessCount;
405 extern int DynamicMaxWorkers;
407 extern buildenv_t *BuildEnv;
408 extern int WorkerProcFlags;
410 extern int SlowStartOpt;
412 extern int NullStdinOpt;
413 extern int UseCCache;
417 extern long PkgDepMemoryTarget;
419 extern int MaxWorkers;
421 extern int UseTmpfsWork;
422 extern int UseTmpfsBase;
423 extern int UseNCurses;
424 extern int LeveragePrebuilt;
425 extern char *DSynthExecPath;
427 extern const char *OperatingSystemName;
428 extern const char *ArchitectureName;
429 extern const char *MachineName;
430 extern const char *ReleaseName;
431 extern const char *VersionName;
433 extern const char *ConfigBase;
434 extern const char *AltConfigBase;
435 extern const char *DPortsPath;
436 extern const char *CCachePath;
437 extern const char *SynthConfig;
438 extern const char *PackagesPath;
439 extern const char *RepositoryPath;
440 extern const char *OptionsPath;
441 extern const char *DistFilesPath;
442 extern const char *BuildBase;
443 extern const char *LogsPath;
444 extern const char *SystemPath;
446 void _dfatal(const char *file, int line, const char *func, int do_errno,
447 const char *fmt, ...);
448 void _ddprintf(int tab, const char *fmt, ...);
449 void _dlog(int which, const char *fmt, ...);
450 char *strdup_or_null(char *str);
451 void dlogreset(void);
453 void addbuildenv(const char *label, const char *data, int type);
455 void initbulk(void (*func)(bulk_t *bulk), int jobs);
456 void queuebulk(const char *s1, const char *s2, const char *s3,
458 bulk_t *getbulk(void);
460 void freebulk(bulk_t *bulk);
461 void freestrp(char **strp);
462 void dupstrp(char **strp);
463 int askyn(const char *ctl, ...);
464 double getswappct(int *noswapp);
465 FILE *dexec_open(const char **cav, int cac, pid_t *pidp,
466 int with_env, int with_mvars);
467 int dexec_close(FILE *fp, pid_t pid);
469 void ParseConfiguration(int isworker);
470 pkg_t *ParsePackageList(int ac, char **av);
471 void FreePackageList(pkg_t *pkgs);
472 pkg_t *GetLocalPackageList(void);
473 pkg_t *GetFullPackageList(void);
474 pkg_t *GetPkgPkg(pkg_t *list);
476 void DoConfigure(void);
477 void DoStatus(pkg_t *pkgs);
478 void DoBuild(pkg_t *pkgs);
479 void DoInitBuild(int slot_override);
480 void DoCleanBuild(int resetlogs);
481 void WorkerProcess(int ac, char **av);
483 int DoCreateTemplate(int force);
484 void DoDestroyTemplate(void);
485 void DoWorkerMounts(worker_t *work);
486 void DoWorkerUnmounts(worker_t *work);
487 void DoRebuildRepo(int ask);
488 void DoUpgradePkgs(pkg_t *pkgs, int ask);
489 void RemovePackages(pkg_t *pkgs);
490 void PurgeDistfiles(pkg_t *pkgs);
495 void GuiUpdate(worker_t *work);
496 void GuiUpdateTop(void);
497 void GuiUpdateLogs(void);
500 int ipcreadmsg(int fd, wmsg_t *msg);
501 int ipcwritemsg(int fd, wmsg_t *msg);