The Pedigree Project  0.1
pipe-syscalls.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "pedigree/kernel/syscallError.h"
21 
22 #include "file-syscalls.h"
23 #include "modules/system/vfs/Pipe.h"
24 #include "modules/system/vfs/VFS.h"
25 #include "pipe-syscalls.h"
26 
27 #include "pedigree/kernel/Subsystem.h"
28 #include <FileDescriptor.h>
29 #include <PosixSubsystem.h>
30 
31 #include "modules/Module.h"
32 
33 #include "pedigree/kernel/process/Process.h"
34 #include "pedigree/kernel/processor/Processor.h"
35 
36 #include <fcntl.h>
37 
39 
40 int posix_pipe(int filedes[2])
41 {
43  reinterpret_cast<uintptr_t>(filedes), sizeof(int) * 2,
44  PosixSubsystem::SafeWrite))
45  {
46  F_NOTICE("pipe -> invalid address");
47  SYSCALL_ERROR(InvalidArgument);
48  return -1;
49  }
50 
51  F_NOTICE("pipe");
52 
53  Process *pProcess =
54  Processor::information().getCurrentThread()->getParent();
55  PosixSubsystem *pSubsystem =
56  reinterpret_cast<PosixSubsystem *>(pProcess->getSubsystem());
57  if (!pSubsystem)
58  {
59  ERROR("No subsystem for the process!");
60  return -1;
61  }
62 
63  size_t readFd = pSubsystem->getFd();
64  size_t writeFd = pSubsystem->getFd();
65 
66  filedes[0] = readFd;
67  filedes[1] = writeFd;
68 
69  File *p = new Pipe(String(""), 0, 0, 0, 0, 0, 0, 0, true);
70 
71  // Create the file descriptor for both
72  FileDescriptor *read = new FileDescriptor(p, 0, readFd, 0, O_RDONLY);
73  pSubsystem->addFileDescriptor(readFd, read);
74 
75  FileDescriptor *write = new FileDescriptor(p, 0, writeFd, 0, O_WRONLY);
76  pSubsystem->addFileDescriptor(writeFd, write);
77 
78  F_NOTICE("pipe: returning " << readFd << " and " << writeFd << ".");
79 
80  return 0;
81 }
Definition: String.h:49
static ProcessorInformation & information()
Definition: Processor.cc:45
void addFileDescriptor(size_t fd, FileDescriptor *pFd)
static bool checkAddress(uintptr_t addr, size_t extent, size_t flags)
#define ERROR(text)
Definition: Log.h:82
Definition: File.h:66
Definition: Pipe.h:35