+execute(); +unnamed pipes (stdin,stdout,stderr); +argc/argv
[meinos.git] / apps / lib / libmeinos / execute.c
blobbe0edd7f76089ca20b0c241688326ff5b9ce79ae
1 /*
2 meinOS - A unix-like x86 microkernel operating system
3 Copyright (C) 2008 Janosch Gräf <janosch.graef@gmx.net>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <sys/types.h>
20 #include <sys/shm.h>
21 #include <rpc.h>
22 #include <misc.h>
23 #include <string.h>
25 int _create_unnamed_pipe(int read,int *_shmid);
27 /**
28 * Creates a new process from executable file
29 * @param path Path to executable
30 * @param argv Argument vector
31 * @param stdin Reference for filehandle for stdin of new process
32 * @param stdout Reference for filehandle for stdout of new process
33 * @param stderr Reference for filehandle for stderr of new process
34 * @return PID of new process
36 pid_t execute(const char *path,char *argv[],int *_stdin,int *_stdout,int *_stderr) {
37 size_t shmsize = sizeof(struct process_data)+1;
38 int shmid;
39 struct process_data *data;
40 size_t i;
42 if (argv!=NULL) {
43 for (i=0;argv[i];i++) shmsize += strlen(argv[i])+1;
45 shmid = shmget(IPC_PRIVATE,shmsize,0);
47 if (shmid!=-1) {
48 data = shmat(shmid,NULL,0);
49 if (data!=NULL) {
50 int stdin = _create_unnamed_pipe(0,&(data->shmid_stdin));
51 int stdout = _create_unnamed_pipe(1,&(data->shmid_stdout));
52 int stderr = _create_unnamed_pipe(1,&(data->shmid_stderr));
53 if (_stdin!=NULL) *_stdin = stdin;
54 if (_stdout!=NULL) *_stdout = stdout;
55 if (_stderr!=NULL) *_stderr = stderr;
57 size_t j = 0;
58 if (argv!=NULL) {
60 for (i=0;argv[i];i++) {
61 strcpy(data->cmdline+j,argv[i]);
62 j += strlen(data->cmdline+j)+1;
65 data->cmdline[j] = 0;
67 shmdt(data);
69 return rpc_call("proc_execute",0,path,shmid);
72 return -1;