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