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