1/*-
2 * Copyright (c) 2013 Antti Kantee.  All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
14 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27#include <sys/resource.h>
28#include <sys/time.h>
29
30#include <errno.h>
31#include <fcntl.h>
32#include <lwp.h>
33#include <pthread.h>
34#include <signal.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <time.h>
39#include <unistd.h>
40
41#include <rumprun-base/rumprun.h>
42
43void __dead
44_exit(int eval)
45{
46
47	if (__predict_false(rumprun_cold)) {
48		printf("\n=== bootstrap failed\n");
49		reboot(0, NULL);
50		/*NOTREACHED*/
51	}
52
53	if (eval) {
54		printf("\n=== ERROR: _exit(%d) called ===\n", eval);
55	} else {
56		printf("\n=== _exit(%d) called ===\n", eval);
57	}
58
59	pthread_exit((void *)(uintptr_t)eval);
60}
61
62/* XXX: manual proto.  plug into libc internals some other day */
63int     ____sigtimedwait50(const sigset_t * __restrict,
64    siginfo_t * __restrict, struct timespec * __restrict);
65int
66____sigtimedwait50(const sigset_t *set, siginfo_t *info,
67	struct timespec *timeout)
68{
69	int rc;
70
71	rc = _lwp_park(CLOCK_MONOTONIC, 0, timeout, 0, NULL, NULL);
72	if (rc == -1) {
73		if (errno == ETIMEDOUT)
74			errno = EAGAIN;
75	} else {
76		errno = EAGAIN;
77	}
78
79	return -1;
80}
81
82/* XXX: manual proto.  plug into libc internals some other day */
83int __getrusage50(int, struct rusage *);
84int
85__getrusage50(int who, struct rusage *usage)
86{
87
88	/* We fake something.  We should query the scheduler some day */
89	memset(usage, 0, sizeof(*usage));
90	if (who == RUSAGE_SELF) {
91		usage->ru_utime.tv_sec = 1;
92		usage->ru_utime.tv_usec = 1;
93		usage->ru_stime.tv_sec = 1;
94		usage->ru_stime.tv_usec = 1;
95	}
96
97	usage->ru_maxrss = 1024;
98	usage->ru_ixrss = 1024;
99	usage->ru_idrss = 1024;
100	usage->ru_isrss = 1024;
101
102	usage->ru_nvcsw = 1;
103	usage->ru_nivcsw = 1;
104
105	/* XXX: wrong in many ways */
106	return ENOTSUP;
107}
108