1 /*-
2  * Copyright (c) 2015 Nuxi, https://nuxi.nl/
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 AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE 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
19  * OR 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 __FBSDID("$FreeBSD$");
28 
29 #include <sys/param.h>
30 #include <sys/capsicum.h>
31 #include <sys/filedesc.h>
32 #include <sys/imgact.h>
33 #include <sys/lock.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
36 #include <sys/proc.h>
37 #include <sys/signalvar.h>
38 #include <sys/syscallsubr.h>
39 #include <sys/unistd.h>
40 
41 #include <contrib/cloudabi/cloudabi_types_common.h>
42 
43 #include <compat/cloudabi/cloudabi_proto.h>
44 
45 int
cloudabi_sys_proc_exec(struct thread * td,struct cloudabi_sys_proc_exec_args * uap)46 cloudabi_sys_proc_exec(struct thread *td,
47     struct cloudabi_sys_proc_exec_args *uap)
48 {
49 	struct image_args args;
50 	struct vmspace *oldvmspace;
51 	int error;
52 
53 	error = pre_execve(td, &oldvmspace);
54 	if (error != 0)
55 		return (error);
56 	error = exec_copyin_data_fds(td, &args, uap->data, uap->data_len,
57 	    uap->fds, uap->fds_len);
58 	if (error == 0) {
59 		args.fd = uap->fd;
60 		error = kern_execve(td, &args, NULL, oldvmspace);
61 	}
62 	post_execve(td, error, oldvmspace);
63 	return (error);
64 }
65 
66 int
cloudabi_sys_proc_exit(struct thread * td,struct cloudabi_sys_proc_exit_args * uap)67 cloudabi_sys_proc_exit(struct thread *td,
68     struct cloudabi_sys_proc_exit_args *uap)
69 {
70 
71 	exit1(td, uap->rval, 0);
72 	/* NOTREACHED */
73 }
74 
75 int
cloudabi_sys_proc_fork(struct thread * td,struct cloudabi_sys_proc_fork_args * uap)76 cloudabi_sys_proc_fork(struct thread *td,
77     struct cloudabi_sys_proc_fork_args *uap)
78 {
79 	struct fork_req fr;
80 	struct filecaps fcaps = {};
81 	int error, fd;
82 
83 	cap_rights_init(&fcaps.fc_rights, CAP_FSTAT, CAP_EVENT);
84 	bzero(&fr, sizeof(fr));
85 	fr.fr_flags = RFFDG | RFPROC | RFPROCDESC;
86 	fr.fr_pd_fd = &fd;
87 	fr.fr_pd_fcaps = &fcaps;
88 	error = fork1(td, &fr);
89 	if (error != 0)
90 		return (error);
91 	/* Return the file descriptor to the parent process. */
92 	td->td_retval[0] = fd;
93 	return (0);
94 }
95 
96 int
cloudabi_sys_proc_raise(struct thread * td,struct cloudabi_sys_proc_raise_args * uap)97 cloudabi_sys_proc_raise(struct thread *td,
98     struct cloudabi_sys_proc_raise_args *uap)
99 {
100 	static const int signals[] = {
101 		[CLOUDABI_SIGABRT] = SIGABRT,
102 		[CLOUDABI_SIGALRM] = SIGALRM,
103 		[CLOUDABI_SIGBUS] = SIGBUS,
104 		[CLOUDABI_SIGCHLD] = SIGCHLD,
105 		[CLOUDABI_SIGCONT] = SIGCONT,
106 		[CLOUDABI_SIGFPE] = SIGFPE,
107 		[CLOUDABI_SIGHUP] = SIGHUP,
108 		[CLOUDABI_SIGILL] = SIGILL,
109 		[CLOUDABI_SIGINT] = SIGINT,
110 		[CLOUDABI_SIGKILL] = SIGKILL,
111 		[CLOUDABI_SIGPIPE] = SIGPIPE,
112 		[CLOUDABI_SIGQUIT] = SIGQUIT,
113 		[CLOUDABI_SIGSEGV] = SIGSEGV,
114 		[CLOUDABI_SIGSTOP] = SIGSTOP,
115 		[CLOUDABI_SIGSYS] = SIGSYS,
116 		[CLOUDABI_SIGTERM] = SIGTERM,
117 		[CLOUDABI_SIGTRAP] = SIGTRAP,
118 		[CLOUDABI_SIGTSTP] = SIGTSTP,
119 		[CLOUDABI_SIGTTIN] = SIGTTIN,
120 		[CLOUDABI_SIGTTOU] = SIGTTOU,
121 		[CLOUDABI_SIGURG] = SIGURG,
122 		[CLOUDABI_SIGUSR1] = SIGUSR1,
123 		[CLOUDABI_SIGUSR2] = SIGUSR2,
124 		[CLOUDABI_SIGVTALRM] = SIGVTALRM,
125 		[CLOUDABI_SIGXCPU] = SIGXCPU,
126 		[CLOUDABI_SIGXFSZ] = SIGXFSZ,
127 	};
128 	ksiginfo_t ksi;
129 	struct proc *p;
130 
131 	if (uap->sig >= nitems(signals) || signals[uap->sig] == 0) {
132 		/* Invalid signal, or the null signal. */
133 		return (uap->sig == 0 ? 0 : EINVAL);
134 	}
135 
136 	p = td->td_proc;
137 	ksiginfo_init(&ksi);
138 	ksi.ksi_signo = signals[uap->sig];
139 	ksi.ksi_code = SI_USER;
140 	ksi.ksi_pid = p->p_pid;
141 	ksi.ksi_uid = td->td_ucred->cr_ruid;
142 	PROC_LOCK(p);
143 	pksignal(p, ksi.ksi_signo, &ksi);
144 	PROC_UNLOCK(p);
145 	return (0);
146 }
147 
148 MODULE_VERSION(cloudabi, 1);
149