UbixOS V2  2.0
vfs_calls.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 <ubixos/sched.h>
30 #include <sys/thread.h>
31 #include <sys/sysproto_posix.h>
32 #include <sys/descrip.h>
33 #include <sys/video.h>
34 #include <sys/pipe.h>
35 #include <sys/errno.h>
36 #include <string.h>
37 #include <ufs/ufs.h>
38 
39 int sys_open(struct thread *td, struct sys_open_args *args) {
40  kprintf("sys_open?");
41  return (kern_openat(td, AT_FDCWD, args->path, args->flags, args->mode));
42 
43 }
44 
45 int sys_openat(struct thread *td, struct sys_openat_args *args) {
46 
47  int error = 0x0;
48  int fd = 0x0;
49  struct file *nfp = 0x0;
50 
51 #ifdef DEBUG_OPENAT
52  kprintf("openat");
53 #endif
54 
55  error = falloc(td, &nfp, &fd);
56 
57  if (error)
58  return (error);
59 
60  if ((args->flag & O_WRONLY) == O_WRONLY)
61  nfp->fd = fopen(args->path, "w");
62  else if ((args->flag & O_RDWR) == O_RDWR)
63  nfp->fd = fopen(args->path, "a");
64  else
65  nfp->fd = fopen(args->path, "r");
66 
67  if (nfp->fd == 0x0) {
68  fdestroy(td, nfp, fd);
69 
70  td->td_retval[0] = -1;
71  error = -1;
72  /*
73 
74  kprintf("[sOA: 0x%X:%s:%s:]", args->flag, args->mode, args->path, td->td_retval[0]);
75 
76  if ((args->flag & O_RDONLY) == O_RDONLY)
77  kprintf("O_RDONLY");
78 
79  if ((args->flag & O_WRONLY) == O_WRONLY)
80  kprintf("O_WRONLY");
81 
82  if ((args->flag & O_RDWR) == O_RDWR)
83  kprintf("O_RDWR");
84 
85  if ((args->flag & O_ACCMODE) == O_ACCMODE)
86  kprintf("O_ACCMODE");
87  */
88 
89  }
90  else {
91  td->td_retval[0] = fd;
92  }
93 
94  //kprintf("[sOA: 0x%X:%s:%s:]", args->flag, args->mode, args->path, td->td_retval[0]);
95  kprintf("sys_openat: %i, 0x%X, 0x%X", fd, nfp, nfp->fd);
96  return (error);
97 }
98 
99 int sys_close(struct thread *td, struct sys_close_args *args) {
100  struct file *fd = 0x0;
101  struct pipeInfo *pFD = 0x0;
102 
103  getfd(td, &fd, args->fd);
104 
105  kprintf("[sC:%i:0x%X:0x%X]", args->fd, fd, fd->fd);
106 
107 #ifdef DEBUG_VFS_CALLS
108  kprintf("[sC::0x%X:0x%X]", args->fd, fd, fd->fd);
109 #endif
110 
111  if (fd == 0x0) {
112  kprintf("COULDN'T FIND FD: ", args->fd);
113  td->td_retval[0] = -1;
114  }
115  else {
116  switch (fd->fd_type) {
117  case 3:
118  pFD = fd->data;
119  if (args->fd == pFD->rFD) {
120  if (pFD->rfdCNT < 2)
121  fdestroy(td, fd, args->fd);
122  pFD->rfdCNT--;
123  }
124 
125  if (args->fd == pFD->wFD) {
126  if (pFD->wfdCNT < 2)
127  fdestroy(td, fd, args->fd);
128  pFD->wfdCNT--;
129  }
130 
131  break;
132  default:
133  if (args->fd < 3)
134  td->td_retval[0] = 0;
135  else {
136  if (!fclose(fd->fd))
137  td->td_retval[0] = -1;
138 
139  kprintf("DESTROY: %i!", args->fd);
140  if (!fdestroy(td, fd, args->fd))
141  kprintf("[%s:%i] fdestroy(0x%X, 0x%X) failed\n", __FILE__, __LINE__, fd, td->o_files[args->fd]);
142 
143  td->td_retval[0] = 0;
144 
145  }
146  }
147  }
148  return (0);
149 }
150 
151 int sys_read(struct thread *td, struct sys_read_args *args) {
152  int x = 0;
153  char c = 0x0;
154  char bf[2];
155  volatile char *buf = args->buf;
156 
157  struct file *fd = 0x0;
158 
159  struct pipeInfo *pFD = 0x0;
160  struct pipeInfo *rpFD = 0x0;
161 
162  size_t nbytes;
163 
164  int rpCNT = 0;
165 
166  getfd(td, &fd, args->fd);
167 
168  if (args->fd > 3) {
169  switch (fd->fd_type) {
170  case 3: /* XXX - Pipe2 Handling */
171  pFD = fd->data;
172  while (pFD->bCNT == 0 && rpCNT < 100) {
173  sched_yield();
174  rpCNT++;
175  }
176 
177  if (rpCNT >= 100 && pFD->bCNT == 0) {
178  td->td_retval[0] = 0;
179  }
180  else {
181  nbytes = (args->nbyte - (pFD->headPB->nbytes - pFD->headPB->offset) <= 0) ? args->nbyte : (pFD->headPB->nbytes - pFD->headPB->offset);
182  //kprintf("[unb: , nbs: %i, bf: 0x%X]", args->nbyte, nbytes, fd->fd->buffer);
183  //kprintf("PR: []", nbytes);
184  memcpy(args->buf, pFD->headPB->buffer + pFD->headPB->offset, nbytes);
185  pFD->headPB->offset += nbytes;
186 
187  if (pFD->headPB->offset >= pFD->headPB->nbytes) {
188  rpFD = pFD->headPB;
189  pFD->headPB = pFD->headPB->next;
190  kfree(rpFD);
191  pFD->bCNT--;
192  }
193 
194  td->td_retval[0] = nbytes;
195  }
196  break;
197  default:
198  //kprintf("[r:0x%X::%i:%s]",fd->fd, args->fd, fd->fd_type, fd->fd->fileName);
199  td->td_retval[0] = fread(args->buf, args->nbyte, 1, fd->fd);
200  }
201  }
202  else {
203  bf[1] = '\0';
204  if (_current->term == tty_foreground)
205  c = getchar();
206 
207  for (x = 0; x < args->nbyte && c != '\n';) {
208  if (_current->term == tty_foreground) {
209 
210  if (c != 0x0) {
211  buf[x++] = c;
212  bf[0] = c;
213  kprintf(bf);
214  }
215 
216  if (c == '\n') {
217  buf[x++] = c;
218  break;
219  }
220 
221  sched_yield();
222  c = getchar();
223  }
224  else {
225  sched_yield();
226  }
227  }
228  if (c == '\n')
229  buf[x++] = '\n';
230 
231  bf[0] = '\n';
232  kprintf(bf);
233 
234  td->td_retval[0] = x;
235  }
236  return (0);
237 }
238 
239 int sys_pread(struct thread *td, struct sys_pread_args *args) {
240  int offset = 0;
241  int x = 0;
242  char c = 0x0;
243  char bf[2];
244  volatile char *buf = args->buf;
245 
246  struct file *fd = 0x0;
247 
248  getfd(td, &fd, args->fd);
249 
250  if (args->fd > 3) {
251  offset = fd->fd->offset;
252  fd->fd->offset = args->offset;
253  td->td_retval[0] = fread(args->buf, args->nbyte, 1, fd->fd);
254  fd->fd->offset = offset;
255  }
256  else {
257  bf[1] = '\0';
258  if (_current->term == tty_foreground)
259  c = getchar();
260 
261  for (x = 0; x < args->nbyte && c != '\n';) {
262  if (_current->term == tty_foreground) {
263 
264  if (c != 0x0) {
265  buf[x++] = c;
266  bf[0] = c;
267  kprintf(bf);
268  }
269 
270  if (c == '\n') {
271  buf[x++] = c;
272  break;
273  }
274 
275  sched_yield();
276  c = getchar();
277  }
278  else {
279  sched_yield();
280  }
281  }
282  if (c == '\n')
283  buf[x++] = '\n';
284 
285  bf[0] = '\n';
286  kprintf(bf);
287 
288  td->td_retval[0] = x;
289  }
290  return (0);
291 }
292 
293 int sys_write(struct thread *td, struct sys_write_args *uap) {
294  char *buffer = 0x0;
295  struct file *fd = 0x0;
296 
297  struct pipeInfo *pFD = 0x0;
298  struct pipeBuf *pBuf = 0x0;
299 
300  size_t nbytes;
301 
302  if (uap->fd == 2) {
303  buffer = kmalloc(1024);
304  memcpy(buffer, uap->buf, uap->nbyte);
305  printColor += 1;
306  kprintf(buffer);
308  kfree(buffer);
309  td->td_retval[0] = uap->nbyte;
310  }
311  else if (uap->fd == 1 && ((struct file*) td->o_files[1])->fd == 0x0) {
312  buffer = kmalloc(1024);
313  memcpy(buffer, uap->buf, uap->nbyte);
314  kprintf(buffer);
315  kfree(buffer);
316  td->td_retval[0] = uap->nbyte;
317  }
318  else {
319  getfd(td, &fd, uap->fd);
320 
321  //kprintf("[fd: %i:0x%X, fd_type: %i]", uap->fd, fd, fd->fd_type);
322 
323  switch (fd->fd_type) {
324  case 3: /* XXX - Temp Pipe Stuff */
325 
326  pFD = fd->data;
327  pBuf = (struct pipeBuf*) kmalloc(sizeof(struct pipeBuf));
328  pBuf->buffer = kmalloc(uap->nbyte);
329 
330  memcpy(pBuf->buffer, uap->buf, uap->nbyte);
331 
332  pBuf->nbytes = uap->nbyte;
333 
334  if (pFD->tailPB)
335  pFD->tailPB->next = pBuf;
336 
337  pFD->tailPB = pBuf;
338 
339  if (!pFD->headPB)
340  pFD->headPB = pBuf;
341 
342  pFD->bCNT++;
343 
344  td->td_retval[0] = nbytes;
345 
346  break;
347  default:
348  if (fd->fd) {
349  kprintf("[0x%X]", fd->fd->res);
350  td->td_retval[0] = fwrite(uap->buf, uap->nbyte, 1, fd->fd);
351  }
352  else {
353  kprintf("[%i]", uap->nbyte);
354  buffer = kmalloc(uap->nbyte);
355  memcpy(buffer, uap->buf, uap->nbyte);
356  kprintf("(%i) %s", uap->fd, uap->buf);
357  kfree(buffer);
358  td->td_retval[0] = uap->nbyte;
359  }
360  }
361 
362  }
363  return (0x0);
364 }
365 
366 int sys_access(struct thread *td, struct sys_access_args *args) {
367  /* XXX - This is a temporary as it always returns true */
368 
369  td->td_retval[0] = 0;
370  return (0);
371 }
372 
373 int sys_getdirentries(struct thread *td, struct sys_getdirentries_args *args) {
374 
375  struct file *fd = 0x0;
376 
377  getfd(td, &fd, args->fd);
378 
379  char buf[DEV_BSIZE];
380  struct dirent *d;
381  char *s;
382  ssize_t n;
383 
384  td->td_retval[0] = fread(args->buf, args->count, 1, fd->fd);
385 
386  return (0);
387 }
388 
389 int sys_readlink(struct thread *thr, struct sys_readlink_args *args) {
390  /* XXX - Need to implement readlink */
391 
392  kprintf("RL: %s:\n", args->path, args->count);
393 
394  //Return Error
395  thr->td_retval[0] = 2;
396  return (-1);
397 }
398 
399 int kern_openat(struct thread *thr, int afd, char *path, int flags, int mode) {
400  int error = 0x0;
401  int fd = 0x0;
402  struct file *nfp = 0x0;
403 
404  /*
405  * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
406  * may be specified.
407  */
408  if (flags & O_EXEC) {
409  if (flags & O_ACCMODE)
410  return (EINVAL);
411  }
412  else if ((flags & O_ACCMODE) == O_ACCMODE) {
413  return (EINVAL);
414  }
415  else {
416  flags = FFLAGS(flags);
417  }
418 
419  error = falloc(thr, &nfp, &fd);
420 
421  if (error) {
422  thr->td_retval[0] = -1;
423  return (error);
424  }
425 
426  if (flags | O_CREAT)
427  kprintf("O_CREAT\n");
428 
429  nfp->f_flag = flags & FMASK;
430 
431  nfp->fd = fopen(path, "rwb");
432 
433  if (nfp->fd == 0x0) {
434  fdestroy(thr, nfp, fd);
435 
436  thr->td_retval[0] = -1;
437 
438  error = -1;
439  }
440  else {
441  thr->td_retval[0] = fd;
442  }
443 
444  //kprintf("sO: 0x%X:%s:", args->mode, args->path, td->td_retval[0]);
445 
446  return (error);
447 
448 }
sys_getdirentries
int sys_getdirentries(struct thread *td, struct sys_getdirentries_args *args)
Definition: vfs_calls.c:373
sys_write_args::buf
const void * buf
Definition: sysproto_posix.h:80
sys_pread_args::fd
int fd
Definition: sysproto_posix.h:730
FMASK
#define FMASK
Definition: fcntl.h:93
sys_pread_args::offset
off_t offset
Definition: sysproto_posix.h:739
sys_open_args::mode
int mode
Definition: sysproto_posix.h:98
buffer
char * buffer
Definition: shell.c:47
sys_pread_args::nbyte
size_t nbyte
Definition: sysproto_posix.h:736
O_EXEC
#define O_EXEC
Definition: fcntl.h:78
defaultColor
#define defaultColor
Definition: video.h:34
sys_openat
int sys_openat(struct thread *td, struct sys_openat_args *args)
Definition: vfs_calls.c:45
fopen
fileDescriptor_t * fopen(const char *file, const char *flags)
Definition: file.c:388
fileDescriptor::res
void * res
Definition: file.h:82
thread::o_files
void * o_files[512]
Definition: thread.h:42
pipeBuf::next
struct pipeBuf * next
Definition: pipe.h:38
file::data
void * data
Definition: descrip.h:74
string.h
fileDescriptor::offset
off_t offset
Definition: file.h:69
file::f_flag
uint32_t f_flag
Definition: descrip.h:68
video.h
sysproto_posix.h
fread
size_t fread(void *ptr, size_t size, size_t nmemb, fileDescriptor_t *fd)
Definition: file.c:291
file
Definition: descrip.h:67
kfree
void kfree(void *baseAddr)
Definition: kmalloc.c:342
printColor
int printColor
Definition: video.c:35
thread
Definition: thread.h:40
sys_open_args::flags
int flags
Definition: sysproto_posix.h:94
sys_close_args::fd
int fd
Definition: sysproto_posix.h:104
fwrite
size_t fwrite(void *ptr, int size, int nmemb, fileDescriptor_t *fd)
Definition: file.c:311
fdestroy
int fdestroy(struct thread *td, struct file *fp, int fd)
The function bar.
Definition: descrip.c:147
sys_open_args
Definition: sysproto_posix.h:88
sys_getdirentries_args::count
u_int count
Definition: sysproto_posix.h:634
pipeInfo::rfdCNT
int rfdCNT
Definition: pipe.h:47
O_ACCMODE
#define O_ACCMODE
Definition: fcntl.h:64
sched.h
falloc
int falloc(struct thread *, struct file **, int *)
Definition: descrip.c:96
sys_getdirentries_args::fd
int fd
Definition: sysproto_posix.h:628
file::fd_type
int fd_type
Definition: descrip.h:72
sys_open
int sys_open(struct thread *td, struct sys_open_args *args)
Definition: vfs_calls.c:39
sys_getdirentries_args
Definition: sysproto_posix.h:626
O_RDWR
#define O_RDWR
Definition: fcntl.h:63
memcpy
void * memcpy(const void *dst, const void *src, size_t length)
sys_pread_args
Definition: sysproto_posix.h:728
pipeInfo::bCNT
int bCNT
Definition: pipe.h:50
pipe.h
errno.h
pipeInfo::rFD
int rFD
Definition: pipe.h:46
file::fd
fileDescriptor_t * fd
Definition: descrip.h:71
AT_FDCWD
#define AT_FDCWD
Definition: fcntl.h:32
thread::td_retval
int td_retval[2]
Definition: thread.h:41
sys_write
int sys_write(struct thread *td, struct sys_write_args *uap)
Definition: vfs_calls.c:293
fclose
int fclose(fileDescriptor_t *fd)
Definition: file.c:526
pipeBuf::nbytes
size_t nbytes
Definition: pipe.h:40
dirent
Definition: ufs.h:92
sys_read_args
Definition: sysproto_posix.h:62
taskStruct::term
tty_term * term
Definition: sched.h:77
buf
Definition: buf.h:35
sys_readlink
int sys_readlink(struct thread *thr, struct sys_readlink_args *args)
Definition: vfs_calls.c:389
pipeBuf::buffer
char * buffer
Definition: pipe.h:39
getfd
int getfd(struct thread *td, struct file **fp, int fd)
get pointer to file fd in specified thread
Definition: descrip.c:214
pipeInfo::wFD
int wFD
Definition: pipe.h:48
sys_openat_args::flag
int flag
Definition: sysproto_posix.h:507
sys_write_args
Definition: sysproto_posix.h:74
pipeInfo::tailPB
struct pipeBuf * tailPB
Definition: pipe.h:52
sys_read_args::fd
int fd
Definition: sysproto_posix.h:64
sys_write_args::fd
int fd
Definition: sysproto_posix.h:76
sys_read_args::buf
const void * buf
Definition: sysproto_posix.h:67
O_WRONLY
#define O_WRONLY
Definition: fcntl.h:62
sys_pread_args::buf
void * buf
Definition: sysproto_posix.h:733
sys_read
int sys_read(struct thread *td, struct sys_read_args *args)
Definition: vfs_calls.c:151
_current
kTask_t * _current
Definition: sched.c:50
pipeBuf::offset
off_t offset
Definition: pipe.h:41
sys_write_args::nbyte
size_t nbyte
Definition: sysproto_posix.h:84
sys_openat_args::path
char * path
Definition: sysproto_posix.h:504
ufs.h
sys_access
int sys_access(struct thread *td, struct sys_access_args *args)
Definition: vfs_calls.c:366
tty_foreground
tty_term * tty_foreground
Definition: tty.c:38
kmalloc
void * kmalloc(uInt32 len)
Definition: kmalloc.c:241
descrip.h
sys_openat_args
Definition: sysproto_posix.h:499
pipeBuf
Definition: pipe.h:37
kern_openat
int kern_openat(struct thread *thr, int afd, char *path, int flags, int mode)
Definition: vfs_calls.c:399
sys_getdirentries_args::buf
char * buf
Definition: sysproto_posix.h:631
O_CREAT
#define O_CREAT
Definition: fcntl.h:57
pipeInfo::headPB
struct pipeBuf * headPB
Definition: pipe.h:51
sys_open_args::path
char * path
Definition: sysproto_posix.h:90
pipeInfo
Definition: pipe.h:45
sys_access_args
Definition: sysproto_posix.h:587
ssize_t
__ssize_t ssize_t
Definition: types.h:117
sys_pread
int sys_pread(struct thread *td, struct sys_pread_args *args)
Definition: vfs_calls.c:239
kprintf
int kprintf(const char *,...)
Definition: kprintf.c:259
getchar
int getchar()
Definition: atkbd.c:346
FFLAGS
#define FFLAGS(oflags)
Definition: fcntl.h:88
thread.h
DEV_BSIZE
#define DEV_BSIZE
Definition: gpt.h:42
sys_close
int sys_close(struct thread *td, struct sys_close_args *args)
Definition: vfs_calls.c:99
EINVAL
#define EINVAL
Definition: errno.h:55
sys_close_args
Definition: sysproto_posix.h:102
sys_read_args::nbyte
size_t nbyte
Definition: sysproto_posix.h:70
sched_yield
void sched_yield()
Definition: sched.c:244
pipeInfo::wfdCNT
int wfdCNT
Definition: pipe.h:49