Tried to fix whole execution system (Shell, Unnamed Pipes)
[meinos.git] / apps / lib / libmeinos / execute.c
blob007032c0831e861363428b842ce218f5f0246837
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 /**
26 * Creates a new process from executable file
27 * @param path Path to executable
28 * @param argv Argument vector
29 * @param _stdin Reference for filehandle for stdin of new process
30 * @param _stdout Reference for filehandle for stdout of new process
31 * @param _stderr Reference for filehandle for stderr of new process
32 * @return PID of new process
34 #include <stdio.h>
35 pid_t execute(const char *path,char *argv[],const char *_stdin,const char *_stdout,const char *_stderr) {
36 size_t shmsize = sizeof(struct process_data);
37 int shmid;
38 struct process_data *data;
39 size_t i;
41 if (argv!=NULL) {
42 for (i=0;argv[i];i++) shmsize += strlen(argv[i])+1;
44 if (_stdin!=NULL) shmsize += strlen(_stdin);
45 if (_stdout!=NULL) shmsize += strlen(_stdout);
46 if (_stderr!=NULL) shmsize += strlen(_stderr);
47 shmid = shmget(IPC_PRIVATE,shmsize,0);
49 if (shmid!=-1) {
50 data = shmat(shmid,NULL,0);
52 if (data!=NULL) {
53 size_t j = 0;
54 if (argv!=NULL) {
55 for (i=0;argv[i];i++) {
56 strcpy(data->cmdline+j,argv[i]);
57 j += strlen(data->cmdline+j)+1;
59 data->argc = i;
61 else data->argc = 0;
63 data->has_stdin = (_stdin!=NULL);
64 if (_stdin!=NULL) {
65 strcpy(data->stdio+j,_stdin);
66 j += strlen(data->stdio+j)+1;
69 data->has_stdout = (_stdout!=NULL);
70 if (_stdout!=NULL) {
71 strcpy(data->stdio+j,_stdout);
72 j += strlen(data->stdio+j)+1;
75 data->has_stderr = (_stderr!=NULL);
76 if (_stderr!=NULL) {
77 strcpy(data->stdio+j,_stderr);
78 j += strlen(data->stdio+j)+1;
81 shmdt(data);
83 return rpc_call("proc_execute",0,path,shmid);
86 return -1;