xref: /dragonfly/usr.bin/dsynth/dsynth.c (revision 207ba670)
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 		DeleteObsoletePkgs = 1;
301 		DoCleanBuild(1);
302 		OptimizeEnv();
303 		pkgs = GetFullPackageList();
304 		DoBuild(pkgs);
305 		DoRebuildRepo(1);
306 	} else if (strcmp(av[0], "build") == 0) {
307 		DoCleanBuild(1);
308 		OptimizeEnv();
309 		pkgs = ParsePackageList(ac - 1, av + 1, 0);
310 		DoBuild(pkgs);
311 		DoRebuildRepo(1);
312 		DoUpgradePkgs(pkgs, 1);
313 	} else if (strcmp(av[0], "just-build") == 0) {
314 		DoCleanBuild(1);
315 		OptimizeEnv();
316 		pkgs = ParsePackageList(ac - 1, av + 1, 0);
317 		DoBuild(pkgs);
318 	} else if (strcmp(av[0], "install") == 0) {
319 		DoCleanBuild(1);
320 		OptimizeEnv();
321 		pkgs = ParsePackageList(ac - 1, av + 1, 0);
322 		DoBuild(pkgs);
323 		DoRebuildRepo(0);
324 		DoUpgradePkgs(pkgs, 0);
325 	} else if (strcmp(av[0], "force") == 0) {
326 		DoCleanBuild(1);
327 		OptimizeEnv();
328 		pkgs = ParsePackageList(ac - 1, av + 1, 0);
329 		RemovePackages(pkgs);
330 		DoBuild(pkgs);
331 		DoRebuildRepo(1);
332 		DoUpgradePkgs(pkgs, 1);
333 	} else if (strcmp(av[0], "test") == 0) {
334 		WorkerProcFlags |= WORKER_PROC_CHECK_PLIST;
335 		DoCleanBuild(1);
336 		OptimizeEnv();
337 		pkgs = ParsePackageList(ac - 1, av + 1, 0);
338 		RemovePackages(pkgs);
339 		WorkerProcFlags |= WORKER_PROC_DEVELOPER;
340 		DoBuild(pkgs);
341 	} else {
342 		fprintf(stderr, "Unknown directive '%s'\n", av[0]);
343 		usage(2);
344 	}
345 	return 0;
346 }
347 
348 static void
349 DoInit(void)
350 {
351 	struct stat st;
352 	char *path;
353 	FILE *fp;
354 
355 	if (stat(ConfigBase1, &st) == 0) {
356 		dfatal("init will not overwrite %s", ConfigBase1);
357 	}
358 	if (stat(ConfigBase2, &st) == 0) {
359 		dfatal("init will not create %s if %s exists",
360 		       ConfigBase2, ConfigBase1);
361 	}
362 	if (mkdir(ConfigBase1, 0755) < 0)
363 		dfatal_errno("Unable to mkdir %s", ConfigBase1);
364 
365 	asprintf(&path, "%s/dsynth.ini", ConfigBase1);
366 	fp = fopen(path, "w");
367 	dassert_errno(fp, "Unable to create %s", path);
368 	fprintf(fp, "%s",
369 	    "; This Synth configuration file is automatically generated\n"
370 	    "; Take care when hand editing!\n"
371 	    "\n"
372 	    "[Global Configuration]\n"
373 	    "profile_selected= LiveSystem\n"
374 	    "\n"
375 	    "[LiveSystem]\n"
376 	    "Operating_system= DragonFly\n"
377 	    "Directory_packages= /build/synth/live_packages\n"
378 	    "Directory_repository= /build/synth/live_packages/All\n"
379 	    "Directory_portsdir= /build/synth/dports\n"
380 	    "Directory_options= /build/synth/options\n"
381 	    "Directory_distfiles= /build/synth/distfiles\n"
382 	    "Directory_buildbase= /build/synth/build\n"
383 	    "Directory_logs= /build/synth/logs\n"
384 	    "Directory_ccache= disabled\n"
385 	    "Directory_system= /\n"
386 	    "Package_suffix= .txz\n"
387 	    "Number_of_builders= 0\n"
388 	    "Max_jobs_per_builder= 0\n"
389 	    "Tmpfs_workdir= true\n"
390 	    "Tmpfs_localbase= true\n"
391 	    "Display_with_ncurses= true\n"
392 	    "leverage_prebuilt= false\n"
393 	    "\n");
394 	if (fclose(fp))
395 		dfatal_errno("Unable to write to %s\n", ConfigBase1);
396 	free(path);
397 
398 	asprintf(&path, "%s/LiveSystem-make.conf", ConfigBase1);
399 	fp = fopen(path, "w");
400 	dassert_errno(fp, "Unable to create %s", path);
401 	fprintf(fp, "%s",
402 	    "#\n"
403 	    "# Various dports options that might be of interest\n"
404 	    "#\n"
405 	    "#LICENSES_ACCEPTED=      NONE\n"
406 	    "#DISABLE_LICENSES=       yes\n"
407 	    "#DEFAULT_VERSIONS=       ssl=openssl\n"
408 	    "#FORCE_PACKAGE=          yes\n"
409 	    "#DPORTS_BUILDER=         yes\n"
410 	    "#\n"
411 	    "# Turn these on to generate debug binaries.  However, these\n"
412 	    "# options will seriously bloat memory use and storage use,\n"
413 	    "# do not use lightly\n"
414 	    "#\n"
415 	    "#STRIP=\n"
416 	    "#WITH_DEBUG=yes\n"
417 	);
418 	if (fclose(fp))
419 		dfatal_errno("Unable to write to %s\n", ConfigBase1);
420 	free(path);
421 }
422 
423 __dead2 static void
424 usage(int ecode)
425 {
426 	if (ecode == 2) {
427 		fprintf(stderr, "Run 'dsynth help' for usage\n");
428 		exit(1);
429 	}
430 
431 	fprintf(stderr,
432     "dsynth [options] directive\n"
433     "    -d                   - Debug verbosity (-dd disables ncurses)\n"
434     "    -h                   - Display this screen and exit\n"
435     "    -m gb                - Load management based on pkgdep memory\n"
436     "    -p profile           - Override profile selected in dsynth.ini\n"
437     "    -s n                 - Set initial DynamicMaxWorkers\n"
438     "    -v                   - Print version info and exit\n"
439     "    -y                   - Automatically answer yes to dsynth questions\n"
440     "    -D                   - Enable DEVELOPER mode\n"
441     "    -P                   - Include the check-plist stage\n"
442     "    -S                   - Disable ncurses\n"
443     "\n"
444     "    init                 - Initialize /etc/dsynth\n"
445     "    status               - Dry-run of 'upgrade-system'\n"
446     "    cleanup              - Clean-up mounts\n"
447     "    configure            - Bring up configuration menu\n"
448     "    upgrade-system       - Incremental build and upgrade using pkg list\n"
449     "                           from local system, then upgrade the local\n"
450     "                           system.\n"
451     "    prepare-system       - 'upgrade-system' but stops after building\n"
452     "    rebuild-repository   - Rebuild database files for current repository\n"
453     "    purge-distfiles      - Delete obsolete source distribution files\n"
454     "    status-everything    - Dry-run of 'everything'\n"
455     "    everything           - Build entire dports tree and repo database\n"
456     "				(-D everything infers -P)\n"
457     "    version              - Print version info and exit\n"
458     "    help                 - Display this screen and exit\n"
459     "    status     [ports]   - Dry-run of 'build' with given list\n"
460     "    build      [ports]   - Incrementally build dports based on the given\n"
461     "                           list, but asks before updating the repo\n"
462     "                           database and system\n"
463     "    just-build [ports]   - 'build' but skips post-build steps\n"
464     "    install    [ports]   - 'build' but upgrades system without asking\n"
465     "    force      [ports]   - 'build' but deletes existing packages first\n"
466     "    test       [ports]   - 'build' w/DEVELOPER=yes and pre-deletes pkgs\n"
467     "				(also infers -P)\n"
468     "    debug      [ports]   - like 'test' but leaves mounts intact\n"
469     "    monitor    [datfile] - Monitor a running dsynth\n"
470     "\n"
471     "    [ports] is a space-delimited list of origins, e.g. editors/joe.  It\n"
472     "            may also be a path to a file containing one origin per line.\n"
473 	);
474 
475 	exit(ecode);
476 }
477