dsynth - Add manual page, change configuration directory, more
[dragonfly.git] / usr.bin / dsynth / dsynth.h
1 /*
2  * Copyright (c) 2019 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * This code uses concepts and configuration based on 'synth', by
8  * John R. Marino <draco@marino.st>, which was written in ada.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
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
19  *    distribution.
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.
23  *
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
35  * SUCH DAMAGE.
36  */
37
38 #include <sys/types.h>
39 #include <sys/wait.h>
40 #include <sys/stat.h>
41 #include <sys/sysctl.h>
42 #include <sys/socket.h>
43 #include <sys/mount.h>
44 #include <sys/procctl.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <stddef.h>
48 #include <stdarg.h>
49 #include <unistd.h>
50 #include <string.h>
51 #include <fcntl.h>
52 #include <signal.h>
53 #include <poll.h>
54 #include <assert.h>
55 #include <errno.h>
56 #include <pthread.h>
57 #include <dirent.h>
58 #include <termios.h>
59 #include <ctype.h>
60 #include <libutil.h>
61
62 struct pkglink;
63
64 #define DSYNTH_VERSION  "1.01"
65 #define MAXWORKERS      1024
66 #define MAXJOBS         8192    /* just used for -j sanity */
67 #define MAXBULK         MAXWORKERS
68
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"
77
78 /*
79  * Topology linkages
80  */
81 typedef struct pkglink {
82         struct pkglink *next;
83         struct pkglink *prev;
84         struct pkg *pkg;
85 } pkglink_t;
86
87 /*
88  * Describes a [flavored] package
89  */
90 typedef struct pkg {
91         struct pkg *build_next; /* topology inversion build list */
92         struct pkg *bnext;      /* linked list from bulk return */
93         struct pkg *hnext1;     /* hash based on portdir */
94         struct pkg *hnext2;     /* hash based on pkgfile */
95         pkglink_t idepon_list;  /* I need these pkgs */
96         pkglink_t deponi_list;  /* pkgs which depend on me */
97         char *portdir;          /* origin name e.g. www/chromium[@flavor] */
98         char *logfile;          /* relative logfile path */
99         char *version;          /* PKGVERSION - e.g. 3.5.0_1            */
100         char *pkgfile;          /* PKGFILE    - e.g. flav-blah-3.5.0_1.txz */
101         char *distfiles;        /* DISTFILES  - e.g. blah-68.0.source.tar.xz */
102         char *distsubdir;       /* DIST_SUBDIR- e.g. cabal              */
103         char *ignore;           /* IGNORE                               */
104         char *fetch_deps;       /* FETCH_DEPENDS                        */
105         char *ext_deps;         /* EXTRACT_DEPENDS                      */
106         char *patch_deps;       /* PATCH_DEPENDS                        */
107         char *build_deps;       /* BUILD_DEPENDS                        */
108         char *lib_deps;         /* LIB_DEPENDS                          */
109         char *run_deps;         /* RUN_DEPENDS                          */
110         char *pos_options;      /* SELECTED_OPTIONS                     */
111         char *neg_options;      /* DESELECTED_OPTIONS                   */
112         char *flavors;          /* FLAVORS    - e.g. py36 py27          */
113         int make_jobs_number;   /* MAKE_JOBS_NUMBER                     */
114         int use_linux;          /* USE_LINUX                            */
115         int idep_count;         /* count recursive idepon build deps    */
116         int depi_count;         /* count recursive deponi build deps    */
117         int dsynth_install_flg; /* locked with WorkerMutex      */
118         int flags;
119 } pkg_t;
120
121 #define PKGF_PACKAGED   0x00000001      /* has a repo package */
122 #define PKGF_DUMMY      0x00000002      /* generic root for flavors */
123 #define PKGF_NOTFOUND   0x00000004      /* dport not found */
124 #define PKGF_CORRUPT    0x00000008      /* dport corrupt */
125 #define PKGF_PLACEHOLD  0x00000010      /* pre-entered */
126 #define PKGF_BUILDLIST  0x00000020      /* on build_list */
127 #define PKGF_BUILDLOOP  0x00000040      /* traversal loop test */
128 #define PKGF_BUILDTRAV  0x00000080      /* traversal optimization */
129 #define PKGF_NOBUILD_D  0x00000100      /* can't build - dependency problem */
130 #define PKGF_NOBUILD_S  0x00000200      /* can't build - skipped */
131 #define PKGF_NOBUILD_F  0x00000400      /* can't build - failed */
132 #define PKGF_SUCCESS    0x00000800      /* build complete */
133 #define PKGF_FAILURE    0x00001000      /* build complete */
134 #define PKGF_RUNNING    0x00002000      /* build complete */
135 #define PKGF_PKGPKG     0x00004000      /* pkg/pkg-static special */
136 #define PKGF_NOTREADY   0x00008000      /* build_find_leaves() only */
137 #define PKGF_ERROR      (PKGF_PLACEHOLD | PKGF_CORRUPT | PKGF_NOTFOUND | \
138                          PKGF_FAILURE)
139 #define PKGF_NOBUILD    (PKGF_NOBUILD_D | PKGF_NOBUILD_S | PKGF_NOBUILD_F)
140
141 #define PKGLIST_EMPTY(pkglink)          ((pkglink)->next == (pkglink))
142 #define PKGLIST_FOREACH(var, head)      \
143         for (var = (head)->next; var != (head); var = (var)->next)
144
145 typedef struct bulk {
146         struct bulk *next;
147         pthread_t td;
148         int debug;
149         int flags;
150         enum { UNLISTED, ONSUBMIT, ONRUN, ISRUNNING, ONRESPONSE } state;
151         char *s1;
152         char *s2;
153         char *s3;
154         char *s4;
155         char *r1;
156         char *r2;
157         char *r3;
158         char *r4;
159         pkg_t *list;            /* pkgs linked by bnext */
160 } bulk_t;
161
162 /*
163  * Worker state (up to MAXWORKERS).  Each worker operates within a
164  * chroot or jail.  A system mirror is setup and the template
165  * is copied in.
166  *
167  * basedir              - tmpfs
168  * /bin                 - nullfs (ro)
169  * /sbin                - nullfs (ro)
170  * /lib                 - nullfs (ro)
171  * /libexec             - nullfs (ro)
172  * /usr/bin             - nullfs (ro)
173  * /usr/include         - nullfs (ro)
174  * /usr/lib             - nullfs (ro)
175  * /usr/libdata         - nullfs (ro)
176  * /usr/libexec         - nullfs (ro)
177  * /usr/sbin            - nullfs (ro)
178  * /usr/share           - nullfs (ro)
179  * /xports              - nullfs (ro)
180  * /options             - nullfs (ro)
181  * /packages            - nullfs (ro)
182  * /distfiles           - nullfs (ro)
183  * construction         - tmpfs
184  * /usr/local           - tmpfs
185  * /boot                - nullfs (ro)
186  * /boot/modules.local  - tmpfs
187  * /usr/games           - nullfs (ro)
188  * /usr/src             - nullfs (ro)
189  * /dev                 - devfs
190  */
191 enum worker_state { WORKER_NONE, WORKER_IDLE, WORKER_PENDING,
192                     WORKER_RUNNING, WORKER_DONE, WORKER_FAILED,
193                     WORKER_FROZEN, WORKER_EXITING };
194 typedef enum worker_state worker_state_t;
195
196 enum worker_phase { PHASE_PENDING,
197                     PHASE_INSTALL_PKGS,
198                     PHASE_CHECK_SANITY,
199                     PHASE_PKG_DEPENDS,
200                     PHASE_FETCH_DEPENDS,
201                     PHASE_FETCH,
202                     PHASE_CHECKSUM,
203                     PHASE_EXTRACT_DEPENDS,
204                     PHASE_EXTRACT,
205                     PHASE_PATCH_DEPENDS,
206                     PHASE_PATCH,
207                     PHASE_BUILD_DEPENDS,
208                     PHASE_LIB_DEPENDS,
209                     PHASE_CONFIGURE,
210                     PHASE_BUILD,
211                     PHASE_RUN_DEPENDS,
212                     PHASE_STAGE,
213                     PHASE_TEST,
214                     PHASE_CHECK_PLIST,
215                     PHASE_PACKAGE,
216                     PHASE_INSTALL_MTREE,
217                     PHASE_INSTALL,
218                     PHASE_DEINSTALL
219                 };
220
221 typedef enum worker_phase worker_phase_t;
222
223 /*
224  * Watchdog timeouts, in minutes, baseline, scales up with load/ncpus but
225  * does not scale down.
226  */
227 #define WDOG1   (5)
228 #define WDOG2   (10)
229 #define WDOG3   (15)
230 #define WDOG4   (30)
231 #define WDOG5   (60)
232 #define WDOG6   (60 + 30)
233 #define WDOG7   (60 * 2)
234 #define WDOG8   (60 * 2 + 30)
235 #define WDOG9   (60 * 3)
236
237 typedef struct worker {
238         int     index;          /* worker number 0..N-1 */
239         int     flags;
240         int     accum_error;    /* cumulative error */
241         int     mount_error;    /* mount and unmount error */
242         int     terminate : 1;  /* request sub-thread to terminate */
243         char    *basedir;       /* base directory including id */
244         char    *flavor;
245         pthread_t td;           /* pthread */
246         pthread_cond_t cond;    /* interlock cond (w/ WorkerMutex) */
247         pkg_t   *pkg;
248         worker_state_t state;   /* general worker state */
249         worker_phase_t phase;   /* phase control in childBuilderThread */
250         time_t  start_time;
251         long    lines;
252         pid_t   pid;
253         int     fds[2];         /* forked environment process */
254         char    status[64];
255 } worker_t;
256
257 #define WORKERF_STATUS_UPDATE   0x0001  /* display update */
258 #define WORKERF_SUCCESS         0x0002  /* completion flag */
259 #define WORKERF_FAILURE         0x0004  /* completion flag */
260 #define WORKERF_FREEZE          0x0008  /* freeze the worker */
261
262 #define MOUNT_TYPE_MASK         0x000F
263 #define MOUNT_TYPE_TMPFS        0x0001
264 #define MOUNT_TYPE_NULLFS       0x0002
265 #define MOUNT_TYPE_DEVFS        0x0003
266 #define MOUNT_TYPE_PROCFS       0x0004
267 #define MOUNT_TYPE_RW           0x0010
268 #define MOUNT_TYPE_BIG          0x0020
269 #define MOUNT_TYPE_TMP          0x0040
270
271 #define NULLFS_RO               (MOUNT_TYPE_NULLFS)
272 #define NULLFS_RW               (MOUNT_TYPE_NULLFS | MOUNT_TYPE_RW)
273 #define PROCFS_RO               (MOUNT_TYPE_PROCFS)
274 #define TMPFS_RW                (MOUNT_TYPE_TMPFS | MOUNT_TYPE_RW)
275 #define TMPFS_RW_BIG            (MOUNT_TYPE_TMPFS | MOUNT_TYPE_RW |     \
276                                  MOUNT_TYPE_BIG)
277 #define DEVFS_RW                (MOUNT_TYPE_DEVFS | MOUNT_TYPE_RW)
278
279 /*
280  * IPC messages between the worker support thread and the worker process.
281  */
282 typedef struct wmsg {
283         int     cmd;
284         int     status;
285         long    lines;
286         worker_phase_t phase;
287 } wmsg_t;
288
289 #define WMSG_CMD_STATUS_UPDATE  0x0001
290 #define WMSG_CMD_SUCCESS        0x0002
291 #define WMSG_CMD_FAILURE        0x0003
292 #define WMSG_CMD_INSTALL_PKGS   0x0004
293 #define WMSG_RES_INSTALL_PKGS   0x0005
294 #define WMSG_CMD_FREEZEWORKER   0x0006
295
296 /*
297  * Make variables and build environment
298  */
299 typedef struct buildenv {
300         struct buildenv *next;
301         const char *label;
302         const char *data;
303 } buildenv_t;
304
305 /*
306  * Operating systems recognized by dsynth
307  */
308 enum os_id {
309         OS_UNKNOWN, OS_DRAGONFLY, OS_FREEBSD, OS_NETBSD, OS_LINUX
310 };
311
312 typedef enum os_id os_id_t;
313
314 /*
315  * DLOG
316  */
317 #define DLOG_ALL        0       /* Usually stdout when curses disabled */
318 #define DLOG_SUCC       1       /* success_list.log             */
319 #define DLOG_FAIL       2       /* failure_list.log             */
320 #define DLOG_IGN        3       /* ignored_list.log             */
321 #define DLOG_SKIP       4       /* skipped_list.log             */
322 #define DLOG_ABN        5       /* abnormal_command_output      */
323 #define DLOG_OBS        6       /* obsolete_packages.log        */
324 #define DLOG_COUNT      7       /* total number of DLOGs        */
325
326 #define dassert(exp, fmt, ...)          \
327         if (!(exp)) dpanic(fmt, ## __VA_ARGS__)
328
329 #define ddassert(exp)                   \
330         dassert((exp), "\"%s\" line %d", __FILE__, __LINE__)
331
332 #define dassert_errno(exp, fmt, ...)    \
333         if (!(exp)) dpanic_errno(fmt, ## __VA_ARGS__)
334
335 #define dlog(which, fmt, ...)           \
336         _dlog(which, fmt, ## __VA_ARGS__)
337
338 #define dfatal(fmt, ...)                \
339         _dfatal(__FILE__, __LINE__, __func__, 0, fmt, ## __VA_ARGS__)
340
341 #define dpanic(fmt, ...)                \
342         _dfatal(__FILE__, __LINE__, __func__, 2, fmt, ## __VA_ARGS__)
343
344 #define dfatal_errno(fmt, ...)          \
345         _dfatal(__FILE__, __LINE__, __func__, 1, fmt, ## __VA_ARGS__)
346
347 #define dpanic_errno(fmt, ...)          \
348         _dfatal(__FILE__, __LINE__, __func__, 3, fmt, ## __VA_ARGS__)
349
350 #define ddprintf(tab, fmt, ...)         \
351         do { if (DebugOpt) _ddprintf(tab, fmt, ## __VA_ARGS__); } while(0)
352
353 #define DOSTRING(label) #label
354 #define SCRIPTPATH(x)   DOSTRING(x)
355
356 extern int BuildCount;
357 extern int BuildTotal;
358 extern int BuildFailCount;
359 extern int BuildSkipCount;
360 extern int BuildSuccessCount;
361 extern int DynamicMaxWorkers;
362
363 extern buildenv_t *BuildEnv;
364 extern int DebugOpt;
365 extern int DebugStopMode;
366 extern int SlowStartOpt;
367 extern int YesOpt;
368 extern int NullStdinOpt;
369 extern int UseCCache;
370 extern int UseTmpfs;
371 extern int NumCores;
372 extern int MaxBulk;
373 extern int MaxWorkers;
374 extern int MaxJobs;
375 extern int UseTmpfsWork;
376 extern int UseTmpfsBase;
377 extern int UseNCurses;
378 extern int LeveragePrebuilt;
379 extern char *DSynthExecPath;
380
381 extern const char *OperatingSystemName;
382 extern const char *ArchitectureName;
383 extern const char *MachineName;
384 extern const char *ReleaseName;
385 extern const char *VersionName;
386
387 extern const char *ConfigBase;
388 extern const char *AltConfigBase;
389 extern const char *DPortsPath;
390 extern const char *CCachePath;
391 extern const char *SynthConfig;
392 extern const char *PackagesPath;
393 extern const char *RepositoryPath;
394 extern const char *OptionsPath;
395 extern const char *DistFilesPath;
396 extern const char *BuildBase;
397 extern const char *LogsPath;
398 extern const char *SystemPath;
399
400 void _dfatal(const char *file, int line, const char *func, int do_errno,
401              const char *fmt, ...);
402 void _ddprintf(int tab, const char *fmt, ...);
403 void _dlog(int which, const char *fmt, ...);
404 void dlogreset(void);
405 void addbuildenv(const char *label, const char *data);
406
407 void initbulk(void (*func)(bulk_t *bulk), int jobs);
408 bulk_t *queuebulk(const char *s1, const char *s2, const char *s3,
409                         const char *s4);
410 bulk_t *getbulk(void);
411 void donebulk(void);
412 void freebulk(bulk_t *bulk);
413 void freestrp(char **strp);
414 void dupstrp(char **strp);
415 int askyn(const char *ctl, ...);
416 double getswappct(int *noswapp);
417
418 void ParseConfiguration(int isworker);
419 pkg_t *ParsePackageList(int ac, char **av);
420 void FreePackageList(pkg_t *pkgs);
421 pkg_t *GetLocalPackageList(void);
422 pkg_t *GetFullPackageList(void);
423 pkg_t *GetPkgPkg(pkg_t *list);
424
425 void DoConfigure(void);
426 void DoStatus(pkg_t *pkgs);
427 void DoBuild(pkg_t *pkgs);
428 void DoInitBuild(int slot_override);
429 void DoCleanBuild(void);
430 void WorkerProcess(int ac, char **av);
431
432 int DoCreateTemplate(int force);
433 void DoDestroyTemplate(void);
434 void DoWorkerMounts(worker_t *work);
435 void DoWorkerUnmounts(worker_t *work);
436 void DoRebuildRepo(int ask);
437 void DoUpgradePkgs(pkg_t *pkgs, int ask);
438 void RemovePackages(pkg_t *pkgs);
439 void PurgeDistfiles(pkg_t *pkgs);
440
441 void GuiInit(void);
442 void GuiDone(void);
443 void GuiReset(void);
444 void GuiUpdate(worker_t *work);
445 void GuiUpdateTop(void);
446 void GuiSync(void);
447
448 int ipcreadmsg(int fd, wmsg_t *msg);
449 int ipcwritemsg(int fd, wmsg_t *msg);