UbixOS V2 2.0

descrip.c
Go to the documentation of this file.
1 /*-
2 * Copyright (c) 2002-2018 The UbixOS Project.
3 * All rights reserved.
4 *
5 * This was developed by Christopher W. Olsen for the UbixOS Project.
6 *
7 * Redistribution and use in source and binary forms, with or without modification, are permitted
8 * provided that the following conditions are met:
9 *
10 * 1) Redistributions of source code must retain the above copyright notice, this list of
11 * conditions, the following disclaimer and the list of authors.
12 * 2) Redistributions in binary form must reproduce the above copyright notice, this list of
13 * conditions, the following disclaimer and the list of authors in the documentation and/or
14 * other materials provided with the distribution.
15 * 3) Neither the name of the UbixOS Project nor the names of its contributors may be used to
16 * endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/descrip.h>
30 #include <sys/sysproto_posix.h>
31 #include <sys/thread.h>
32 #include <lib/kprintf.h>
33 #include <ubixos/endtask.h>
34 #include <lib/kmalloc.h>
35 #include <assert.h>
36 #include <sys/select.h>
37
38 static struct file *kern_files = 0x0;
39
40 int fcntl(struct thread *td, struct sys_fcntl_args *uap) {
41 struct file *fp = 0x0;
42 struct file *dup_fp = 0x0;
43 int i = 0;
44
45 if (td->o_files[uap->fd] == 0x0) {
46 kprintf("ERROR!!!\n");
47 return (-1);
48 }
49
50 fp = (struct file*) td->o_files[uap->fd];
51
52 switch (uap->cmd) {
53 case 17:
54 /* First 5 Descriptors Are Reserved */
55 for (i = 5; i < MAX_FILES; i++) {
56 if (td->o_files[i] == 0x0) {
57 dup_fp = (struct file*) kmalloc(sizeof(struct file));
58 memcpy(dup_fp, fp, sizeof(struct file));
59
60 td->o_files[i] = (void*) dup_fp;
61 td->td_retval[0] = i;
62
63 ((struct file*) td->o_files[uap->fd])->fd->dup++;
64
65 fclose(((struct file*) td->o_files[uap->fd])->fd);
66
67 //td->o_files[uap->fd] = 0;
68
69 if (!fdestroy(td, fp, uap->fd))
70 kprintf("[%s:%i] fdestroy(0x%X, 0x%X) failed\n", __FILE__, __LINE__, fp, td->o_files[uap->fd]);
71
72 kprintf("FCNTL: %i, %i, 0x%X.", i, uap->fd, fp);
73
74 break;
75 }
76 }
77 break;
78 case 3:
79 td->td_retval[0] = fp->f_flag;
80 break;
81 case 4:
82 fp->f_flag &= ~FCNTLFLAGS;
83 fp->f_flag |= FFLAGS(uap->arg & ~O_ACCMODE) & FCNTLFLAGS;
84 break;
85 default:
86 kprintf("ERROR DEFAULT: [%i]", uap->fd);
87 }
88
89 return (0x0);
90 }
91
92 int sys_fcntl(struct thread *td, struct sys_fcntl_args *uap) {
93 return (fcntl(td, uap));
94 }
95
96 int falloc(struct thread *td, struct file **resultfp, int *resultfd) {
97
98 struct file *fp = 0x0;
99 int i = 0;
100
101 fp = (struct file*) kmalloc(sizeof(struct file));
102
103 /* First 5 Descriptors Are Reserved */
104 for (i = 5; i < MAX_FILES; i++) {
105 if (td->o_files[i] == 0x0) {
106 td->o_files[i] = (void*) fp;
107 if (resultfd)
108 *resultfd = i;
109 if (resultfp)
110 *resultfp = fp;
111 goto allocated;
112 }
113 }
114
115 kfree(fp);
116
117 *resultfp = 0x0;
118 *resultfd = 0x0;
119
120 allocated:
121
122 return (0x0);
123 }
124
125 #include <sys/ioctl.h>
126
147 int fdestroy(struct thread *td, struct file *fp, int fd) {
148 int error = 0;
149
150 if (td->o_files[fd] != fp) {
151 error = -1;
152 }
153 else {
154 kfree(td->o_files[fd]);
155 td->o_files[fd] = 0x0;
156 }
157
158 kprintf("ERR: %i", error);
159 return (error);
160 }
161
162 int close(struct thread *td, struct close_args *uap) {
163 #ifdef DEBUG
164 kprintf("[%s:%i]",__FILE__,__LINE__);
165 #endif
166 kprintf("[%s:%i]", __FILE__, __LINE__);
167 kfree((void*) td->o_files[uap->fd]);
168 td->o_files[uap->fd] = 0x0;
169 td->td_retval[0] = 0x0;
170 return (0x0);
171 }
172
176 int getdtablesize(struct thread *td, struct getdtablesize_args *uap) {
177 #ifdef DEBUG
178 kprintf("[%s:%i]",__FILE__,__LINE__);
179 #endif
180 td->td_retval[0] = O_FILES;
181 return (0);
182 }
183
184 /* HACK */
185 int fstat(struct thread *td, struct sys_fstat_args *uap) {
186 struct file *fp = 0x0;
187
188 #ifdef DEBUG
189 kprintf("[%s:%i]",__FILE__,__LINE__);
190 #endif
191
192 fp = (struct file*) _current->td.o_files[uap->fd];
193 uap->sb->st_mode = 0x2180;
194 uap->sb->st_blksize = 0x1000;
195 kprintf("fstat: %i", uap->fd);
196 return (0x0);
197 }
198
204 int ioctl(struct thread *td, struct ioctl_args *uap) {
205 td->td_retval[0] = 0x0;
206 return (0x0);
207 }
208
214 int getfd(struct thread *td, struct file **fp, int fd) {
215 int error = 0x0;
216
217 #ifdef DEBUG
218 kprintf("[%s:%i]",__FILE__,__LINE__);
219 #endif
220
221 *fp = (struct file*) td->o_files[fd];
222
223 if (fp == 0x0)
224 error = -1;
225
226 return (error);
227 }
228
229 int sys_ioctl(struct thread *td, struct sys_ioctl_args *args) {
230 switch (args->com) {
231 case TIOCGETA:
232 if (args->fd == 0 || args->fd == 1) {
233 struct termios *t = (struct termios*) args->data;
234
235 t->c_iflag = 0x2B02;
236 t->c_oflag = 0x3;
237 t->c_cflag = 0x4B00;
238 t->c_lflag = 0x5CB;
239
240 t->c_cc[0] = 4;
241 t->c_cc[1] = 255;
242 t->c_cc[2] = 255;
243 t->c_cc[3] = 127;
244 t->c_cc[4] = 23;
245 t->c_cc[5] = 21;
246 t->c_cc[6] = 18;
247 t->c_cc[7] = 8;
248 t->c_cc[8] = 3;
249 t->c_cc[9] = 28;
250 t->c_cc[10] = 26;
251 t->c_cc[11] = 25;
252 t->c_cc[12] = 17;
253 t->c_cc[13] = 19;
254 t->c_cc[14] = 22;
255 t->c_cc[15] = 15;
256 t->c_cc[16] = 1;
257 t->c_cc[17] = 0;
258 t->c_cc[18] = 20;
259 t->c_cc[19] = 255;
260
261 t->c_ispeed = 0x9600;
262 t->c_ospeed = 0x9600;
263
264 td->td_retval[0] = 0;
265 }
266 else {
267 td->td_retval[0] = -1;
268 }
269 break;
270 case TIOCGWINSZ:
271 asm("nop");
272 struct winsize *win = (struct winsize*) args->data;
273 win->ws_row = 50;
274 win->ws_col = 80;
275 break;
276 default:
277 kprintf("ioFD:%i:0x%X!", args->fd, args->com);
278 break;
279 }
280
281 td->td_retval[0] = 0x0;
282 return (0x0);
283 }
284
285 int sys_select(struct thread *td, struct sys_select_args *args) {
286 int error = 0x0;
287 /*
288 int i = 0x0;
289
290 fd_set sock_rfds;
291 fd_set sock_wfds;
292 fd_set sock_efds;
293
294 FD_ZERO(&sock_rfds);
295 FD_ZERO(&sock_wfds);
296 FD_ZERO(&sock_efds);
297
298 if (args->in != 0x0) {
299 for (i = 0; i < args->nd; i++) {
300 FD_SET(((struct file * ) td->o_files[args->in[0]]).socket, &sock_rfds);
301 }
302 }
303
304 if (args->ou != 0x0) {
305 for (i = 0; i < args->nd; i++) {
306 FD_SET(((struct file * ) td->o_files[args->ou[0]]).socket, &sock_wfds);
307 }
308 }
309
310 if (args->ex != 0x0) {
311 for (i = 0; i < args->nd; i++) {
312 FD_SET(((struct file * ) td->o_files[args->ex[0]]).socket, &sock_efds);
313 }
314 }
315
316 if ((td->td_retval[0] = lwip_select(args->nd, &sock_rfds, &sock_wfds, &sock_efds, args->tv)) == -1)
317 error = -1;
318
319 if (args->in != 0x0) {
320 for (i = 0; i < MAX_FILES; i++) {
321 if (!FD_ISSET(((struct file * ) td->o_files[args->ou[0]]).socket, &sock_rfds))
322 FD_CLR(((struct file * ) td->o_files[args->ou[0]]).socket, args->in);
323 }
324 }
325
326 if (args->ou != 0x0) {
327 for (i = 0; i < MAX_FILES; i++) {
328 if (!FD_ISSET(((struct file * ) td->o_files[args->ou[0]]).socket, &sock_wfds))
329 FD_CLR(((struct file * ) td->o_files[args->ou[0]]).socket, args->ou);
330 }
331 }
332
333 if (args->ex != 0x0) {
334 for (i = 0; i < MAX_FILES; i++) {
335 if (!FD_ISSET(((struct file * ) td->o_files[args->ou[0]]).socket, &sock_efds))
336 FD_CLR(((struct file * ) td->o_files[args->ou[0]]).socket, args->ex);
337 }
338 }
339
340 */
341
342 if ((td->td_retval[0] = lwip_select(args->nd, args->in, args->ou, args->ex, args->tv)) == -1)
343 error = -1;
344
345 return (error);
346 }
347
348 int dup2(struct thread *td, u_int32_t from, u_int32_t to) {
349 struct file *fp = 0x0;
350 struct file *dup_fp = 0x0;
351
352 if (to > MAX_FILES) {
353 kprintf("TO: %i > MAX_FILES: %i", to, MAX_FILES);
354 return (-1);
355 }
356 else if (td->o_files[to] != 0x0) {
357
358 fclose(((struct file*) td->o_files[to])->fd);
359 fdestroy(td, (struct file*) td->o_files[to], to);
360 }
361
362 fp = (struct file*) td->o_files[from];
363 dup_fp = (struct file*) kmalloc(sizeof(struct file));
364
365 memcpy(dup_fp, fp, sizeof(struct file));
366
367 kprintf("DUP2.0: %i:%i [0x%X:0x%X]", from, to, td->o_files[from], td->o_files[to]);
368
369 td->o_files[to] = (void*) dup_fp;
370
371 ((struct file*) td->o_files[from])->fd->dup++;
372
373 kprintf("DUP2.1: %i:%i <%i> [0x%X:0x%X]", from, to, ((struct file*) td->o_files[from])->fd->dup, td->o_files[from], td->o_files[to]);
374 return (0x0);
375 }
376
377 int sys_dup2(struct thread *td, struct sys_dup2_args *args) {
378
379 int error = 0x0;
380
381 if ((td->td_retval[0] = dup2(td, args->from, args->to)) == -1)
382 error = -1;
383
384 return (error);
385 }
sys_fcntl_args::arg
long arg
Definition: sysproto_posix.h:260
fileDescriptor::dup
int dup
Definition: file.h:83
getfd
int getfd(struct thread *td, struct file **fp, int fd)
get pointer to file fd in specified thread
Definition: descrip.c:214
sys_fstat_args
Definition: sysproto_posix.h:467
TIOCGETA
#define TIOCGETA
Definition: ioctl.h:53
sys_ioctl_args::fd
int fd
Definition: sysproto_posix.h:529
sys_ioctl_args::com
u_long com
Definition: sysproto_posix.h:532
sys_fcntl_args::fd
int fd
Definition: sysproto_posix.h:254
termios::c_ispeed
speed_t c_ispeed
Definition: ioctl.h:42
close_args::fd
int fd
Definition: sysproto_posix.h:374
sys_fcntl_args::cmd
int cmd
Definition: sysproto_posix.h:257
winsize::ws_col
unsigned short ws_col
Definition: ioctl.h:48
sys_select
int sys_select(struct thread *td, struct sys_select_args *args)
Definition: descrip.c:285
winsize
Definition: ioctl.h:46
thread::o_files
void * o_files[512]
Definition: thread.h:42
dup2
int dup2(struct thread *td, u_int32_t from, u_int32_t to)
Definition: descrip.c:348
termios::c_ospeed
speed_t c_ospeed
Definition: ioctl.h:43
sys_fcntl
int sys_fcntl(struct thread *td, struct sys_fcntl_args *uap)
Definition: descrip.c:92
file::f_flag
uint32_t f_flag
Definition: descrip.h:68
sys_select_args::nd
int nd
Definition: sysproto_posix.h:673
sys_select_args::ou
fd_set * ou
Definition: sysproto_posix.h:679
sysproto_posix.h
close_args
Definition: sysproto_posix.h:372
sys_dup2
int sys_dup2(struct thread *td, struct sys_dup2_args *args)
Definition: descrip.c:377
file
Definition: descrip.h:67
kfree
void kfree(void *baseAddr)
Definition: kmalloc.c:342
thread
Definition: thread.h:40
stat::st_mode
__mode_t st_mode
Definition: stat.h:47
sys_fcntl_args
Definition: sysproto_posix.h:252
assert.h
O_FILES
#define O_FILES
Definition: thread.h:38
sys_select_args::ex
fd_set * ex
Definition: sysproto_posix.h:682
close
int close(struct thread *td, struct close_args *uap)
Definition: descrip.c:162
endtask.h
fdestroy
int fdestroy(struct thread *td, struct file *fp, int fd)
The function bar.
Definition: descrip.c:147
O_ACCMODE
#define O_ACCMODE
Definition: fcntl.h:64
termios
Definition: ioctl.h:36
memcpy
void * memcpy(const void *dst, const void *src, size_t length)
taskStruct::td
struct thread td
Definition: sched.h:78
ioctl_args
Definition: sysproto_posix.h:475
file::fd
fileDescriptor_t * fd
Definition: descrip.h:71
thread::td_retval
int td_retval[2]
Definition: thread.h:41
fclose
int fclose(fileDescriptor_t *fd)
Definition: file.c:526
kprintf.h
winsize::ws_row
unsigned short ws_row
Definition: ioctl.h:47
getdtablesize
int getdtablesize(struct thread *td, struct getdtablesize_args *uap)
return data table size
Definition: descrip.c:176
fcntl
int fcntl(struct thread *td, struct sys_fcntl_args *uap)
Definition: descrip.c:40
fstat
int fstat(struct thread *td, struct sys_fstat_args *uap)
Definition: descrip.c:185
sys_ioctl_args::data
caddr_t data
Definition: sysproto_posix.h:535
sys_fstat_args::sb
struct stat * sb
Definition: sysproto_posix.h:472
sys_select_args
Definition: sysproto_posix.h:671
sys_dup2_args::from
u_int from
Definition: sysproto_posix.h:812
sys_fstat_args::fd
int fd
Definition: sysproto_posix.h:469
termios::c_cc
cc_t c_cc[20]
Definition: ioctl.h:41
_current
kTask_t * _current
Definition: sched.c:50
FCNTLFLAGS
#define FCNTLFLAGS
Definition: fcntl.h:80
getdtablesize_args
Definition: sysproto_posix.h:435
termios::c_iflag
tcflag_t c_iflag
Definition: ioctl.h:37
ioctl.h
TIOCGWINSZ
#define TIOCGWINSZ
Definition: ioctl.h:54
sys_select_args::in
fd_set * in
Definition: sysproto_posix.h:676
falloc
int falloc(struct thread *td, struct file **resultfp, int *resultfd)
Definition: descrip.c:96
kmalloc
void * kmalloc(uInt32 len)
Definition: kmalloc.c:241
descrip.h
sys_dup2_args
Definition: sysproto_posix.h:810
select.h
sys_dup2_args::to
u_int to
Definition: sysproto_posix.h:815
sys_select_args::tv
struct timeval * tv
Definition: sysproto_posix.h:685
ioctl
int ioctl(struct thread *td, struct ioctl_args *uap)
ioctl functionality not implimented yet
Definition: descrip.c:204
termios::c_oflag
tcflag_t c_oflag
Definition: ioctl.h:38
termios::c_cflag
tcflag_t c_cflag
Definition: ioctl.h:39
sys_ioctl
int sys_ioctl(struct thread *td, struct sys_ioctl_args *args)
Definition: descrip.c:229
MAX_FILES
#define MAX_FILES
Definition: descrip.h:42
kprintf
int kprintf(const char *,...)
Definition: kprintf.c:259
stat::st_blksize
blksize_t st_blksize
Definition: stat.h:60
FFLAGS
#define FFLAGS(oflags)
Definition: fcntl.h:88
u_int32_t
__uint32_t u_int32_t
Definition: types.h:53
thread.h
sys_ioctl_args
Definition: sysproto_posix.h:527
kmalloc.h
termios::c_lflag
tcflag_t c_lflag
Definition: ioctl.h:40