- Implemented execp*.
[planlOS.git] / system / kernel / sys / pipe.c
blob0838c54a5aee737f72c4e2755ac0d8a669afca4c
1 /*
2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #include "sys/pipe.h"
23 #include "ke/errors.h"
24 #include "fs/request.h"
25 #include "ke/debug.h"
26 #include <string.h>
27 #include <stdlib.h>
29 typedef struct
31 FsFileSystem fs;
32 } SysPipeFS;
34 typedef struct SysPipeFile
36 FsFile file;
37 struct SysPipe *pipe;
38 } SysPipeFile;
40 typedef struct SysPipe
42 SysPipeFile in;
43 SysPipeFile out;
44 char buffer[4096];
45 int buffered;
46 KeSpinlock lock;
47 KeSpinlock writelock;
48 KeSpinlock readlock;
49 } SysPipe;
52 static SysPipeFS fs;
54 static int sysPipeClose(SysPipeFile *file)
56 SysPipe *pipe = file->pipe;
57 keLockSpinlock(&pipe->lock);
58 // Only close pipe when both ends are closed
59 if (((file == &pipe->in) && (pipe->out.file.refcount != 0))
60 || ((file == &pipe->out) && (pipe->in.file.refcount != 0)))
62 keUnlockSpinlock(&pipe->lock);
63 return 0;
65 free(pipe);
66 return 0;
68 static int sysPipeRead(SysPipeFile *file, char *data, int length)
70 int origlength = length;
71 SysPipe *pipe = file->pipe;
72 // Only one thread is allowed to read from the pipe at a time
73 keLockSpinlock(&pipe->readlock);
74 // TODO: Do this in another thread to allow non-blocking writing?
75 while (length > 0)
77 keLockSpinlock(&pipe->lock);
78 int max = pipe->buffered;
79 if (max >= length)
81 memcpy(data, pipe->buffer + pipe->buffered - length, length);
82 pipe->buffered -= length;
83 keUnlockSpinlock(&pipe->lock);
84 break;
86 else
88 memcpy(data, pipe->buffer, max);
89 data += max;
90 length -= max;
91 pipe->buffered -= max;
93 keUnlockSpinlock(&pipe->lock);
94 asm volatile("int $0x32");
96 keUnlockSpinlock(&pipe->readlock);
97 return origlength;
99 static int sysPipeWrite(SysPipeFile *file, char *data, int length)
101 int origlength = length;
102 SysPipe *pipe = file->pipe;
103 // Only one thread is allowed to write to the pipe at a time
104 keLockSpinlock(&pipe->writelock);
105 // Continue writing until everything was written
106 // TODO: Do this in another thread to allow non-blocking writing?
107 while (length > 0)
109 keLockSpinlock(&pipe->lock);
110 int max = 4096 - pipe->buffered;
111 if (max >= length)
113 memcpy(pipe->buffer + pipe->buffered, data, length);
114 pipe->buffered += length;
115 keUnlockSpinlock(&pipe->lock);
116 break;
118 else
120 memcpy(pipe->buffer + pipe->buffered, data, max);
121 data += max;
122 length -= max;
123 pipe->buffered += max;
125 keUnlockSpinlock(&pipe->lock);
126 asm volatile("int $0x32");
128 keUnlockSpinlock(&pipe->writelock);
129 return origlength;
132 static int sysPipeRequest(struct FsFileSystem *fs, struct FsRequest *request)
134 if (!request->file) return KE_ERROR_UNKNOWN;
135 SysPipeFile *file = (SysPipeFile*)request->file;
136 switch (request->type)
138 case FS_REQUEST_CLOSE:
139 request->return_value = sysPipeClose(file);
140 fsFinishRequest(request);
141 return 0;
142 case FS_REQUEST_READ:
143 request->return_value = sysPipeRead(file, request->buffer, request->bufferlength);
144 fsFinishRequest(request);
145 return 0;
146 case FS_REQUEST_WRITE:
147 request->return_value = sysPipeWrite(file, request->buffer, request->bufferlength);
148 fsFinishRequest(request);
149 return 0;
150 default:
151 return KE_ERROR_UNKNOWN;
155 void sysInitPipe(void)
157 memset(&fs, 0, sizeof(fs));
158 fs.fs.query_request = sysPipeRequest;
161 int sysOpenPipe(FsFileHandle *in, FsFileHandle *out)
163 // Create pipe
164 SysPipe *pipe = malloc(sizeof(SysPipe));
165 memset(pipe, 0, sizeof(SysPipe));
166 pipe->in.pipe = pipe;
167 pipe->in.file.fs = &fs.fs;
168 pipe->in.file.refcount = 1;
169 pipe->out.pipe = pipe;
170 pipe->out.file.fs = &fs.fs;
171 pipe->out.file.refcount = 1;
172 // Create file handles
173 in->file = &pipe->in.file;
174 out->file = &pipe->out.file;
175 return 0;