UbixOS  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 
31 #include <sys/sysproto_posix.h>
32 #include <sys/thread.h>
33 #include <lib/kprintf.h>
34 #include <ubixos/endtask.h>
35 #include <lib/kmalloc.h>
36 #include <assert.h>
37 #include <sys/select.h>
38 
39 // XXX MrOlsen (2020-01-30) No longer needed -> static struct file *kern_files = 0x0;
40 
41 int fcntl(struct thread *td, struct sys_fcntl_args *uap) {
42  struct file *fp = 0x0;
43  struct file *dup_fp = 0x0;
44  int i = 0;
45 
46  if (td->o_files[uap->fd] == 0x0) {
47  kprintf("ERROR!!!\n");
48  return (-1);
49  }
50 
51  fp = (struct file*) td->o_files[uap->fd];
52 
53  switch (uap->cmd) {
54  case 17:
55  /* First 5 Descriptors Are Reserved */
56  for (i = 5; i < MAX_FILES; i++) {
57  if (td->o_files[i] == 0x0) {
58  dup_fp = (struct file*) kmalloc(sizeof(struct file));
59  memcpy(dup_fp, fp, sizeof(struct file));
60 
61  td->o_files[i] = (void*) dup_fp;
62  td->td_retval[0] = i;
63 
64  ((struct file*) td->o_files[uap->fd])->fd->dup++;
65 
66  fclose(((struct file*) td->o_files[uap->fd])->fd);
67 
68  //td->o_files[uap->fd] = 0;
69 
70  if (fdestroy(td, fp, uap->fd) != 0x0)
71  kprintf("[%s:%i] fdestroy(0x%X, 0x%X) failed\n", __FILE__, __LINE__, fp, td->o_files[uap->fd]);
72 
73  kprintf("FCNTL: %i, %i, 0x%X.", i, uap->fd, fp);
74 
75  break;
76  }
77  }
78  break;
79  case 3:
80  td->td_retval[0] = fp->f_flag;
81  break;
82  case 4:
83  fp->f_flag &= ~FCNTLFLAGS;
84  fp->f_flag |= FFLAGS(uap->arg & ~O_ACCMODE) & FCNTLFLAGS;
85  break;
86  default:
87  kprintf("ERROR DEFAULT: [%i]", uap->fd);
88  }
89 
90  return (0x0);
91 }
92 
93 int sys_fcntl(struct thread *td, struct sys_fcntl_args *uap) {
94  return (fcntl(td, uap));
95 }
96 
97 int falloc(struct thread *td, struct file **resultfp, int *resultfd) {
98 
99  struct file *fp = 0x0;
100  int i = 0;
101 
102  fp = (struct file*) kmalloc(sizeof(struct file));
103 
104  /* First 5 Descriptors Are Reserved */
105  for (i = 5; i < MAX_FILES; i++) {
106  if (td->o_files[i] == 0x0) {
107  td->o_files[i] = (void*) fp;
108  if (resultfd)
109  *resultfd = i;
110  if (resultfp)
111  *resultfp = fp;
112  goto allocated;
113  }
114  }
115 
116  kfree(fp);
117 
118  *resultfp = 0x0;
119  *resultfd = 0x0;
120 
121  allocated:
122 
123  return (0x0);
124 }
125 
126 #include <sys/ioctl.h>
127 
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  return (error);
159 }
160 
161 int close(struct thread *td, struct close_args *uap) {
162 #ifdef DEBUG
163  kprintf("[%s:%i]",__FILE__,__LINE__);
164 #endif
165  kprintf("[%s:%i]", __FILE__, __LINE__);
166  kfree((void*) td->o_files[uap->fd]);
167  td->o_files[uap->fd] = 0x0;
168  td->td_retval[0] = 0x0;
169  return (0x0);
170 }
171 
175 int getdtablesize(struct thread *td, struct getdtablesize_args *uap) {
176 #ifdef DEBUG
177  kprintf("[%s:%i]",__FILE__,__LINE__);
178 #endif
179  td->td_retval[0] = O_FILES;
180  return (0);
181 }
182 
183 /* HACK */
184 int fstat(struct thread *td, struct sys_fstat_args *uap) {
185  struct file *fp = 0x0;
186 
187 #ifdef DEBUG
188  kprintf("[%s:%i]",__FILE__,__LINE__);
189 #endif
190 
191  fp = (struct file*) _current->td.o_files[uap->fd];
192  uap->sb->st_mode = 0x2180;
193  uap->sb->st_blksize = 0x1000;
194  kprintf("fstat: %i", uap->fd);
195  return (0x0);
196 }
197 
203 int ioctl(struct thread *td, struct ioctl_args *uap) {
204  td->td_retval[0] = 0x0;
205  return (0x0);
206 }
207 
213 int getfd(struct thread *td, struct file **fp, int fd) {
214  int error = 0x0;
215 
216 #ifdef DEBUG
217  kprintf("[%s:%i]",__FILE__,__LINE__);
218 #endif
219 
220  *fp = (struct file*) td->o_files[fd];
221 
222  if (fp == 0x0)
223  error = -1;
224 
225  return (error);
226 }
227 
228 int sys_ioctl(struct thread *td, struct sys_ioctl_args *args) {
229  switch (args->com) {
230  case TIOCGETA:
231  if (args->fd == 0 || args->fd == 1) {
232  struct termios *t = (struct termios*) args->data;
233 
234  t->c_iflag = 0x2B02;
235  t->c_oflag = 0x3;
236  t->c_cflag = 0x4B00;
237  t->c_lflag = 0x5CB;
238 
239  t->c_cc[0] = 4;
240  t->c_cc[1] = 255;
241  t->c_cc[2] = 255;
242  t->c_cc[3] = 127;
243  t->c_cc[4] = 23;
244  t->c_cc[5] = 21;
245  t->c_cc[6] = 18;
246  t->c_cc[7] = 8;
247  t->c_cc[8] = 3;
248  t->c_cc[9] = 28;
249  t->c_cc[10] = 26;
250  t->c_cc[11] = 25;
251  t->c_cc[12] = 17;
252  t->c_cc[13] = 19;
253  t->c_cc[14] = 22;
254  t->c_cc[15] = 15;
255  t->c_cc[16] = 1;
256  t->c_cc[17] = 0;
257  t->c_cc[18] = 20;
258  t->c_cc[19] = 255;
259 
260  t->c_ispeed = 0x9600;
261  t->c_ospeed = 0x9600;
262 
263  td->td_retval[0] = 0;
264  }
265  else {
266  td->td_retval[0] = -1;
267  }
268  break;
269  case TIOCGWINSZ:
270  asm("nop");
271  struct winsize *win = (struct winsize*) args->data;
272  win->ws_row = 50;
273  win->ws_col = 80;
274  break;
275  default:
276  kprintf("ioFD:%i:0x%X!", args->fd, args->com);
277  break;
278  }
279 
280  td->td_retval[0] = 0x0;
281  return (0x0);
282 }
283 
284 int sys_select(struct thread *td, struct sys_select_args *args) {
285  int error = 0x0;
286  /*
287  int i = 0x0;
288 
289  fd_set sock_rfds;
290  fd_set sock_wfds;
291  fd_set sock_efds;
292 
293  FD_ZERO(&sock_rfds);
294  FD_ZERO(&sock_wfds);
295  FD_ZERO(&sock_efds);
296 
297  if (args->in != 0x0) {
298  for (i = 0; i < args->nd; i++) {
299  FD_SET(((struct file * ) td->o_files[args->in[0]]).socket, &sock_rfds);
300  }
301  }
302 
303  if (args->ou != 0x0) {
304  for (i = 0; i < args->nd; i++) {
305  FD_SET(((struct file * ) td->o_files[args->ou[0]]).socket, &sock_wfds);
306  }
307  }
308 
309  if (args->ex != 0x0) {
310  for (i = 0; i < args->nd; i++) {
311  FD_SET(((struct file * ) td->o_files[args->ex[0]]).socket, &sock_efds);
312  }
313  }
314 
315  if ((td->td_retval[0] = lwip_select(args->nd, &sock_rfds, &sock_wfds, &sock_efds, args->tv)) == -1)
316  error = -1;
317 
318  if (args->in != 0x0) {
319  for (i = 0; i < MAX_FILES; i++) {
320  if (!FD_ISSET(((struct file * ) td->o_files[args->ou[0]]).socket, &sock_rfds))
321  FD_CLR(((struct file * ) td->o_files[args->ou[0]]).socket, args->in);
322  }
323  }
324 
325  if (args->ou != 0x0) {
326  for (i = 0; i < MAX_FILES; i++) {
327  if (!FD_ISSET(((struct file * ) td->o_files[args->ou[0]]).socket, &sock_wfds))
328  FD_CLR(((struct file * ) td->o_files[args->ou[0]]).socket, args->ou);
329  }
330  }
331 
332  if (args->ex != 0x0) {
333  for (i = 0; i < MAX_FILES; i++) {
334  if (!FD_ISSET(((struct file * ) td->o_files[args->ou[0]]).socket, &sock_efds))
335  FD_CLR(((struct file * ) td->o_files[args->ou[0]]).socket, args->ex);
336  }
337  }
338 
339  */
340 
341  if ((td->td_retval[0] = lwip_select(args->nd, args->in, args->ou, args->ex, args->tv)) == -1)
342  error = -1;
343 
344  return (error);
345 }
346 
347 int dup2(struct thread *td, u_int32_t from, u_int32_t to) {
348 
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  if (fdestroy(td, (struct file*) td->o_files[to], to) != 0x0)
360  kprintf("[%s:%i] Error with fdestroy!", __FILE__, __LINE__);
361  }
362 
363  fp = (struct file*) td->o_files[from];
364  dup_fp = (struct file*) kmalloc(sizeof(struct file));
365 
366  memcpy(dup_fp, fp, sizeof(struct file));
367 
368  td->o_files[to] = (void*) dup_fp;
369 
370  ((struct file*) td->o_files[from])->fd->dup++;
371 
372  return (0x0);
373 }
374 
375 int sys_dup2(struct thread *td, struct sys_dup2_args *args) {
376 
377  int error = 0x0;
378 
379  if ((td->td_retval[0] = dup2(td, args->from, args->to)) == -1)
380  error = -1;
381 
382  return (error);
383 }
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:213
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:284
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:347
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:93
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:375
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:161
endtask.h
fdestroy
int fdestroy(struct thread *td, struct file *fp, int fd)
This destroys a thread local file descriptor.
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:533
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:175
fcntl
int fcntl(struct thread *td, struct sys_fcntl_args *uap)
Definition: descrip.c:41
fstat
int fstat(struct thread *td, struct sys_fstat_args *uap)
Definition: descrip.c:184
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:811
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:97
kmalloc
void * kmalloc(uInt32 len)
Definition: kmalloc.c:241
descrip.h
sys_dup2_args
Definition: sysproto_posix.h:809
select.h
sys_dup2_args::to
u_int to
Definition: sysproto_posix.h:814
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:203
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:228
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