xref: /dragonfly/usr.bin/dsynth/dsynth.c (revision 029e6489)
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 "dsynth.h"
39 
40 static void DoInit(void);
41 static void usage(int ecode) __dead2;
42 
43 int OverridePkgDeleteOpt;
44 int YesOpt;
45 int DebugOpt;
46 int MaskProbeAbort;
47 int ColorOpt = 1;
48 int NullStdinOpt = 1;
49 int SlowStartOpt = -1;
50 long PkgDepMemoryTarget;
51 char *DSynthExecPath;
52 char *ProfileOverrideOpt;
53 int NiceOpt = 10;
54 
55 int
56 main(int ac, char **av)
57 {
58 	pkg_t *pkgs;
59 	int isworker;
60 	int c;
61 	int sopt;
62 
63 	/*
64 	 * Get our exec path so we can self-exec clean WORKER
65 	 * processes.
66 	 */
67 	{
68 		size_t len;
69 		const int name[] = {
70 			CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1,
71 		};
72 		if (sysctl(name, 4, NULL, &len, NULL, 0) < 0)
73 			dfatal_errno("Cannot get binary path");
74 		DSynthExecPath = malloc(len + 1);
75 		if (sysctl(name, 4, DSynthExecPath, &len, NULL, 0) < 0)
76 			dfatal_errno("Cannot get binary path");
77 		DSynthExecPath[len] = 0;
78 	}
79 
80 	/*
81 	 * Override profile in dsynth.ini (can be further overridden
82 	 * with the -p profile option).
83 	 */
84 	ProfileOverrideOpt = getenv("DSYNTH_PROFILE");
85 
86 	/*
87 	 * Process options and make sure the directive is present
88 	 */
89 	sopt = 0;
90 	while ((c = getopt(ac, av, "dhm:p:vxys:DPSN")) != -1) {
91 		switch(c) {
92 		case 'x':
93 			++OverridePkgDeleteOpt;
94 			break;
95 		case 'y':
96 			++YesOpt;
97 			break;
98 		case 'D':
99 			WorkerProcFlags |= WORKER_PROC_DEVELOPER;
100 			break;
101 		case 'P':
102 			WorkerProcFlags |= WORKER_PROC_CHECK_PLIST;
103 			break;
104 		case 'S':
105 			UseNCurses = 0;
106 			if (++sopt == 2)
107 				ColorOpt = 0;
108 			break;
109 		case 'N':
110 			NiceOpt = 0;
111 			break;
112 		case 'd':
113 			++DebugOpt;
114 			if (DebugOpt >= 2)
115 				UseNCurses = 0;
116 			break;
117 		case 'h':
118 			usage(0);
119 			/* NOT REACHED */
120 			exit(0);
121 		case 'v':
122 			printf("dsynth %s\n", DSYNTH_VERSION);
123 			exit(0);
124 		case 's':
125 			/*
126 			 * Start with N jobs, increasing to the configured
127 			 * maximum slowly.  0 to disable (starts with the
128 			 * full count).
129 			 */
130 			SlowStartOpt = strtol(optarg, NULL, 0);
131 			break;
132 		case 'm':
133 			PkgDepMemoryTarget = strtoul(optarg, NULL, 0);
134 			PkgDepMemoryTarget *= ONEGB;
135 			break;
136 		case 'p':
137 			ProfileOverrideOpt = optarg;
138 			break;
139 		default:
140 			fprintf(stderr, "Unknown option: %c\n", c);
141 			usage(2);
142 			/* NOT REACHED */
143 			break;
144 		}
145 	}
146 	ac -= optind;
147 	av += optind;
148 	pkgs = NULL;
149 	if (ac < 1) {
150 		fprintf(stderr, "Missing directive\n");
151 		usage(2);
152 		/* NOT REACHED */
153 	}
154 
155 	/*
156 	 * Directives which do not require a working configuration
157 	 */
158 	if (strcmp(av[0], "init") == 0) {
159 		DoInit();
160 		exit(0);
161 		/* NOT REACHED */
162 	}
163 	if (strcmp(av[0], "help") == 0) {
164 		usage(0);
165 		exit(0);
166 		/* NOT REACHED */
167 	}
168 	if (strcmp(av[0], "version") == 0) {
169 		printf("dsynth %s\n", DSYNTH_VERSION);
170 		exit(0);
171 		/* NOT REACHED */
172 	}
173 
174 	/*
175 	 * Preconfiguration.
176 	 */
177 	if (strcmp(av[0], "WORKER") == 0) {
178 		isworker = 1;
179 	} else {
180 		isworker = 0;
181 	}
182 
183 	signal(SIGPIPE, SIG_IGN);
184 	ParseConfiguration(isworker);
185 
186 	/*
187 	 * Setup some environment for bulk operations (pkglist scan).
188 	 * These are not used by the builder (the builder will replicate
189 	 * all of these).
190 	 */
191 	addbuildenv("PORTSDIR", DPortsPath,
192 		    BENV_ENVIRONMENT | BENV_PKGLIST);
193 	addbuildenv("BATCH", "yes",
194 		    BENV_ENVIRONMENT | BENV_PKGLIST);
195 	addbuildenv("PKG_SUFX", UsePkgSufx,
196 		    BENV_ENVIRONMENT | BENV_PKGLIST);
197 	addbuildenv("PACKAGE_BUILDING", "yes",
198 		    BENV_ENVIRONMENT | BENV_PKGLIST);
199 	addbuildenv("ARCH", ArchitectureName,
200 		    BENV_ENVIRONMENT | BENV_PKGLIST);
201 
202 #if 0
203 	/*
204 	 *
205 	 */
206 	addbuildenv("OSTYPE", OperatingSystemName,
207 		    BENV_ENVIRONMENT | BENV_PKGLIST);
208 	addbuildenv("MACHTYPE", MachineName,
209 		    BENV_ENVIRONMENT | BENV_PKGLIST);
210 #endif
211 	/*
212 	 * SlowStart auto adjust.  We nominally start with 1 job and increase
213 	 * it to the maximum every 5 seconds to give various dynamic management
214 	 * parameters time to stabilize.
215 	 *
216 	 * This can take a while on a many-core box with a high jobs setting,
217 	 * so increase the initial jobs in such cases.
218 	 */
219 	if (SlowStartOpt > MaxWorkers)
220 		SlowStartOpt = MaxWorkers;
221 	if (SlowStartOpt < 0) {
222 		if (MaxWorkers < 16)
223 			SlowStartOpt = 1;
224 		else
225 			SlowStartOpt = MaxWorkers / 4;
226 	}
227 
228 	/*
229 	 * Special directive for when dsynth execs itself to manage
230 	 * a worker chroot.
231 	 */
232 	if (isworker) {
233 		WorkerProcess(ac, av);
234 		exit(0);
235 	}
236 
237 	/*
238 	 * Build initialization and directive handling
239 	 */
240 	DoInitBuild(-1);
241 
242 	/*
243 	 * Directives that use the configuration but are not interlocked
244 	 * against a running dsynth.
245 	 */
246 	if (strcmp(av[0], "monitor") == 0) {
247 		char *spath;
248 		char *lpath;
249 
250 		if (ac == 1) {
251 			asprintf(&spath, "%s/%s", StatsBase, STATS_FILE);
252 			asprintf(&lpath, "%s/%s", StatsBase, STATS_LOCKFILE);
253 			MonitorDirective(spath, lpath);
254 			free(spath);
255 			free(lpath);
256 		} else {
257 			MonitorDirective(av[1], NULL);
258 		}
259 		exit(0);
260 		/* NOT REACHED */
261 	}
262 
263 	/*
264 	 * Front-end exec (not a WORKER exec), normal startup.  We have
265 	 * the configuration so the first thing we need to do is check
266 	 * the lock file.
267 	 */
268 	{
269 		char *lkpath;
270 		int fd;
271 
272 		asprintf(&lkpath, "%s/.lock", BuildBase);
273 		fd = open(lkpath, O_RDWR | O_CREAT | O_CLOEXEC, 0644);
274 		if (fd < 0)
275 			dfatal_errno("Unable to create %s", lkpath);
276 		if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
277 			dfatal("Another dsynth is using %s, exiting",
278 			       BuildBase);
279 		}
280 		/* leave descriptor open */
281 	}
282 
283 	if (strcmp(av[0], "debug") == 0) {
284 		DoCleanBuild(1);
285 		OptimizeEnv();
286 		pkgs = ParsePackageList(ac - 1, av + 1, 1);
287 		RemovePackages(pkgs);
288 		DoBuild(pkgs);
289 	} else if (strcmp(av[0], "status") == 0) {
290 		OptimizeEnv();
291 		if (ac - 1)
292 			pkgs = ParsePackageList(ac - 1, av + 1, 0);
293 		else
294 			pkgs = GetLocalPackageList();
295 		DoStatus(pkgs);
296 	} else if (strcmp(av[0], "cleanup") == 0) {
297 		DoCleanBuild(0);
298 	} else if (strcmp(av[0], "configure") == 0) {
299 		DoCleanBuild(0);
300 		DoConfigure();
301 	} else if (strcmp(av[0], "upgrade-system") == 0) {
302 		DoCleanBuild(1);
303 		OptimizeEnv();
304 		pkgs = GetLocalPackageList();
305 		DoBuild(pkgs);
306 		DoRebuildRepo(0);
307 		DoUpgradePkgs(pkgs, 0);
308 	} else if (strcmp(av[0], "prepare-system") == 0) {
309 		DoCleanBuild(1);
310 		OptimizeEnv();
311 		pkgs = GetLocalPackageList();
312 		DoBuild(pkgs);
313 		DoRebuildRepo(0);
314 	} else if (strcmp(av[0], "rebuild-repository") == 0) {
315 		OptimizeEnv();
316 		DoRebuildRepo(0);
317 	} else if (strcmp(av[0], "purge-distfiles") == 0) {
318 		OptimizeEnv();
319 		pkgs = GetFullPackageList();
320 		PurgeDistfiles(pkgs);
321 	} else if (strcmp(av[0], "reset-db") == 0) {
322 		char *dbmpath;
323 
324 		asprintf(&dbmpath, "%s/ports_crc.db", BuildBase);
325 		remove(dbmpath);
326 		printf("%s reset, will be regenerated on next build\n",
327 		       dbmpath);
328 		free(dbmpath);
329 	} else if (strcmp(av[0], "status-everything") == 0) {
330 		OptimizeEnv();
331 		pkgs = GetFullPackageList();
332 		DoStatus(pkgs);
333 	} else if (strcmp(av[0], "everything") == 0) {
334 		if (WorkerProcFlags & WORKER_PROC_DEVELOPER)
335 			WorkerProcFlags |= WORKER_PROC_CHECK_PLIST;
336 		MaskProbeAbort = 1;
337 		DeleteObsoletePkgs = 1;
338 		DoCleanBuild(1);
339 		OptimizeEnv();
340 		pkgs = GetFullPackageList();
341 		DoBuild(pkgs);
342 		DoRebuildRepo(1);
343 	} else if (strcmp(av[0], "build") == 0) {
344 		DoCleanBuild(1);
345 		OptimizeEnv();
346 		pkgs = ParsePackageList(ac - 1, av + 1, 0);
347 		DoBuild(pkgs);
348 		DoRebuildRepo(1);
349 		DoUpgradePkgs(pkgs, 1);
350 	} else if (strcmp(av[0], "just-build") == 0) {
351 		DoCleanBuild(1);
352 		OptimizeEnv();
353 		pkgs = ParsePackageList(ac - 1, av + 1, 0);
354 		DoBuild(pkgs);
355 	} else if (strcmp(av[0], "install") == 0) {
356 		DoCleanBuild(1);
357 		OptimizeEnv();
358 		pkgs = ParsePackageList(ac - 1, av + 1, 0);
359 		DoBuild(pkgs);
360 		DoRebuildRepo(0);
361 		DoUpgradePkgs(pkgs, 0);
362 	} else if (strcmp(av[0], "force") == 0) {
363 		DoCleanBuild(1);
364 		OptimizeEnv();
365 		pkgs = ParsePackageList(ac - 1, av + 1, 0);
366 		RemovePackages(pkgs);
367 		DoBuild(pkgs);
368 		DoRebuildRepo(1);
369 		DoUpgradePkgs(pkgs, 1);
370 	} else if (strcmp(av[0], "test") == 0) {
371 		WorkerProcFlags |= WORKER_PROC_CHECK_PLIST;
372 		DoCleanBuild(1);
373 		OptimizeEnv();
374 		pkgs = ParsePackageList(ac - 1, av + 1, 0);
375 		RemovePackages(pkgs);
376 		WorkerProcFlags |= WORKER_PROC_DEVELOPER;
377 		DoBuild(pkgs);
378 	} else {
379 		fprintf(stderr, "Unknown directive '%s'\n", av[0]);
380 		usage(2);
381 	}
382 	return 0;
383 }
384 
385 static void
386 DoInit(void)
387 {
388 	struct stat st;
389 	char *path;
390 	FILE *fp;
391 
392 	if (stat(ConfigBase1, &st) == 0) {
393 		dfatal("init will not overwrite %s", ConfigBase1);
394 	}
395 	if (stat(ConfigBase2, &st) == 0) {
396 		dfatal("init will not create %s if %s exists",
397 		       ConfigBase2, ConfigBase1);
398 	}
399 	if (mkdir(ConfigBase1, 0755) < 0)
400 		dfatal_errno("Unable to mkdir %s", ConfigBase1);
401 
402 	asprintf(&path, "%s/dsynth.ini", ConfigBase1);
403 	fp = fopen(path, "w");
404 	dassert_errno(fp, "Unable to create %s", path);
405 	fprintf(fp, "%s",
406 	    "; This Synth configuration file is automatically generated\n"
407 	    "; Take care when hand editing!\n"
408 	    "\n"
409 	    "[Global Configuration]\n"
410 	    "profile_selected= LiveSystem\n"
411 	    "\n"
412 	    "[LiveSystem]\n"
413 	    "Operating_system= DragonFly\n"
414 	    "Directory_packages= /build/synth/live_packages\n"
415 	    "Directory_repository= /build/synth/live_packages/All\n"
416 	    "Directory_portsdir= /build/synth/dports\n"
417 	    "Directory_options= /build/synth/options\n"
418 	    "Directory_distfiles= /build/synth/distfiles\n"
419 	    "Directory_buildbase= /build/synth/build\n"
420 	    "Directory_logs= /build/synth/logs\n"
421 	    "Directory_ccache= disabled\n"
422 	    "Directory_system= /\n"
423 	    "Package_suffix= .txz\n"
424 	    "Number_of_builders= 0\n"
425 	    "Max_jobs_per_builder= 0\n"
426 	    "Tmpfs_workdir= true\n"
427 	    "Tmpfs_localbase= true\n"
428 	    "Display_with_ncurses= true\n"
429 	    "leverage_prebuilt= false\n"
430 	    "\n");
431 	if (fclose(fp))
432 		dfatal_errno("Unable to write to %s\n", ConfigBase1);
433 	free(path);
434 
435 	asprintf(&path, "%s/LiveSystem-make.conf", ConfigBase1);
436 	fp = fopen(path, "w");
437 	dassert_errno(fp, "Unable to create %s", path);
438 	fprintf(fp, "%s",
439 	    "#\n"
440 	    "# Various dports options that might be of interest\n"
441 	    "#\n"
442 	    "#LICENSES_ACCEPTED=      NONE\n"
443 	    "#DISABLE_LICENSES=       yes\n"
444 	    "#DEFAULT_VERSIONS=       ssl=openssl\n"
445 	    "#FORCE_PACKAGE=          yes\n"
446 	    "#DPORTS_BUILDER=         yes\n"
447 	    "#\n"
448 	    "# Turn these on to generate debug binaries.  However, these\n"
449 	    "# options will seriously bloat memory use and storage use,\n"
450 	    "# do not use lightly\n"
451 	    "#\n"
452 	    "#STRIP=\n"
453 	    "#WITH_DEBUG=yes\n"
454 	);
455 	if (fclose(fp))
456 		dfatal_errno("Unable to write to %s\n", ConfigBase1);
457 	free(path);
458 }
459 
460 __dead2 static void
461 usage(int ecode)
462 {
463 	if (ecode == 2) {
464 		fprintf(stderr, "Run 'dsynth help' for usage\n");
465 		exit(1);
466 	}
467 
468 	fprintf(stderr,
469     "dsynth [options] directive\n"
470     "    -d                   - Debug verbosity (-dd disables ncurses)\n"
471     "    -h                   - Display this screen and exit\n"
472     "    -m gb                - Load management based on pkgdep memory\n"
473     "    -p profile           - Override profile selected in dsynth.ini\n"
474     "    -s n                 - Set initial DynamicMaxWorkers\n"
475     "    -v                   - Print version info and exit\n"
476     "    -x                   - Do not rebuild packages with dependencies\n"
477     "                           which require rebuilding\n"
478     "    -xx                  - Do not rebuild packages whos dports trees\n"
479     "                           change\n"
480     "    -y                   - Automatically answer yes to dsynth questions\n"
481     "    -D                   - Enable DEVELOPER mode\n"
482     "    -P                   - Include the check-plist stage\n"
483     "    -S                   - Disable ncurses\n"
484     "    -N                   - Do not nice-up sub-processes (else nice +10)\n"
485     "\n"
486     "    init                 - Initialize /etc/dsynth\n"
487     "    status               - Dry-run of 'upgrade-system'\n"
488     "    cleanup              - Clean-up mounts\n"
489     "    configure            - Bring up configuration menu\n"
490     "    upgrade-system       - Incremental build and upgrade using pkg list\n"
491     "                           from local system, then upgrade the local\n"
492     "                           system.\n"
493     "    prepare-system       - 'upgrade-system' but stops after building\n"
494     "    rebuild-repository   - Rebuild database files for current repository\n"
495     "    purge-distfiles      - Delete obsolete source distribution files\n"
496     "    reset-db             - Delete ports_crc.db, regenerate next build\n"
497     "    status-everything    - Dry-run of 'everything'\n"
498     "    everything           - Build entire dports tree and repo database\n"
499     "				(-D everything infers -P)\n"
500     "    version              - Print version info and exit\n"
501     "    help                 - Display this screen and exit\n"
502     "    status     [ports]   - Dry-run of 'build' with given list\n"
503     "    build      [ports]   - Incrementally build dports based on the given\n"
504     "                           list, but asks before updating the repo\n"
505     "                           database and system\n"
506     "    just-build [ports]   - 'build' but skips post-build steps\n"
507     "    install    [ports]   - 'build' but upgrades system without asking\n"
508     "    force      [ports]   - 'build' but deletes existing packages first\n"
509     "    test       [ports]   - 'build' w/DEVELOPER=yes and pre-deletes pkgs\n"
510     "				(also infers -P)\n"
511     "    debug      [ports]   - like 'test' but leaves mounts intact\n"
512     "    monitor    [datfile] - Monitor a running dsynth\n"
513     "\n"
514     "    [ports] is a space-delimited list of origins, e.g. editors/joe.  It\n"
515     "            may also be a path to a file containing one origin per line.\n"
516 	);
517 
518 	exit(ecode);
519 }
520