xref: /netbsd-src/tests/lib/libc/gen/posix_spawn/h_spawn.c (revision c0746c1e9a05705192ac7c39a7590c9a43d72db6)
1*c0746c1eSchristos /* $NetBSD: h_spawn.c,v 1.3 2021/11/07 15:46:20 christos Exp $ */
20ce98f42Smartin 
30ce98f42Smartin /*-
40ce98f42Smartin  * Copyright (c) 2012 The NetBSD Foundation, Inc.
50ce98f42Smartin  * All rights reserved.
60ce98f42Smartin  *
70ce98f42Smartin  * This code is derived from software contributed to The NetBSD Foundation
80ce98f42Smartin  * by Charles Zhang <charles@NetBSD.org> and
90ce98f42Smartin  * Martin Husemann <martin@NetBSD.org>.
100ce98f42Smartin  *
110ce98f42Smartin  * Redistribution and use in source and binary forms, with or without
120ce98f42Smartin  * modification, are permitted provided that the following conditions
130ce98f42Smartin  * are met:
140ce98f42Smartin  * 1. Redistributions of source code must retain the above copyright
150ce98f42Smartin  *    notice, this list of conditions and the following disclaimer.
160ce98f42Smartin  * 2. Redistributions in binary form must reproduce the above copyright
170ce98f42Smartin  *    notice, this list of conditions and the following disclaimer in the
180ce98f42Smartin  *    documentation and/or other materials provided with the distribution.
190ce98f42Smartin  *
200ce98f42Smartin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
210ce98f42Smartin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
220ce98f42Smartin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
230ce98f42Smartin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
240ce98f42Smartin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
250ce98f42Smartin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
260ce98f42Smartin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
270ce98f42Smartin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
280ce98f42Smartin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
290ce98f42Smartin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
300ce98f42Smartin  * POSSIBILITY OF SUCH DAMAGE.
310ce98f42Smartin  */
32*c0746c1eSchristos #include <sys/cdefs.h>
33*c0746c1eSchristos __RCSID("$NetBSD: h_spawn.c,v 1.3 2021/11/07 15:46:20 christos Exp $");
340ce98f42Smartin 
350ce98f42Smartin #include <stdio.h>
360ce98f42Smartin #include <stdlib.h>
373e0848b4Smartin #include <string.h>
383e0848b4Smartin #include <unistd.h>
390ce98f42Smartin 
400ce98f42Smartin int
main(int argc,char ** argv)410ce98f42Smartin main(int argc, char **argv)
420ce98f42Smartin {
430ce98f42Smartin 	unsigned long ret;
440ce98f42Smartin 	char *endp;
450ce98f42Smartin 
463e0848b4Smartin 	if (argc == 2 && strcmp(argv[1], "--resetids") == 0) {
473e0848b4Smartin 		if (getuid() != geteuid() || getgid() != getegid()) {
483e0848b4Smartin 			fprintf(stderr, "uid/gid do not match effective ids, "
493e0848b4Smartin 			    "uid: %d euid: %d gid: %d egid: %d\n",
503e0848b4Smartin 			    getuid(), geteuid(), getgid(), getegid());
513e0848b4Smartin 			exit(255);
523e0848b4Smartin 		}
533e0848b4Smartin 		return 0;
543e0848b4Smartin 	} else if (argc != 2) {
550ce98f42Smartin 		fprintf(stderr, "usage:\n\t%s (retcode)\n", getprogname());
560ce98f42Smartin 		exit(255);
570ce98f42Smartin 	}
580ce98f42Smartin 	ret = strtoul(argv[1], &endp, 10);
593e0848b4Smartin 	if (*endp != 0) {
603e0848b4Smartin 		fprintf(stderr,
613e0848b4Smartin 		    "invalid arg: %s\n"
623e0848b4Smartin 		    "usage:\n\t%s (retcode)\n", endp, getprogname());
633e0848b4Smartin 		exit(255);
643e0848b4Smartin 	}
650ce98f42Smartin 
660ce98f42Smartin 	fprintf(stderr, "%s exiting with status %lu\n", getprogname(), ret);
670ce98f42Smartin 	return ret;
680ce98f42Smartin }
69