xref: /dflybsd-src/usr.bin/dsynth/dsynth.h (revision 516819d9579c6a23e9457b608e1d19f64096d248)
18e25f19bSMatthew Dillon /*
28e25f19bSMatthew Dillon  * Copyright (c) 2019 The DragonFly Project.  All rights reserved.
38e25f19bSMatthew Dillon  *
48e25f19bSMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
58e25f19bSMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
68e25f19bSMatthew Dillon  *
78e25f19bSMatthew Dillon  * This code uses concepts and configuration based on 'synth', by
88e25f19bSMatthew Dillon  * John R. Marino <draco@marino.st>, which was written in ada.
98e25f19bSMatthew Dillon  *
108e25f19bSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
118e25f19bSMatthew Dillon  * modification, are permitted provided that the following conditions
128e25f19bSMatthew Dillon  * are met:
138e25f19bSMatthew Dillon  *
148e25f19bSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
158e25f19bSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
168e25f19bSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
178e25f19bSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
188e25f19bSMatthew Dillon  *    the documentation and/or other materials provided with the
198e25f19bSMatthew Dillon  *    distribution.
208e25f19bSMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
218e25f19bSMatthew Dillon  *    contributors may be used to endorse or promote products derived
228e25f19bSMatthew Dillon  *    from this software without specific, prior written permission.
238e25f19bSMatthew Dillon  *
248e25f19bSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
258e25f19bSMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
268e25f19bSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
278e25f19bSMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
288e25f19bSMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
298e25f19bSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
308e25f19bSMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
318e25f19bSMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
328e25f19bSMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
338e25f19bSMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
348e25f19bSMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
358e25f19bSMatthew Dillon  * SUCH DAMAGE.
368e25f19bSMatthew Dillon  */
378e25f19bSMatthew Dillon 
388e25f19bSMatthew Dillon #include <sys/types.h>
398e25f19bSMatthew Dillon #include <sys/wait.h>
408e25f19bSMatthew Dillon #include <sys/stat.h>
418e25f19bSMatthew Dillon #include <sys/sysctl.h>
428e25f19bSMatthew Dillon #include <sys/socket.h>
438e25f19bSMatthew Dillon #include <sys/mount.h>
448e25f19bSMatthew Dillon #include <sys/procctl.h>
458e25f19bSMatthew Dillon #include <stdio.h>
468e25f19bSMatthew Dillon #include <stdlib.h>
478e25f19bSMatthew Dillon #include <stddef.h>
488e25f19bSMatthew Dillon #include <stdarg.h>
498e25f19bSMatthew Dillon #include <unistd.h>
508e25f19bSMatthew Dillon #include <string.h>
518e25f19bSMatthew Dillon #include <fcntl.h>
528e25f19bSMatthew Dillon #include <signal.h>
538e25f19bSMatthew Dillon #include <poll.h>
548e25f19bSMatthew Dillon #include <assert.h>
558e25f19bSMatthew Dillon #include <errno.h>
568e25f19bSMatthew Dillon #include <pthread.h>
578e25f19bSMatthew Dillon #include <dirent.h>
588e25f19bSMatthew Dillon #include <termios.h>
598e25f19bSMatthew Dillon #include <ctype.h>
608e25f19bSMatthew Dillon #include <libutil.h>
618e25f19bSMatthew Dillon 
62a574cbf0SMatthew Dillon #include <elf.h>
63a574cbf0SMatthew Dillon 
648e25f19bSMatthew Dillon struct pkglink;
658e25f19bSMatthew Dillon 
668e25f19bSMatthew Dillon #define DSYNTH_VERSION	"1.01"
678e25f19bSMatthew Dillon #define MAXWORKERS	1024
688e25f19bSMatthew Dillon #define MAXJOBS		8192	/* just used for -j sanity */
698e25f19bSMatthew Dillon #define MAXBULK		MAXWORKERS
708e25f19bSMatthew Dillon 
718e25f19bSMatthew Dillon #define MAKE_BINARY		"/usr/bin/make"
728e25f19bSMatthew Dillon #define PKG_BINARY		"/usr/local/sbin/pkg"
738e25f19bSMatthew Dillon #define MOUNT_BINARY		"/sbin/mount"
748e25f19bSMatthew Dillon #define MOUNT_NULLFS_BINARY	"/sbin/mount_null"
758e25f19bSMatthew Dillon #define MOUNT_TMPFS_BINARY	"/sbin/mount_tmpfs"
768e25f19bSMatthew Dillon #define MOUNT_DEVFS_BINARY	"/sbin/mount_devfs"
778ec23ca1SMatthew Dillon #define MOUNT_PROCFS_BINARY	"/sbin/mount_procfs"
788e25f19bSMatthew Dillon #define UMOUNT_BINARY		"/sbin/umount"
798e25f19bSMatthew Dillon 
807f0eca56SMatthew Dillon #define ONEGB		(1024L * 1024 * 1024)
817f0eca56SMatthew Dillon 
828e25f19bSMatthew Dillon /*
83667fb2cbSMatthew Dillon  * This can be ".tar", ".tgz", ".txz", or ".tbz".
84667fb2cbSMatthew Dillon  *
85667fb2cbSMatthew Dillon  * .tar	- very fast but you'll need 1TB+ of storage just for the package files.
86667fb2cbSMatthew Dillon  * .txz - very compact but decompression speed is horrible.
87667fb2cbSMatthew Dillon  * .tgz - reasonable compression, extremely fast decompression.  Roughly
88667fb2cbSMatthew Dillon  *	  1.1x to 2.0x the size of a .txz, but decompresses 10x faster.
89667fb2cbSMatthew Dillon  * .tbz - worse than .tgz generally
90667fb2cbSMatthew Dillon  */
91667fb2cbSMatthew Dillon #define USE_PKG_SUFX		".tgz"
92667fb2cbSMatthew Dillon 
93667fb2cbSMatthew Dillon /*
948e25f19bSMatthew Dillon  * Topology linkages
958e25f19bSMatthew Dillon  */
968e25f19bSMatthew Dillon typedef struct pkglink {
978e25f19bSMatthew Dillon 	struct pkglink *next;
988e25f19bSMatthew Dillon 	struct pkglink *prev;
998e25f19bSMatthew Dillon 	struct pkg *pkg;
10063fcce5bSMatthew Dillon 	int	dep_type;
1018e25f19bSMatthew Dillon } pkglink_t;
1028e25f19bSMatthew Dillon 
10363fcce5bSMatthew Dillon #define DEP_TYPE_FETCH	1
10463fcce5bSMatthew Dillon #define DEP_TYPE_EXT	2
10563fcce5bSMatthew Dillon #define DEP_TYPE_PATCH	3
10663fcce5bSMatthew Dillon #define DEP_TYPE_BUILD	4
10763fcce5bSMatthew Dillon #define DEP_TYPE_LIB	5
10863fcce5bSMatthew Dillon #define DEP_TYPE_RUN	6
10963fcce5bSMatthew Dillon 
1108e25f19bSMatthew Dillon /*
1118e25f19bSMatthew Dillon  * Describes a [flavored] package
1128e25f19bSMatthew Dillon  */
1138e25f19bSMatthew Dillon typedef struct pkg {
1148e25f19bSMatthew Dillon 	struct pkg *build_next;	/* topology inversion build list */
1158e25f19bSMatthew Dillon 	struct pkg *bnext;	/* linked list from bulk return */
1168e25f19bSMatthew Dillon 	struct pkg *hnext1;	/* hash based on portdir */
1178e25f19bSMatthew Dillon 	struct pkg *hnext2;	/* hash based on pkgfile */
1188e25f19bSMatthew Dillon 	pkglink_t idepon_list;	/* I need these pkgs */
1198e25f19bSMatthew Dillon 	pkglink_t deponi_list;	/* pkgs which depend on me */
1208e25f19bSMatthew Dillon 	char *portdir;		/* origin name e.g. www/chromium[@flavor] */
1218e25f19bSMatthew Dillon 	char *logfile;		/* relative logfile path */
1228e25f19bSMatthew Dillon 	char *version;		/* PKGVERSION - e.g. 3.5.0_1		*/
1238e25f19bSMatthew Dillon 	char *pkgfile;		/* PKGFILE    - e.g. flav-blah-3.5.0_1.txz */
1241645cafeSMatthew Dillon 	char *distfiles;	/* DISTFILES  - e.g. blah-68.0.source.tar.xz */
1251645cafeSMatthew Dillon 	char *distsubdir;	/* DIST_SUBDIR- e.g. cabal		*/
12687017ac4SMatthew Dillon 	char *ignore;		/* IGNORE (also covers BROKEN)		*/
1278e25f19bSMatthew Dillon 	char *fetch_deps;	/* FETCH_DEPENDS			*/
1288e25f19bSMatthew Dillon 	char *ext_deps;		/* EXTRACT_DEPENDS			*/
1298e25f19bSMatthew Dillon 	char *patch_deps;	/* PATCH_DEPENDS			*/
1308e25f19bSMatthew Dillon 	char *build_deps;	/* BUILD_DEPENDS			*/
1318e25f19bSMatthew Dillon 	char *lib_deps;		/* LIB_DEPENDS				*/
1328e25f19bSMatthew Dillon 	char *run_deps;		/* RUN_DEPENDS				*/
1338e25f19bSMatthew Dillon 	char *pos_options;	/* SELECTED_OPTIONS			*/
1348e25f19bSMatthew Dillon 	char *neg_options;	/* DESELECTED_OPTIONS			*/
1358e25f19bSMatthew Dillon 	char *flavors;		/* FLAVORS    - e.g. py36 py27		*/
1368e25f19bSMatthew Dillon 	int make_jobs_number;	/* MAKE_JOBS_NUMBER			*/
1378e25f19bSMatthew Dillon 	int use_linux;		/* USE_LINUX				*/
1388e25f19bSMatthew Dillon 	int idep_count;		/* count recursive idepon build deps	*/
1398e25f19bSMatthew Dillon 	int depi_count;		/* count recursive deponi build deps	*/
14088c24d72SMatthew Dillon 	int depi_depth;		/* tree depth who depends on me		*/
1418e25f19bSMatthew Dillon 	int dsynth_install_flg;	/* locked with WorkerMutex	*/
1428e25f19bSMatthew Dillon 	int flags;
143fef2fc63SMatthew Dillon 	size_t pkgfile_size;	/* size of pkgfile */
1448e25f19bSMatthew Dillon } pkg_t;
1458e25f19bSMatthew Dillon 
1468e25f19bSMatthew Dillon #define PKGF_PACKAGED	0x00000001	/* has a repo package */
1478e25f19bSMatthew Dillon #define PKGF_DUMMY	0x00000002	/* generic root for flavors */
1488e25f19bSMatthew Dillon #define PKGF_NOTFOUND	0x00000004	/* dport not found */
1498e25f19bSMatthew Dillon #define PKGF_CORRUPT	0x00000008	/* dport corrupt */
1508e25f19bSMatthew Dillon #define PKGF_PLACEHOLD	0x00000010	/* pre-entered */
1518e25f19bSMatthew Dillon #define PKGF_BUILDLIST	0x00000020	/* on build_list */
1528e25f19bSMatthew Dillon #define PKGF_BUILDLOOP	0x00000040	/* traversal loop test */
1538e25f19bSMatthew Dillon #define PKGF_BUILDTRAV	0x00000080	/* traversal optimization */
1548e25f19bSMatthew Dillon #define PKGF_NOBUILD_D	0x00000100	/* can't build - dependency problem */
1558e25f19bSMatthew Dillon #define PKGF_NOBUILD_S	0x00000200	/* can't build - skipped */
1568e25f19bSMatthew Dillon #define PKGF_NOBUILD_F	0x00000400	/* can't build - failed */
15787017ac4SMatthew Dillon #define PKGF_NOBUILD_I	0x00000800	/* can't build - ignored or broken */
15887017ac4SMatthew Dillon #define PKGF_SUCCESS	0x00001000	/* build complete */
15987017ac4SMatthew Dillon #define PKGF_FAILURE	0x00002000	/* build complete */
16087017ac4SMatthew Dillon #define PKGF_RUNNING	0x00004000	/* build complete */
16187017ac4SMatthew Dillon #define PKGF_PKGPKG	0x00008000	/* pkg/pkg-static special */
16287017ac4SMatthew Dillon #define PKGF_NOTREADY	0x00010000	/* build_find_leaves() only */
1638e25f19bSMatthew Dillon #define PKGF_ERROR	(PKGF_PLACEHOLD | PKGF_CORRUPT | PKGF_NOTFOUND | \
1648e25f19bSMatthew Dillon 			 PKGF_FAILURE)
16587017ac4SMatthew Dillon #define PKGF_NOBUILD	(PKGF_NOBUILD_D | PKGF_NOBUILD_S | PKGF_NOBUILD_F | \
16687017ac4SMatthew Dillon 			 PKGF_NOBUILD_I)
1678e25f19bSMatthew Dillon 
1688e25f19bSMatthew Dillon #define PKGLIST_EMPTY(pkglink)		((pkglink)->next == (pkglink))
1698e25f19bSMatthew Dillon #define PKGLIST_FOREACH(var, head)	\
1708e25f19bSMatthew Dillon 	for (var = (head)->next; var != (head); var = (var)->next)
1718e25f19bSMatthew Dillon 
1728e25f19bSMatthew Dillon typedef struct bulk {
1738e25f19bSMatthew Dillon 	struct bulk *next;
1748e25f19bSMatthew Dillon 	pthread_t td;
1758e25f19bSMatthew Dillon 	int debug;
1768e25f19bSMatthew Dillon 	int flags;
1778e25f19bSMatthew Dillon 	enum { UNLISTED, ONSUBMIT, ONRUN, ISRUNNING, ONRESPONSE } state;
1788e25f19bSMatthew Dillon 	char *s1;
1798e25f19bSMatthew Dillon 	char *s2;
1808e25f19bSMatthew Dillon 	char *s3;
1818e25f19bSMatthew Dillon 	char *s4;
1828e25f19bSMatthew Dillon 	char *r1;
1838e25f19bSMatthew Dillon 	char *r2;
1848e25f19bSMatthew Dillon 	char *r3;
1858e25f19bSMatthew Dillon 	char *r4;
1868e25f19bSMatthew Dillon 	pkg_t *list;		/* pkgs linked by bnext */
1878e25f19bSMatthew Dillon } bulk_t;
1888e25f19bSMatthew Dillon 
1898e25f19bSMatthew Dillon /*
1908e25f19bSMatthew Dillon  * Worker state (up to MAXWORKERS).  Each worker operates within a
1918e25f19bSMatthew Dillon  * chroot or jail.  A system mirror is setup and the template
1928e25f19bSMatthew Dillon  * is copied in.
1938e25f19bSMatthew Dillon  *
1948e25f19bSMatthew Dillon  * basedir		- tmpfs
1958e25f19bSMatthew Dillon  * /bin			- nullfs (ro)
1968e25f19bSMatthew Dillon  * /sbin		- nullfs (ro)
1978e25f19bSMatthew Dillon  * /lib			- nullfs (ro)
1988e25f19bSMatthew Dillon  * /libexec		- nullfs (ro)
1998e25f19bSMatthew Dillon  * /usr/bin		- nullfs (ro)
2008e25f19bSMatthew Dillon  * /usr/include		- nullfs (ro)
2018e25f19bSMatthew Dillon  * /usr/lib		- nullfs (ro)
2028e25f19bSMatthew Dillon  * /usr/libdata		- nullfs (ro)
2038e25f19bSMatthew Dillon  * /usr/libexec		- nullfs (ro)
2048e25f19bSMatthew Dillon  * /usr/sbin		- nullfs (ro)
2058e25f19bSMatthew Dillon  * /usr/share		- nullfs (ro)
2068e25f19bSMatthew Dillon  * /xports		- nullfs (ro)
2078e25f19bSMatthew Dillon  * /options		- nullfs (ro)
2088e25f19bSMatthew Dillon  * /packages		- nullfs (ro)
2098e25f19bSMatthew Dillon  * /distfiles		- nullfs (ro)
2108e25f19bSMatthew Dillon  * construction		- tmpfs
2118e25f19bSMatthew Dillon  * /usr/local		- tmpfs
2128e25f19bSMatthew Dillon  * /boot		- nullfs (ro)
2138e25f19bSMatthew Dillon  * /boot/modules.local	- tmpfs
2148e25f19bSMatthew Dillon  * /usr/games		- nullfs (ro)
2158e25f19bSMatthew Dillon  * /usr/src		- nullfs (ro)
2168e25f19bSMatthew Dillon  * /dev			- devfs
2178e25f19bSMatthew Dillon  */
2188e25f19bSMatthew Dillon enum worker_state { WORKER_NONE, WORKER_IDLE, WORKER_PENDING,
2198e25f19bSMatthew Dillon 		    WORKER_RUNNING, WORKER_DONE, WORKER_FAILED,
2208ec23ca1SMatthew Dillon 		    WORKER_FROZEN, WORKER_EXITING };
2218e25f19bSMatthew Dillon typedef enum worker_state worker_state_t;
2228e25f19bSMatthew Dillon 
2238e25f19bSMatthew Dillon enum worker_phase { PHASE_PENDING,
2248e25f19bSMatthew Dillon 		    PHASE_INSTALL_PKGS,
2258e25f19bSMatthew Dillon 		    PHASE_CHECK_SANITY,
2268e25f19bSMatthew Dillon 		    PHASE_PKG_DEPENDS,
2278e25f19bSMatthew Dillon 		    PHASE_FETCH_DEPENDS,
2288e25f19bSMatthew Dillon 		    PHASE_FETCH,
2298e25f19bSMatthew Dillon 		    PHASE_CHECKSUM,
2308e25f19bSMatthew Dillon 		    PHASE_EXTRACT_DEPENDS,
2318e25f19bSMatthew Dillon 		    PHASE_EXTRACT,
2328e25f19bSMatthew Dillon 		    PHASE_PATCH_DEPENDS,
2338e25f19bSMatthew Dillon 		    PHASE_PATCH,
2348e25f19bSMatthew Dillon 		    PHASE_BUILD_DEPENDS,
2358e25f19bSMatthew Dillon 		    PHASE_LIB_DEPENDS,
2368e25f19bSMatthew Dillon 		    PHASE_CONFIGURE,
2378e25f19bSMatthew Dillon 		    PHASE_BUILD,
2388e25f19bSMatthew Dillon 		    PHASE_RUN_DEPENDS,
2398e25f19bSMatthew Dillon 		    PHASE_STAGE,
2408e25f19bSMatthew Dillon 		    PHASE_TEST,
2418e25f19bSMatthew Dillon 		    PHASE_CHECK_PLIST,
2428e25f19bSMatthew Dillon 		    PHASE_PACKAGE,
2438e25f19bSMatthew Dillon 		    PHASE_INSTALL_MTREE,
2448e25f19bSMatthew Dillon 		    PHASE_INSTALL,
2458e25f19bSMatthew Dillon 		    PHASE_DEINSTALL
2468e25f19bSMatthew Dillon 		};
2478e25f19bSMatthew Dillon 
2488e25f19bSMatthew Dillon typedef enum worker_phase worker_phase_t;
2498e25f19bSMatthew Dillon 
2508e25f19bSMatthew Dillon /*
2518e25f19bSMatthew Dillon  * Watchdog timeouts, in minutes, baseline, scales up with load/ncpus but
2528e25f19bSMatthew Dillon  * does not scale down.
2538e25f19bSMatthew Dillon  */
2548e25f19bSMatthew Dillon #define WDOG1	(5)
2558e25f19bSMatthew Dillon #define WDOG2	(10)
2568e25f19bSMatthew Dillon #define WDOG3	(15)
2578e25f19bSMatthew Dillon #define WDOG4	(30)
2588e25f19bSMatthew Dillon #define WDOG5	(60)
2598e25f19bSMatthew Dillon #define WDOG6	(60 + 30)
2608e25f19bSMatthew Dillon #define WDOG7	(60 * 2)
2618e25f19bSMatthew Dillon #define WDOG8	(60 * 2 + 30)
2628e25f19bSMatthew Dillon #define WDOG9	(60 * 3)
2638e25f19bSMatthew Dillon 
2648e25f19bSMatthew Dillon typedef struct worker {
2658e25f19bSMatthew Dillon 	int	index;		/* worker number 0..N-1 */
2668e25f19bSMatthew Dillon 	int	flags;
2678e25f19bSMatthew Dillon 	int	accum_error;	/* cumulative error */
2688e25f19bSMatthew Dillon 	int	mount_error;	/* mount and unmount error */
2698e25f19bSMatthew Dillon 	int	terminate : 1;	/* request sub-thread to terminate */
2708e25f19bSMatthew Dillon 	char	*basedir;	/* base directory including id */
2718e25f19bSMatthew Dillon 	char	*flavor;
2728e25f19bSMatthew Dillon 	pthread_t td;		/* pthread */
2738e25f19bSMatthew Dillon 	pthread_cond_t cond;	/* interlock cond (w/ WorkerMutex) */
2748e25f19bSMatthew Dillon 	pkg_t	*pkg;
2758e25f19bSMatthew Dillon 	worker_state_t state;	/* general worker state */
2768e25f19bSMatthew Dillon 	worker_phase_t phase;	/* phase control in childBuilderThread */
2778e25f19bSMatthew Dillon 	time_t	start_time;
2788e25f19bSMatthew Dillon 	long	lines;
279*516819d9SMatthew Dillon 	long	memuse;
2808e25f19bSMatthew Dillon 	pid_t	pid;
2818e25f19bSMatthew Dillon 	int	fds[2];		/* forked environment process */
2828e25f19bSMatthew Dillon 	char	status[64];
283fef2fc63SMatthew Dillon 	size_t	pkg_dep_size;	/* pkg dependency size(s) */
2848e25f19bSMatthew Dillon } worker_t;
2858e25f19bSMatthew Dillon 
2868e25f19bSMatthew Dillon #define WORKERF_STATUS_UPDATE	0x0001	/* display update */
2878e25f19bSMatthew Dillon #define WORKERF_SUCCESS		0x0002	/* completion flag */
2888e25f19bSMatthew Dillon #define WORKERF_FAILURE		0x0004	/* completion flag */
2898ec23ca1SMatthew Dillon #define WORKERF_FREEZE		0x0008	/* freeze the worker */
2908e25f19bSMatthew Dillon 
2918e25f19bSMatthew Dillon #define MOUNT_TYPE_MASK		0x000F
2928e25f19bSMatthew Dillon #define MOUNT_TYPE_TMPFS	0x0001
2938e25f19bSMatthew Dillon #define MOUNT_TYPE_NULLFS	0x0002
2948e25f19bSMatthew Dillon #define MOUNT_TYPE_DEVFS	0x0003
2958ec23ca1SMatthew Dillon #define MOUNT_TYPE_PROCFS	0x0004
2968e25f19bSMatthew Dillon #define MOUNT_TYPE_RW		0x0010
2978e25f19bSMatthew Dillon #define MOUNT_TYPE_BIG		0x0020
2988e25f19bSMatthew Dillon #define MOUNT_TYPE_TMP		0x0040
2998e25f19bSMatthew Dillon 
3008e25f19bSMatthew Dillon #define NULLFS_RO		(MOUNT_TYPE_NULLFS)
3018e25f19bSMatthew Dillon #define NULLFS_RW		(MOUNT_TYPE_NULLFS | MOUNT_TYPE_RW)
3028ec23ca1SMatthew Dillon #define PROCFS_RO		(MOUNT_TYPE_PROCFS)
3038e25f19bSMatthew Dillon #define TMPFS_RW		(MOUNT_TYPE_TMPFS | MOUNT_TYPE_RW)
3048e25f19bSMatthew Dillon #define TMPFS_RW_BIG		(MOUNT_TYPE_TMPFS | MOUNT_TYPE_RW |	\
3058e25f19bSMatthew Dillon 				 MOUNT_TYPE_BIG)
3068e25f19bSMatthew Dillon #define DEVFS_RW		(MOUNT_TYPE_DEVFS | MOUNT_TYPE_RW)
3078e25f19bSMatthew Dillon 
3088e25f19bSMatthew Dillon /*
3098e25f19bSMatthew Dillon  * IPC messages between the worker support thread and the worker process.
3108e25f19bSMatthew Dillon  */
3118e25f19bSMatthew Dillon typedef struct wmsg {
3128e25f19bSMatthew Dillon 	int	cmd;
3138e25f19bSMatthew Dillon 	int	status;
3148e25f19bSMatthew Dillon 	long	lines;
315*516819d9SMatthew Dillon 	long	memuse;
3168e25f19bSMatthew Dillon 	worker_phase_t phase;
3178e25f19bSMatthew Dillon } wmsg_t;
3188e25f19bSMatthew Dillon 
3198e25f19bSMatthew Dillon #define WMSG_CMD_STATUS_UPDATE	0x0001
3208e25f19bSMatthew Dillon #define WMSG_CMD_SUCCESS	0x0002
3218e25f19bSMatthew Dillon #define WMSG_CMD_FAILURE	0x0003
3228e25f19bSMatthew Dillon #define WMSG_CMD_INSTALL_PKGS	0x0004
3238e25f19bSMatthew Dillon #define WMSG_RES_INSTALL_PKGS	0x0005
3248ec23ca1SMatthew Dillon #define WMSG_CMD_FREEZEWORKER	0x0006
3258e25f19bSMatthew Dillon 
3268e25f19bSMatthew Dillon /*
3278e25f19bSMatthew Dillon  * Make variables and build environment
3288e25f19bSMatthew Dillon  */
3298e25f19bSMatthew Dillon typedef struct buildenv {
3308e25f19bSMatthew Dillon 	struct buildenv *next;
3318e25f19bSMatthew Dillon 	const char *label;
3328e25f19bSMatthew Dillon 	const char *data;
3336fd67931SMatthew Dillon 	int type;
3348e25f19bSMatthew Dillon } buildenv_t;
3358e25f19bSMatthew Dillon 
3368e25f19bSMatthew Dillon /*
3378e25f19bSMatthew Dillon  * Operating systems recognized by dsynth
3388e25f19bSMatthew Dillon  */
3398e25f19bSMatthew Dillon enum os_id {
3408e25f19bSMatthew Dillon 	OS_UNKNOWN, OS_DRAGONFLY, OS_FREEBSD, OS_NETBSD, OS_LINUX
3418e25f19bSMatthew Dillon };
3428e25f19bSMatthew Dillon 
3438e25f19bSMatthew Dillon typedef enum os_id os_id_t;
3448e25f19bSMatthew Dillon 
3458e25f19bSMatthew Dillon /*
3468e25f19bSMatthew Dillon  * DLOG
3478e25f19bSMatthew Dillon  */
3488e25f19bSMatthew Dillon #define DLOG_ALL	0	/* Usually stdout when curses disabled */
3498e25f19bSMatthew Dillon #define DLOG_SUCC	1	/* success_list.log		*/
3508e25f19bSMatthew Dillon #define DLOG_FAIL	2	/* failure_list.log		*/
3518e25f19bSMatthew Dillon #define DLOG_IGN	3	/* ignored_list.log		*/
3528e25f19bSMatthew Dillon #define DLOG_SKIP	4	/* skipped_list.log		*/
3538e25f19bSMatthew Dillon #define DLOG_ABN	5	/* abnormal_command_output	*/
3548e25f19bSMatthew Dillon #define DLOG_OBS	6	/* obsolete_packages.log	*/
3558e25f19bSMatthew Dillon #define DLOG_COUNT	7	/* total number of DLOGs	*/
356a574cbf0SMatthew Dillon #define DLOG_MASK	0x0FF
357a574cbf0SMatthew Dillon 
358a574cbf0SMatthew Dillon #define DLOG_FILTER	0x100	/* Filter out of stdout in non-curses mode  */
359a574cbf0SMatthew Dillon #define DLOG_RED	0x200	/* Print in color */
360a574cbf0SMatthew Dillon #define DLOG_GRN	0x400	/* Print in color */
3618e25f19bSMatthew Dillon 
3628e25f19bSMatthew Dillon #define dassert(exp, fmt, ...)		\
3638e25f19bSMatthew Dillon 	if (!(exp)) dpanic(fmt, ## __VA_ARGS__)
3648e25f19bSMatthew Dillon 
3658e25f19bSMatthew Dillon #define ddassert(exp)			\
3668e25f19bSMatthew Dillon 	dassert((exp), "\"%s\" line %d", __FILE__, __LINE__)
3678e25f19bSMatthew Dillon 
3688e25f19bSMatthew Dillon #define dassert_errno(exp, fmt, ...)	\
3699c4c701fSMatthew Dillon 	if (!(exp)) dpanic_errno(fmt, ## __VA_ARGS__)
3708e25f19bSMatthew Dillon 
3718e25f19bSMatthew Dillon #define dlog(which, fmt, ...)		\
3728e25f19bSMatthew Dillon 	_dlog(which, fmt, ## __VA_ARGS__)
3738e25f19bSMatthew Dillon 
374a574cbf0SMatthew Dillon #define dlog_tsnl(which, fmt, ...)	\
375a574cbf0SMatthew Dillon 	_dlog(which, fmt, ## __VA_ARGS__)
376a574cbf0SMatthew Dillon 
3778e25f19bSMatthew Dillon #define dfatal(fmt, ...)		\
3788e25f19bSMatthew Dillon 	_dfatal(__FILE__, __LINE__, __func__, 0, fmt, ## __VA_ARGS__)
3798e25f19bSMatthew Dillon 
3808e25f19bSMatthew Dillon #define dpanic(fmt, ...)		\
3818e25f19bSMatthew Dillon 	_dfatal(__FILE__, __LINE__, __func__, 2, fmt, ## __VA_ARGS__)
3828e25f19bSMatthew Dillon 
3838e25f19bSMatthew Dillon #define dfatal_errno(fmt, ...)		\
3848e25f19bSMatthew Dillon 	_dfatal(__FILE__, __LINE__, __func__, 1, fmt, ## __VA_ARGS__)
3858e25f19bSMatthew Dillon 
3868e25f19bSMatthew Dillon #define dpanic_errno(fmt, ...)		\
3878e25f19bSMatthew Dillon 	_dfatal(__FILE__, __LINE__, __func__, 3, fmt, ## __VA_ARGS__)
3888e25f19bSMatthew Dillon 
3898e25f19bSMatthew Dillon #define ddprintf(tab, fmt, ...)		\
39054f2fefcSMatthew Dillon 	do { if (DebugOpt >= 2) _ddprintf(tab, fmt, ## __VA_ARGS__); } while(0)
3918e25f19bSMatthew Dillon 
3926fd67931SMatthew Dillon /*
3936fd67931SMatthew Dillon  * addbuildenv() types
3946fd67931SMatthew Dillon  */
3956fd67931SMatthew Dillon #define BENV_ENVIRONMENT	1
3966fd67931SMatthew Dillon #define BENV_MAKECONF		2
3976fd67931SMatthew Dillon 
3986fd67931SMatthew Dillon /*
3996fd67931SMatthew Dillon  * WORKER process flags
4006fd67931SMatthew Dillon  */
4016fd67931SMatthew Dillon #define WORKER_PROC_DEBUGSTOP	0x0001
4026fd67931SMatthew Dillon #define WORKER_PROC_DEVELOPER	0x0002
4036fd67931SMatthew Dillon 
4046fd67931SMatthew Dillon /*
4056fd67931SMatthew Dillon  * Misc
4066fd67931SMatthew Dillon  */
4078e25f19bSMatthew Dillon #define DOSTRING(label)	#label
4088e25f19bSMatthew Dillon #define SCRIPTPATH(x)	DOSTRING(x)
4096fd67931SMatthew Dillon #define MAXCAC		256
4108e25f19bSMatthew Dillon 
4118e25f19bSMatthew Dillon extern int BuildCount;
4128e25f19bSMatthew Dillon extern int BuildTotal;
4138e25f19bSMatthew Dillon extern int BuildFailCount;
4148e25f19bSMatthew Dillon extern int BuildSkipCount;
41587017ac4SMatthew Dillon extern int BuildIgnoreCount;
4168e25f19bSMatthew Dillon extern int BuildSuccessCount;
417f7f25838SMatthew Dillon extern int DynamicMaxWorkers;
4188e25f19bSMatthew Dillon 
4198e25f19bSMatthew Dillon extern buildenv_t *BuildEnv;
4206fd67931SMatthew Dillon extern int WorkerProcFlags;
4218e25f19bSMatthew Dillon extern int DebugOpt;
422a574cbf0SMatthew Dillon extern int ColorOpt;
423f7f25838SMatthew Dillon extern int SlowStartOpt;
4241645cafeSMatthew Dillon extern int YesOpt;
4259c4c701fSMatthew Dillon extern int NullStdinOpt;
4268e25f19bSMatthew Dillon extern int UseCCache;
4278e25f19bSMatthew Dillon extern int UseTmpfs;
4288e25f19bSMatthew Dillon extern int NumCores;
4297f0eca56SMatthew Dillon extern long PhysMem;
4307f0eca56SMatthew Dillon extern long PkgDepMemoryTarget;
4318e25f19bSMatthew Dillon extern int MaxBulk;
4328e25f19bSMatthew Dillon extern int MaxWorkers;
4338e25f19bSMatthew Dillon extern int MaxJobs;
4348e25f19bSMatthew Dillon extern int UseTmpfsWork;
4358e25f19bSMatthew Dillon extern int UseTmpfsBase;
4368e25f19bSMatthew Dillon extern int UseNCurses;
4378e25f19bSMatthew Dillon extern int LeveragePrebuilt;
4388e25f19bSMatthew Dillon extern char *DSynthExecPath;
4398e25f19bSMatthew Dillon 
4408e25f19bSMatthew Dillon extern const char *OperatingSystemName;
4418e25f19bSMatthew Dillon extern const char *ArchitectureName;
4428e25f19bSMatthew Dillon extern const char *MachineName;
4438e25f19bSMatthew Dillon extern const char *ReleaseName;
4448e25f19bSMatthew Dillon extern const char *VersionName;
4458e25f19bSMatthew Dillon 
4468ec23ca1SMatthew Dillon extern const char *ConfigBase;
4478ec23ca1SMatthew Dillon extern const char *AltConfigBase;
4488e25f19bSMatthew Dillon extern const char *DPortsPath;
4498e25f19bSMatthew Dillon extern const char *CCachePath;
4508e25f19bSMatthew Dillon extern const char *SynthConfig;
4518e25f19bSMatthew Dillon extern const char *PackagesPath;
4528e25f19bSMatthew Dillon extern const char *RepositoryPath;
4538e25f19bSMatthew Dillon extern const char *OptionsPath;
4548e25f19bSMatthew Dillon extern const char *DistFilesPath;
4558e25f19bSMatthew Dillon extern const char *BuildBase;
4568e25f19bSMatthew Dillon extern const char *LogsPath;
4578e25f19bSMatthew Dillon extern const char *SystemPath;
4588e25f19bSMatthew Dillon 
4598e25f19bSMatthew Dillon void _dfatal(const char *file, int line, const char *func, int do_errno,
4608e25f19bSMatthew Dillon 	     const char *fmt, ...);
4618e25f19bSMatthew Dillon void _ddprintf(int tab, const char *fmt, ...);
4628e25f19bSMatthew Dillon void _dlog(int which, const char *fmt, ...);
4634ea2ee4dSMatthew Dillon char *strdup_or_null(char *str);
4648e25f19bSMatthew Dillon void dlogreset(void);
46554f2fefcSMatthew Dillon int dlog00_fd(void);
4666fd67931SMatthew Dillon void addbuildenv(const char *label, const char *data, int type);
4678e25f19bSMatthew Dillon 
4688e25f19bSMatthew Dillon void initbulk(void (*func)(bulk_t *bulk), int jobs);
4697f0eca56SMatthew Dillon void queuebulk(const char *s1, const char *s2, const char *s3,
4708e25f19bSMatthew Dillon 			const char *s4);
4718e25f19bSMatthew Dillon bulk_t *getbulk(void);
4728e25f19bSMatthew Dillon void donebulk(void);
4738e25f19bSMatthew Dillon void freebulk(bulk_t *bulk);
4748e25f19bSMatthew Dillon void freestrp(char **strp);
4758e25f19bSMatthew Dillon void dupstrp(char **strp);
4761645cafeSMatthew Dillon int askyn(const char *ctl, ...);
4773699ee09SMatthew Dillon double getswappct(int *noswapp);
4786fd67931SMatthew Dillon FILE *dexec_open(const char **cav, int cac, pid_t *pidp,
4796fd67931SMatthew Dillon 			int with_env, int with_mvars);
4806fd67931SMatthew Dillon int dexec_close(FILE *fp, pid_t pid);
4813cebe4a8SMatthew Dillon const char *getphasestr(worker_phase_t phase);
4828e25f19bSMatthew Dillon 
4838e25f19bSMatthew Dillon void ParseConfiguration(int isworker);
4848e25f19bSMatthew Dillon pkg_t *ParsePackageList(int ac, char **av);
4858e25f19bSMatthew Dillon void FreePackageList(pkg_t *pkgs);
4868e25f19bSMatthew Dillon pkg_t *GetLocalPackageList(void);
4878e25f19bSMatthew Dillon pkg_t *GetFullPackageList(void);
4888e25f19bSMatthew Dillon pkg_t *GetPkgPkg(pkg_t *list);
4898e25f19bSMatthew Dillon 
4908e25f19bSMatthew Dillon void DoConfigure(void);
4911645cafeSMatthew Dillon void DoStatus(pkg_t *pkgs);
4928e25f19bSMatthew Dillon void DoBuild(pkg_t *pkgs);
4938e25f19bSMatthew Dillon void DoInitBuild(int slot_override);
49487017ac4SMatthew Dillon void DoCleanBuild(int resetlogs);
4958e25f19bSMatthew Dillon void WorkerProcess(int ac, char **av);
4968e25f19bSMatthew Dillon 
4971d6e00cdSMatthew Dillon int DoCreateTemplate(int force);
4988e25f19bSMatthew Dillon void DoDestroyTemplate(void);
4998e25f19bSMatthew Dillon void DoWorkerMounts(worker_t *work);
5008e25f19bSMatthew Dillon void DoWorkerUnmounts(worker_t *work);
5018e25f19bSMatthew Dillon void DoRebuildRepo(int ask);
5028e25f19bSMatthew Dillon void DoUpgradePkgs(pkg_t *pkgs, int ask);
5038e25f19bSMatthew Dillon void RemovePackages(pkg_t *pkgs);
5048e25f19bSMatthew Dillon void PurgeDistfiles(pkg_t *pkgs);
5058e25f19bSMatthew Dillon 
5068e25f19bSMatthew Dillon void GuiInit(void);
5073699ee09SMatthew Dillon void GuiDone(void);
5088e25f19bSMatthew Dillon void GuiReset(void);
5098e25f19bSMatthew Dillon void GuiUpdate(worker_t *work);
5108e25f19bSMatthew Dillon void GuiUpdateTop(void);
51154f2fefcSMatthew Dillon void GuiUpdateLogs(void);
5128e25f19bSMatthew Dillon void GuiSync(void);
5138e25f19bSMatthew Dillon 
5148e25f19bSMatthew Dillon int ipcreadmsg(int fd, wmsg_t *msg);
5158e25f19bSMatthew Dillon int ipcwritemsg(int fd, wmsg_t *msg);
516