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