dsynth - Abstract the run-status infrastructure
[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 #include <elf.h>
63
64 struct pkglink;
65
66 #define DSYNTH_VERSION  "1.01"
67 #define MAXWORKERS      1024
68 #define MAXJOBS         8192    /* just used for -j sanity */
69 #define MAXBULK         MAXWORKERS
70
71 #define MAKE_BINARY             "/usr/bin/make"
72 #define PKG_BINARY              "/usr/local/sbin/pkg"
73 #define MOUNT_BINARY            "/sbin/mount"
74 #define MOUNT_NULLFS_BINARY     "/sbin/mount_null"
75 #define MOUNT_TMPFS_BINARY      "/sbin/mount_tmpfs"
76 #define MOUNT_DEVFS_BINARY      "/sbin/mount_devfs"
77 #define MOUNT_PROCFS_BINARY     "/sbin/mount_procfs"
78 #define UMOUNT_BINARY           "/sbin/umount"
79
80 #define ONEGB           (1024L * 1024 * 1024)
81 #define DISABLED_STR    "disabled"
82
83 /*
84  * This can be ".tar", ".tgz", ".txz", or ".tbz".
85  *
86  * .tar - very fast but you'll need 1TB+ of storage just for the package files.
87  * .txz - very compact but decompression speed is horrible.
88  * .tgz - reasonable compression, extremely fast decompression.  Roughly
89  *        1.1x to 2.0x the size of a .txz, but decompresses 10x faster.
90  * .tbz - worse than .tgz generally
91  */
92 #define USE_PKG_SUFX            ".tgz"
93
94 /*
95  * Topology linkages
96  */
97 typedef struct pkglink {
98         struct pkglink *next;
99         struct pkglink *prev;
100         struct pkg *pkg;
101         int     dep_type;
102 } pkglink_t;
103
104 #define DEP_TYPE_FETCH  1
105 #define DEP_TYPE_EXT    2
106 #define DEP_TYPE_PATCH  3
107 #define DEP_TYPE_BUILD  4
108 #define DEP_TYPE_LIB    5
109 #define DEP_TYPE_RUN    6
110
111 /*
112  * Describes a [flavored] package
113  */
114 typedef struct pkg {
115         struct pkg *build_next; /* topology inversion build list */
116         struct pkg *bnext;      /* linked list from bulk return */
117         struct pkg *hnext1;     /* hash based on portdir */
118         struct pkg *hnext2;     /* hash based on pkgfile */
119         pkglink_t idepon_list;  /* I need these pkgs */
120         pkglink_t deponi_list;  /* pkgs which depend on me */
121         char *portdir;          /* origin name e.g. www/chromium[@flavor] */
122         char *logfile;          /* relative logfile path */
123         char *version;          /* PKGVERSION - e.g. 3.5.0_1            */
124         char *pkgfile;          /* PKGFILE    - e.g. flav-blah-3.5.0_1.txz */
125         char *distfiles;        /* DISTFILES  - e.g. blah-68.0.source.tar.xz */
126         char *distsubdir;       /* DIST_SUBDIR- e.g. cabal              */
127         char *ignore;           /* IGNORE (also covers BROKEN)          */
128         char *fetch_deps;       /* FETCH_DEPENDS                        */
129         char *ext_deps;         /* EXTRACT_DEPENDS                      */
130         char *patch_deps;       /* PATCH_DEPENDS                        */
131         char *build_deps;       /* BUILD_DEPENDS                        */
132         char *lib_deps;         /* LIB_DEPENDS                          */
133         char *run_deps;         /* RUN_DEPENDS                          */
134         char *pos_options;      /* SELECTED_OPTIONS                     */
135         char *neg_options;      /* DESELECTED_OPTIONS                   */
136         char *flavors;          /* FLAVORS    - e.g. py36 py27          */
137         char *uses;             /* USES (metaport test)                 */
138         int make_jobs_number;   /* MAKE_JOBS_NUMBER                     */
139         int use_linux;          /* USE_LINUX                            */
140         int idep_count;         /* count recursive idepon build deps    */
141         int depi_count;         /* count recursive deponi build deps    */
142         int depi_depth;         /* tree depth who depends on me         */
143         int dsynth_install_flg; /* locked with WorkerMutex      */
144         int flags;
145         size_t pkgfile_size;    /* size of pkgfile */
146 } pkg_t;
147
148 #define PKGF_PACKAGED   0x00000001      /* has a repo package */
149 #define PKGF_DUMMY      0x00000002      /* generic root for flavors */
150 #define PKGF_NOTFOUND   0x00000004      /* dport not found */
151 #define PKGF_CORRUPT    0x00000008      /* dport corrupt */
152 #define PKGF_PLACEHOLD  0x00000010      /* pre-entered */
153 #define PKGF_BUILDLIST  0x00000020      /* on build_list */
154 #define PKGF_BUILDLOOP  0x00000040      /* traversal loop test */
155 #define PKGF_BUILDTRAV  0x00000080      /* traversal optimization */
156 #define PKGF_NOBUILD_D  0x00000100      /* can't build - dependency problem */
157 #define PKGF_NOBUILD_S  0x00000200      /* can't build - skipped */
158 #define PKGF_NOBUILD_F  0x00000400      /* can't build - failed */
159 #define PKGF_NOBUILD_I  0x00000800      /* can't build - ignored or broken */
160 #define PKGF_SUCCESS    0x00001000      /* build complete */
161 #define PKGF_FAILURE    0x00002000      /* build complete */
162 #define PKGF_RUNNING    0x00004000      /* build complete */
163 #define PKGF_PKGPKG     0x00008000      /* pkg/pkg-static special */
164 #define PKGF_NOTREADY   0x00010000      /* build_find_leaves() only */
165 #define PKGF_MANUALSEL  0x00020000      /* manually specified */
166 #define PKGF_META       0x00040000      /* USES contains 'metaport' */
167 #define PKGF_ERROR      (PKGF_PLACEHOLD | PKGF_CORRUPT | PKGF_NOTFOUND | \
168                          PKGF_FAILURE)
169 #define PKGF_NOBUILD    (PKGF_NOBUILD_D | PKGF_NOBUILD_S | PKGF_NOBUILD_F | \
170                          PKGF_NOBUILD_I)
171
172 #define PKGLIST_EMPTY(pkglink)          ((pkglink)->next == (pkglink))
173 #define PKGLIST_FOREACH(var, head)      \
174         for (var = (head)->next; var != (head); var = (var)->next)
175
176 typedef struct bulk {
177         struct bulk *next;
178         pthread_t td;
179         int debug;
180         int flags;
181         enum { UNLISTED, ONSUBMIT, ONRUN, ISRUNNING, ONRESPONSE } state;
182         char *s1;
183         char *s2;
184         char *s3;
185         char *s4;
186         char *r1;
187         char *r2;
188         char *r3;
189         char *r4;
190         pkg_t *list;            /* pkgs linked by bnext */
191 } bulk_t;
192
193 /*
194  * Worker state (up to MAXWORKERS).  Each worker operates within a
195  * chroot or jail.  A system mirror is setup and the template
196  * is copied in.
197  *
198  * basedir              - tmpfs
199  * /bin                 - nullfs (ro)
200  * /sbin                - nullfs (ro)
201  * /lib                 - nullfs (ro)
202  * /libexec             - nullfs (ro)
203  * /usr/bin             - nullfs (ro)
204  * /usr/include         - nullfs (ro)
205  * /usr/lib             - nullfs (ro)
206  * /usr/libdata         - nullfs (ro)
207  * /usr/libexec         - nullfs (ro)
208  * /usr/sbin            - nullfs (ro)
209  * /usr/share           - nullfs (ro)
210  * /xports              - nullfs (ro)
211  * /options             - nullfs (ro)
212  * /packages            - nullfs (ro)
213  * /distfiles           - nullfs (ro)
214  * construction         - tmpfs
215  * /usr/local           - tmpfs
216  * /boot                - nullfs (ro)
217  * /boot/modules.local  - tmpfs
218  * /usr/games           - nullfs (ro)
219  * /usr/src             - nullfs (ro)
220  * /dev                 - devfs
221  */
222 enum worker_state { WORKER_NONE, WORKER_IDLE, WORKER_PENDING,
223                     WORKER_RUNNING, WORKER_DONE, WORKER_FAILED,
224                     WORKER_FROZEN, WORKER_EXITING };
225 typedef enum worker_state worker_state_t;
226
227 enum worker_phase { PHASE_PENDING,
228                     PHASE_INSTALL_PKGS,
229                     PHASE_CHECK_SANITY,
230                     PHASE_PKG_DEPENDS,
231                     PHASE_FETCH_DEPENDS,
232                     PHASE_FETCH,
233                     PHASE_CHECKSUM,
234                     PHASE_EXTRACT_DEPENDS,
235                     PHASE_EXTRACT,
236                     PHASE_PATCH_DEPENDS,
237                     PHASE_PATCH,
238                     PHASE_BUILD_DEPENDS,
239                     PHASE_LIB_DEPENDS,
240                     PHASE_CONFIGURE,
241                     PHASE_BUILD,
242                     PHASE_RUN_DEPENDS,
243                     PHASE_STAGE,
244                     PHASE_TEST,
245                     PHASE_CHECK_PLIST,
246                     PHASE_PACKAGE,
247                     PHASE_INSTALL_MTREE,
248                     PHASE_INSTALL,
249                     PHASE_DEINSTALL
250                 };
251
252 typedef enum worker_phase worker_phase_t;
253
254 /*
255  * Watchdog timeouts, in minutes, baseline, scales up with load/ncpus but
256  * does not scale down.
257  */
258 #define WDOG1   (5)
259 #define WDOG2   (10)
260 #define WDOG3   (15)
261 #define WDOG4   (30)
262 #define WDOG5   (60)
263 #define WDOG6   (60 + 30)
264 #define WDOG7   (60 * 2)
265 #define WDOG8   (60 * 2 + 30)
266 #define WDOG9   (60 * 3)
267
268 typedef struct worker {
269         int     index;          /* worker number 0..N-1 */
270         int     flags;
271         int     accum_error;    /* cumulative error */
272         int     mount_error;    /* mount and unmount error */
273         int     terminate : 1;  /* request sub-thread to terminate */
274         char    *basedir;       /* base directory including id */
275         char    *flavor;
276         pthread_t td;           /* pthread */
277         pthread_cond_t cond;    /* interlock cond (w/ WorkerMutex) */
278         pkg_t   *pkg;
279         worker_state_t state;   /* general worker state */
280         worker_phase_t phase;   /* phase control in childBuilderThread */
281         time_t  start_time;
282         long    lines;
283         long    memuse;
284         pid_t   pid;
285         int     fds[2];         /* forked environment process */
286         char    status[64];
287         size_t  pkg_dep_size;   /* pkg dependency size(s) */
288 } worker_t;
289
290 #define WORKERF_STATUS_UPDATE   0x0001  /* display update */
291 #define WORKERF_SUCCESS         0x0002  /* completion flag */
292 #define WORKERF_FAILURE         0x0004  /* completion flag */
293 #define WORKERF_FREEZE          0x0008  /* freeze the worker */
294
295 #define MOUNT_TYPE_MASK         0x000F
296 #define MOUNT_TYPE_TMPFS        0x0001
297 #define MOUNT_TYPE_NULLFS       0x0002
298 #define MOUNT_TYPE_DEVFS        0x0003
299 #define MOUNT_TYPE_PROCFS       0x0004
300 #define MOUNT_TYPE_RW           0x0010
301 #define MOUNT_TYPE_BIG          0x0020
302 #define MOUNT_TYPE_TMP          0x0040
303
304 #define NULLFS_RO               (MOUNT_TYPE_NULLFS)
305 #define NULLFS_RW               (MOUNT_TYPE_NULLFS | MOUNT_TYPE_RW)
306 #define PROCFS_RO               (MOUNT_TYPE_PROCFS)
307 #define TMPFS_RW                (MOUNT_TYPE_TMPFS | MOUNT_TYPE_RW)
308 #define TMPFS_RW_BIG            (MOUNT_TYPE_TMPFS | MOUNT_TYPE_RW |     \
309                                  MOUNT_TYPE_BIG)
310 #define DEVFS_RW                (MOUNT_TYPE_DEVFS | MOUNT_TYPE_RW)
311
312 /*
313  * IPC messages between the worker support thread and the worker process.
314  */
315 typedef struct wmsg {
316         int     cmd;
317         int     status;
318         long    lines;
319         long    memuse;
320         worker_phase_t phase;
321 } wmsg_t;
322
323 #define WMSG_CMD_STATUS_UPDATE  0x0001
324 #define WMSG_CMD_SUCCESS        0x0002
325 #define WMSG_CMD_FAILURE        0x0003
326 #define WMSG_CMD_INSTALL_PKGS   0x0004
327 #define WMSG_RES_INSTALL_PKGS   0x0005
328 #define WMSG_CMD_FREEZEWORKER   0x0006
329
330 /*
331  * Make variables and build environment
332  */
333 typedef struct buildenv {
334         struct buildenv *next;
335         char *label;
336         char *data;
337         int type;
338 } buildenv_t;
339
340 /*
341  * Operating systems recognized by dsynth
342  */
343 enum os_id {
344         OS_UNKNOWN, OS_DRAGONFLY, OS_FREEBSD, OS_NETBSD, OS_LINUX
345 };
346
347 typedef enum os_id os_id_t;
348
349 /*
350  * DLOG
351  */
352 #define DLOG_ALL        0       /* Usually stdout when curses disabled */
353 #define DLOG_SUCC       1       /* success_list.log             */
354 #define DLOG_FAIL       2       /* failure_list.log             */
355 #define DLOG_IGN        3       /* ignored_list.log             */
356 #define DLOG_SKIP       4       /* skipped_list.log             */
357 #define DLOG_ABN        5       /* abnormal_command_output      */
358 #define DLOG_OBS        6       /* obsolete_packages.log        */
359 #define DLOG_COUNT      7       /* total number of DLOGs        */
360 #define DLOG_MASK       0x0FF
361
362 #define DLOG_FILTER     0x100   /* Filter out of stdout in non-curses mode  */
363 #define DLOG_RED        0x200   /* Print in color */
364 #define DLOG_GRN        0x400   /* Print in color */
365
366 #define dassert(exp, fmt, ...)          \
367         if (!(exp)) dpanic(fmt, ## __VA_ARGS__)
368
369 #define ddassert(exp)                   \
370         dassert((exp), "\"%s\" line %d", __FILE__, __LINE__)
371
372 #define dassert_errno(exp, fmt, ...)    \
373         if (!(exp)) dpanic_errno(fmt, ## __VA_ARGS__)
374
375 #define dlog(which, fmt, ...)           \
376         _dlog(which, fmt, ## __VA_ARGS__)
377
378 #define dlog_tsnl(which, fmt, ...)      \
379         _dlog(which, fmt, ## __VA_ARGS__)
380
381 #define dfatal(fmt, ...)                \
382         _dfatal(__FILE__, __LINE__, __func__, 0, fmt, ## __VA_ARGS__)
383
384 #define dpanic(fmt, ...)                \
385         _dfatal(__FILE__, __LINE__, __func__, 2, fmt, ## __VA_ARGS__)
386
387 #define dfatal_errno(fmt, ...)          \
388         _dfatal(__FILE__, __LINE__, __func__, 1, fmt, ## __VA_ARGS__)
389
390 #define dpanic_errno(fmt, ...)          \
391         _dfatal(__FILE__, __LINE__, __func__, 3, fmt, ## __VA_ARGS__)
392
393 #define ddprintf(tab, fmt, ...)         \
394         do { if (DebugOpt >= 2) _ddprintf(tab, fmt, ## __VA_ARGS__); } while(0)
395
396 /*
397  * addbuildenv() types
398  */
399 #define BENV_ENVIRONMENT        1
400 #define BENV_MAKECONF           2
401 #define BENV_CMDMASK            0x000F
402
403 #define BENV_PKGLIST            0x0010
404
405 /*
406  * WORKER process flags
407  */
408 #define WORKER_PROC_DEBUGSTOP   0x0001
409 #define WORKER_PROC_DEVELOPER   0x0002
410
411 /*
412  * Misc
413  */
414 #define DOSTRING(label) #label
415 #define SCRIPTPATH(x)   DOSTRING(x)
416 #define MAXCAC          256
417
418 /*
419  * RunStats satellite modules
420  */
421 typedef struct topinfo {
422         int pkgimpulse;
423         int pkgrate;
424         int noswap;
425         int h;
426         int m;
427         int s;
428         int total;
429         int successful;
430         int ignored;
431         int remaining;
432         int failed;
433         int skipped;
434         double dswap;
435         double dload[3];
436 } topinfo_t;
437
438 typedef struct runstats {
439         struct runstats *next;
440         void (*init)(void);
441         void (*done)(void);
442         void (*reset)(void);
443         void (*update)(worker_t *work);
444         void (*updateTop)(topinfo_t *info);
445         void (*updateLogs)(void);
446         void (*sync)(void);
447 } runstats_t;
448
449 extern runstats_t NCursesRunStats;
450 extern runstats_t MonitorRunStats;
451 extern runstats_t HtmlRunStats;
452
453 extern int BuildCount;
454 extern int BuildTotal;
455 extern int BuildFailCount;
456 extern int BuildSkipCount;
457 extern int BuildIgnoreCount;
458 extern int BuildSuccessCount;
459 extern int DynamicMaxWorkers;
460
461 extern buildenv_t *BuildEnv;
462 extern int WorkerProcFlags;
463 extern int DebugOpt;
464 extern int ColorOpt;
465 extern int SlowStartOpt;
466 extern int YesOpt;
467 extern int NullStdinOpt;
468 extern int UseCCache;
469 extern int UseUsrSrc;
470 extern int UseTmpfs;
471 extern int NumCores;
472 extern long PhysMem;
473 extern long PkgDepMemoryTarget;
474 extern int MaxBulk;
475 extern int MaxWorkers;
476 extern int MaxJobs;
477 extern int UseTmpfsWork;
478 extern int UseTmpfsBase;
479 extern int UseNCurses;
480 extern int LeveragePrebuilt;
481 extern char *DSynthExecPath;
482
483
484 extern const char *OperatingSystemName;
485 extern const char *ArchitectureName;
486 extern const char *MachineName;
487 extern const char *ReleaseName;
488 extern const char *VersionName;
489
490 extern const char *ConfigBase;
491 extern const char *AltConfigBase;
492 extern const char *DPortsPath;
493 extern const char *CCachePath;
494 extern const char *SynthConfig;
495 extern const char *PackagesPath;
496 extern const char *RepositoryPath;
497 extern const char *OptionsPath;
498 extern const char *DistFilesPath;
499 extern const char *BuildBase;
500 extern const char *LogsPath;
501 extern const char *SystemPath;
502
503 void _dfatal(const char *file, int line, const char *func, int do_errno,
504              const char *fmt, ...);
505 void _ddprintf(int tab, const char *fmt, ...);
506 void _dlog(int which, const char *fmt, ...);
507 char *strdup_or_null(char *str);
508 void dlogreset(void);
509 int dlog00_fd(void);
510 void addbuildenv(const char *label, const char *data, int type);
511 void delbuildenv(const char *label);
512
513 void initbulk(void (*func)(bulk_t *bulk), int jobs);
514 void queuebulk(const char *s1, const char *s2, const char *s3,
515                         const char *s4);
516 bulk_t *getbulk(void);
517 void donebulk(void);
518 void freebulk(bulk_t *bulk);
519 void freestrp(char **strp);
520 void dupstrp(char **strp);
521 int askyn(const char *ctl, ...);
522 double getswappct(int *noswapp);
523 FILE *dexec_open(const char **cav, int cac, pid_t *pidp,
524                         int with_env, int with_mvars);
525 int dexec_close(FILE *fp, pid_t pid);
526 const char *getphasestr(worker_phase_t phase);
527
528 void ParseConfiguration(int isworker);
529 pkg_t *ParsePackageList(int ac, char **av);
530 void FreePackageList(pkg_t *pkgs);
531 pkg_t *GetLocalPackageList(void);
532 pkg_t *GetFullPackageList(void);
533 pkg_t *GetPkgPkg(pkg_t *list);
534
535 void DoConfigure(void);
536 void DoStatus(pkg_t *pkgs);
537 void DoBuild(pkg_t *pkgs);
538 void DoInitBuild(int slot_override);
539 void DoCleanBuild(int resetlogs);
540 void OptimizeEnv(void);
541 void WorkerProcess(int ac, char **av);
542
543 int DoCreateTemplate(int force);
544 void DoDestroyTemplate(void);
545 void DoWorkerMounts(worker_t *work);
546 void DoWorkerUnmounts(worker_t *work);
547 void DoRebuildRepo(int ask);
548 void DoUpgradePkgs(pkg_t *pkgs, int ask);
549 void RemovePackages(pkg_t *pkgs);
550 void PurgeDistfiles(pkg_t *pkgs);
551
552 void RunStatsInit(void);
553 void RunStatsDone(void);
554 void RunStatsReset(void);
555 void RunStatsUpdate(worker_t *work);
556 void RunStatsUpdateTop(void);
557 void RunStatsUpdateLogs(void);
558 void RunStatsSync(void);
559
560 int ipcreadmsg(int fd, wmsg_t *msg);
561 int ipcwritemsg(int fd, wmsg_t *msg);