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