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