]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/cloudabi/cloudabi_proc.c
Merge OpenSSL 1.0.2f.
[FreeBSD/FreeBSD.git] / sys / compat / cloudabi / cloudabi_proc.c
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 <compat/cloudabi/cloudabi_proto.h>
42 #include <compat/cloudabi/cloudabi_syscalldefs.h>
43
44 int
45 cloudabi_sys_proc_exec(struct thread *td,
46     struct cloudabi_sys_proc_exec_args *uap)
47 {
48         struct image_args args;
49         struct vmspace *oldvmspace;
50         int error;
51
52         error = pre_execve(td, &oldvmspace);
53         if (error != 0)
54                 return (error);
55         error = exec_copyin_data_fds(td, &args, uap->data, uap->datalen,
56             uap->fds, uap->fdslen);
57         if (error == 0) {
58                 args.fd = uap->fd;
59                 error = kern_execve(td, &args, NULL);
60         }
61         post_execve(td, error, oldvmspace);
62         return (error);
63 }
64
65 int
66 cloudabi_sys_proc_exit(struct thread *td,
67     struct cloudabi_sys_proc_exit_args *uap)
68 {
69
70         exit1(td, uap->rval, 0);
71         /* NOTREACHED */
72 }
73
74 int
75 cloudabi_sys_proc_fork(struct thread *td,
76     struct cloudabi_sys_proc_fork_args *uap)
77 {
78         struct filecaps fcaps = {};
79         struct proc *p2;
80         int error, fd;
81
82         cap_rights_init(&fcaps.fc_rights, CAP_FSTAT, CAP_EVENT);
83         error = fork1(td, RFFDG | RFPROC | RFPROCDESC, 0, &p2, &fd, 0, &fcaps);
84         if (error != 0)
85                 return (error);
86         /* Return the file descriptor to the parent process. */
87         td->td_retval[0] = fd;
88         return (0);
89 }
90
91 int
92 cloudabi_sys_proc_raise(struct thread *td,
93     struct cloudabi_sys_proc_raise_args *uap)
94 {
95         static const int signals[] = {
96                 [CLOUDABI_SIGABRT] = SIGABRT,
97                 [CLOUDABI_SIGALRM] = SIGALRM,
98                 [CLOUDABI_SIGBUS] = SIGBUS,
99                 [CLOUDABI_SIGCHLD] = SIGCHLD,
100                 [CLOUDABI_SIGCONT] = SIGCONT,
101                 [CLOUDABI_SIGFPE] = SIGFPE,
102                 [CLOUDABI_SIGHUP] = SIGHUP,
103                 [CLOUDABI_SIGILL] = SIGILL,
104                 [CLOUDABI_SIGINT] = SIGINT,
105                 [CLOUDABI_SIGKILL] = SIGKILL,
106                 [CLOUDABI_SIGPIPE] = SIGPIPE,
107                 [CLOUDABI_SIGQUIT] = SIGQUIT,
108                 [CLOUDABI_SIGSEGV] = SIGSEGV,
109                 [CLOUDABI_SIGSTOP] = SIGSTOP,
110                 [CLOUDABI_SIGSYS] = SIGSYS,
111                 [CLOUDABI_SIGTERM] = SIGTERM,
112                 [CLOUDABI_SIGTRAP] = SIGTRAP,
113                 [CLOUDABI_SIGTSTP] = SIGTSTP,
114                 [CLOUDABI_SIGTTIN] = SIGTTIN,
115                 [CLOUDABI_SIGTTOU] = SIGTTOU,
116                 [CLOUDABI_SIGURG] = SIGURG,
117                 [CLOUDABI_SIGUSR1] = SIGUSR1,
118                 [CLOUDABI_SIGUSR2] = SIGUSR2,
119                 [CLOUDABI_SIGVTALRM] = SIGVTALRM,
120                 [CLOUDABI_SIGXCPU] = SIGXCPU,
121                 [CLOUDABI_SIGXFSZ] = SIGXFSZ,
122         };
123         ksiginfo_t ksi;
124         struct proc *p;
125
126         if (uap->sig >= nitems(signals) || signals[uap->sig] == 0) {
127                 /* Invalid signal, or the null signal. */
128                 return (uap->sig == 0 ? 0 : EINVAL);
129         }
130
131         p = td->td_proc;
132         ksiginfo_init(&ksi);
133         ksi.ksi_signo = signals[uap->sig];
134         ksi.ksi_code = SI_USER;
135         ksi.ksi_pid = p->p_pid;
136         ksi.ksi_uid = td->td_ucred->cr_ruid;
137         PROC_LOCK(p);
138         pksignal(p, ksi.ksi_signo, &ksi);
139         PROC_UNLOCK(p);
140         return (0);
141 }
142
143 MODULE_VERSION(cloudabi, 1);