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 UMOUNT_BINARY "/sbin/umount"
80 typedef struct pkglink {
87 * Describes a [flavored] package
90 struct pkg *build_next; /* topology inversion build list */
91 struct pkg *bnext; /* linked list from bulk return */
92 struct pkg *hnext1; /* hash based on portdir */
93 struct pkg *hnext2; /* hash based on pkgfile */
94 pkglink_t idepon_list; /* I need these pkgs */
95 pkglink_t deponi_list; /* pkgs which depend on me */
96 char *portdir; /* origin name e.g. www/chromium[@flavor] */
97 char *logfile; /* relative logfile path */
98 char *version; /* PKGVERSION - e.g. 3.5.0_1 */
99 char *pkgfile; /* PKGFILE - e.g. flav-blah-3.5.0_1.txz */
100 char *ignore; /* IGNORE */
101 char *fetch_deps; /* FETCH_DEPENDS */
102 char *ext_deps; /* EXTRACT_DEPENDS */
103 char *patch_deps; /* PATCH_DEPENDS */
104 char *build_deps; /* BUILD_DEPENDS */
105 char *lib_deps; /* LIB_DEPENDS */
106 char *run_deps; /* RUN_DEPENDS */
107 char *pos_options; /* SELECTED_OPTIONS */
108 char *neg_options; /* DESELECTED_OPTIONS */
109 char *flavors; /* FLAVORS - e.g. py36 py27 */
110 int make_jobs_number; /* MAKE_JOBS_NUMBER */
111 int use_linux; /* USE_LINUX */
112 int idep_count; /* count recursive idepon build deps */
113 int depi_count; /* count recursive deponi build deps */
114 int dsynth_install_flg; /* locked with WorkerMutex */
118 #define PKGF_PACKAGED 0x00000001 /* has a repo package */
119 #define PKGF_DUMMY 0x00000002 /* generic root for flavors */
120 #define PKGF_NOTFOUND 0x00000004 /* dport not found */
121 #define PKGF_CORRUPT 0x00000008 /* dport corrupt */
122 #define PKGF_PLACEHOLD 0x00000010 /* pre-entered */
123 #define PKGF_BUILDLIST 0x00000020 /* on build_list */
124 #define PKGF_BUILDLOOP 0x00000040 /* traversal loop test */
125 #define PKGF_BUILDTRAV 0x00000080 /* traversal optimization */
126 #define PKGF_NOBUILD_D 0x00000100 /* can't build - dependency problem */
127 #define PKGF_NOBUILD_S 0x00000200 /* can't build - skipped */
128 #define PKGF_NOBUILD_F 0x00000400 /* can't build - failed */
129 #define PKGF_SUCCESS 0x00000800 /* build complete */
130 #define PKGF_FAILURE 0x00001000 /* build complete */
131 #define PKGF_RUNNING 0x00002000 /* build complete */
132 #define PKGF_PKGPKG 0x00004000 /* pkg/pkg-static special */
133 #define PKGF_NOTREADY 0x00008000 /* build_find_leaves() only */
134 #define PKGF_ERROR (PKGF_PLACEHOLD | PKGF_CORRUPT | PKGF_NOTFOUND | \
136 #define PKGF_NOBUILD (PKGF_NOBUILD_D | PKGF_NOBUILD_S | PKGF_NOBUILD_F)
138 #define PKGLIST_EMPTY(pkglink) ((pkglink)->next == (pkglink))
139 #define PKGLIST_FOREACH(var, head) \
140 for (var = (head)->next; var != (head); var = (var)->next)
142 typedef struct bulk {
147 enum { UNLISTED, ONSUBMIT, ONRUN, ISRUNNING, ONRESPONSE } state;
156 pkg_t *list; /* pkgs linked by bnext */
160 * Worker state (up to MAXWORKERS). Each worker operates within a
161 * chroot or jail. A system mirror is setup and the template
166 * /sbin - nullfs (ro)
168 * /libexec - nullfs (ro)
169 * /usr/bin - nullfs (ro)
170 * /usr/include - nullfs (ro)
171 * /usr/lib - nullfs (ro)
172 * /usr/libdata - nullfs (ro)
173 * /usr/libexec - nullfs (ro)
174 * /usr/sbin - nullfs (ro)
175 * /usr/share - nullfs (ro)
176 * /xports - nullfs (ro)
177 * /options - nullfs (ro)
178 * /packages - nullfs (ro)
179 * /distfiles - nullfs (ro)
180 * construction - tmpfs
182 * /boot - nullfs (ro)
183 * /boot/modules.local - tmpfs
184 * /usr/games - nullfs (ro)
185 * /usr/src - nullfs (ro)
188 enum worker_state { WORKER_NONE, WORKER_IDLE, WORKER_PENDING,
189 WORKER_RUNNING, WORKER_DONE, WORKER_FAILED,
191 typedef enum worker_state worker_state_t;
193 enum worker_phase { PHASE_PENDING,
200 PHASE_EXTRACT_DEPENDS,
218 typedef enum worker_phase worker_phase_t;
221 * Watchdog timeouts, in minutes, baseline, scales up with load/ncpus but
222 * does not scale down.
229 #define WDOG6 (60 + 30)
230 #define WDOG7 (60 * 2)
231 #define WDOG8 (60 * 2 + 30)
232 #define WDOG9 (60 * 3)
234 typedef struct worker {
235 int index; /* worker number 0..N-1 */
237 int accum_error; /* cumulative error */
238 int mount_error; /* mount and unmount error */
239 int terminate : 1; /* request sub-thread to terminate */
240 char *basedir; /* base directory including id */
242 pthread_t td; /* pthread */
243 pthread_cond_t cond; /* interlock cond (w/ WorkerMutex) */
245 worker_state_t state; /* general worker state */
246 worker_phase_t phase; /* phase control in childBuilderThread */
250 int fds[2]; /* forked environment process */
254 #define WORKERF_STATUS_UPDATE 0x0001 /* display update */
255 #define WORKERF_SUCCESS 0x0002 /* completion flag */
256 #define WORKERF_FAILURE 0x0004 /* completion flag */
258 #define MOUNT_TYPE_MASK 0x000F
259 #define MOUNT_TYPE_TMPFS 0x0001
260 #define MOUNT_TYPE_NULLFS 0x0002
261 #define MOUNT_TYPE_DEVFS 0x0003
262 #define MOUNT_TYPE_RW 0x0010
263 #define MOUNT_TYPE_BIG 0x0020
264 #define MOUNT_TYPE_TMP 0x0040
266 #define NULLFS_RO (MOUNT_TYPE_NULLFS)
267 #define NULLFS_RW (MOUNT_TYPE_NULLFS | MOUNT_TYPE_RW)
268 #define TMPFS_RW (MOUNT_TYPE_TMPFS | MOUNT_TYPE_RW)
269 #define TMPFS_RW_BIG (MOUNT_TYPE_TMPFS | MOUNT_TYPE_RW | \
271 #define DEVFS_RW (MOUNT_TYPE_DEVFS | MOUNT_TYPE_RW)
274 * IPC messages between the worker support thread and the worker process.
276 typedef struct wmsg {
280 worker_phase_t phase;
283 #define WMSG_CMD_STATUS_UPDATE 0x0001
284 #define WMSG_CMD_SUCCESS 0x0002
285 #define WMSG_CMD_FAILURE 0x0003
286 #define WMSG_CMD_INSTALL_PKGS 0x0004
287 #define WMSG_RES_INSTALL_PKGS 0x0005
290 * Make variables and build environment
292 typedef struct buildenv {
293 struct buildenv *next;
299 * Operating systems recognized by dsynth
302 OS_UNKNOWN, OS_DRAGONFLY, OS_FREEBSD, OS_NETBSD, OS_LINUX
305 typedef enum os_id os_id_t;
310 #define DLOG_ALL 0 /* Usually stdout when curses disabled */
311 #define DLOG_SUCC 1 /* success_list.log */
312 #define DLOG_FAIL 2 /* failure_list.log */
313 #define DLOG_IGN 3 /* ignored_list.log */
314 #define DLOG_SKIP 4 /* skipped_list.log */
315 #define DLOG_ABN 5 /* abnormal_command_output */
316 #define DLOG_OBS 6 /* obsolete_packages.log */
317 #define DLOG_COUNT 7 /* total number of DLOGs */
319 #define dassert(exp, fmt, ...) \
320 if (!(exp)) dpanic(fmt, ## __VA_ARGS__)
322 #define ddassert(exp) \
323 dassert((exp), "\"%s\" line %d", __FILE__, __LINE__)
325 #define dassert_errno(exp, fmt, ...) \
326 if (!(cond)) dpanic_errno(fmt, ## __VA_ARGS__)
328 #define dlog(which, fmt, ...) \
329 _dlog(which, fmt, ## __VA_ARGS__)
331 #define dfatal(fmt, ...) \
332 _dfatal(__FILE__, __LINE__, __func__, 0, fmt, ## __VA_ARGS__)
334 #define dpanic(fmt, ...) \
335 _dfatal(__FILE__, __LINE__, __func__, 2, fmt, ## __VA_ARGS__)
337 #define dfatal_errno(fmt, ...) \
338 _dfatal(__FILE__, __LINE__, __func__, 1, fmt, ## __VA_ARGS__)
340 #define dpanic_errno(fmt, ...) \
341 _dfatal(__FILE__, __LINE__, __func__, 3, fmt, ## __VA_ARGS__)
343 #define ddprintf(tab, fmt, ...) \
344 do { if (DebugOpt) _ddprintf(tab, fmt, ## __VA_ARGS__); } while(0)
346 #define DOSTRING(label) #label
347 #define SCRIPTPATH(x) DOSTRING(x)
349 extern int BuildCount;
350 extern int BuildTotal;
351 extern int BuildFailCount;
352 extern int BuildSkipCount;
353 extern int BuildSuccessCount;
355 extern buildenv_t *BuildEnv;
357 extern int UseCCache;
361 extern int MaxWorkers;
363 extern int UseTmpfsWork;
364 extern int UseTmpfsBase;
365 extern int UseNCurses;
366 extern int LeveragePrebuilt;
367 extern char *DSynthExecPath;
369 extern const char *OperatingSystemName;
370 extern const char *ArchitectureName;
371 extern const char *MachineName;
372 extern const char *ReleaseName;
373 extern const char *VersionName;
375 extern const char *DPortsPath;
376 extern const char *CCachePath;
377 extern const char *SynthConfig;
378 extern const char *PackagesPath;
379 extern const char *RepositoryPath;
380 extern const char *OptionsPath;
381 extern const char *DistFilesPath;
382 extern const char *BuildBase;
383 extern const char *LogsPath;
384 extern const char *SystemPath;
386 void _dfatal(const char *file, int line, const char *func, int do_errno,
387 const char *fmt, ...);
388 void _ddprintf(int tab, const char *fmt, ...);
389 void _dlog(int which, const char *fmt, ...);
390 void dlogreset(void);
391 void addbuildenv(const char *label, const char *data);
393 void initbulk(void (*func)(bulk_t *bulk), int jobs);
394 bulk_t *queuebulk(const char *s1, const char *s2, const char *s3,
396 bulk_t *getbulk(void);
398 void freebulk(bulk_t *bulk);
399 void freestrp(char **strp);
400 void dupstrp(char **strp);
402 void ParseConfiguration(int isworker);
403 pkg_t *ParsePackageList(int ac, char **av);
404 void FreePackageList(pkg_t *pkgs);
405 pkg_t *GetLocalPackageList(void);
406 pkg_t *GetFullPackageList(void);
407 pkg_t *GetPkgPkg(pkg_t *list);
409 void DoConfigure(void);
410 void DoBuild(pkg_t *pkgs);
411 void DoInitBuild(int slot_override);
412 void DoCleanBuild(void);
413 void WorkerProcess(int ac, char **av);
415 int DoCreateTemplate(void);
416 void DoDestroyTemplate(void);
417 void DoWorkerMounts(worker_t *work);
418 void DoWorkerUnmounts(worker_t *work);
419 void DoRebuildRepo(int ask);
420 void DoUpgradePkgs(pkg_t *pkgs, int ask);
421 void RemovePackages(pkg_t *pkgs);
422 void PurgeDistfiles(pkg_t *pkgs);
426 void GuiUpdate(worker_t *work);
427 void GuiUpdateTop(void);
430 int ipcreadmsg(int fd, wmsg_t *msg);
431 int ipcwritemsg(int fd, wmsg_t *msg);