[449] | 1 | /* |
---|
| 2 | * unistd.h - User level <unistd> library definition. |
---|
| 3 | * |
---|
| 4 | * Author Alain Greiner (2016,2017,2018) |
---|
| 5 | * |
---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 7 | * |
---|
| 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 11 | * under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 13 | * |
---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 17 | * General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | #ifndef _UNISTD_H_ |
---|
| 25 | #define _UNISTD_H_ |
---|
| 26 | |
---|
| 27 | /***************************************************************************************** |
---|
[589] | 28 | * This file defines the user level <unistd> library. |
---|
[449] | 29 | * All these functions make a system call to access the kernel structures. |
---|
| 30 | * The user/kernel shared structures and mnemonics are defined in |
---|
| 31 | * the <syscalls/shared_include/shared_unistd.h> file. |
---|
| 32 | ****************************************************************************************/ |
---|
| 33 | |
---|
| 34 | #include <shared_unistd.h> |
---|
| 35 | |
---|
| 36 | /***************************************************************************************** |
---|
[626] | 37 | * This function implements the "alarm" system call. |
---|
| 38 | * sets a timer to deliver the signal SIGALRM to the calling process, |
---|
[589] | 39 | * after the specified number of seconds. |
---|
| 40 | * If an alarm has already been set with alarm() but has not been delivered, |
---|
| 41 | * another call to alarm() will supersede the prior call. |
---|
| 42 | * The request alarm(0) cancels the current alarm and the signal will not be delivered. |
---|
| 43 | ***************************************************************************************** |
---|
| 44 | * @ seconds : number of seconds. |
---|
| 45 | * @ returns the amount of time left on the timer from a previous call to alarm(). |
---|
| 46 | * If no alarm is currently set, the return value is 0. |
---|
| 47 | ****************************************************************************************/ |
---|
| 48 | unsigned alarm( unsigned seconds ); |
---|
| 49 | |
---|
| 50 | /***************************************************************************************** |
---|
[626] | 51 | * This function implements the "chdir" system call. |
---|
| 52 | * It changes the current working directory in the reference process descriptor. |
---|
[589] | 53 | ***************************************************************************************** |
---|
| 54 | * @ pathname : pathname (can be relative or absolute). |
---|
| 55 | * @ return 0 if success / returns -1 if failure. |
---|
| 56 | ****************************************************************************************/ |
---|
| 57 | int chdir( const char * pathname ); |
---|
| 58 | |
---|
| 59 | /***************************************************************************************** |
---|
[626] | 60 | * This function implements the "close" system call. |
---|
| 61 | * It releases the memory allocated for the file identified by the <fd> argument, |
---|
| 62 | * and remove the fd array_entry in all process descriptor copies. |
---|
[589] | 63 | ***************************************************************************************** |
---|
| 64 | * @ fd : file descriptor index in fd_array. |
---|
| 65 | * @ return 0 if success / returns -1 if failure. |
---|
| 66 | ****************************************************************************************/ |
---|
| 67 | int close( int fd ); |
---|
| 68 | |
---|
| 69 | /***************************************************************************************** |
---|
[626] | 70 | * This function implement the "exec" system call. |
---|
[589] | 71 | * It creates, in the same cluster as the calling thread, a new process descriptor, |
---|
| 72 | * and a new associated main thread descriptor, executing a new memory image defined |
---|
| 73 | * by the <filename> argument. This new process inherit from the old process the PID |
---|
| 74 | * and the PPID, as well as all open files (including the TXT). |
---|
| 75 | * The old process descriptor, and all its threads are blocked, and marked for deletion. |
---|
| 76 | * Therefore the exec syscall does not return to the calling thread in case of success. |
---|
| 77 | * This function build an exec_info_t structure containing the new process arguments, |
---|
| 78 | * as defined by the <arv> argument, and the new process environment variables, |
---|
| 79 | * as defined by the <envp> argument. |
---|
| 80 | * TODO : the <argv> and <envp> arguments are not supported yet (both must be NULL). |
---|
| 81 | ***************************************************************************************** |
---|
| 82 | * @ filename : string pointer on .elf filename (virtual pointer in user space) |
---|
| 83 | * @ argv : array of strings on process arguments (virtual pointers in user space) |
---|
| 84 | * @ envp : array of strings on environment variables (virtual pointers in user space) |
---|
| 85 | * @ does not return if success / returns -1 if failure. |
---|
| 86 | ****************************************************************************************/ |
---|
| 87 | int execve( char * filename, |
---|
| 88 | char ** argv, |
---|
| 89 | char ** envp ); |
---|
| 90 | |
---|
| 91 | /***************************************************************************************** |
---|
[626] | 92 | * This function implement the "fork" system call. |
---|
[589] | 93 | * The calling process descriptor (parent process), and the associated thread descriptor |
---|
| 94 | * are replicated in a - likely - remote cluster, that becomes the new process owner. |
---|
| 95 | * The child process get a new PID is linked to the parent PID. The child process inherit |
---|
| 96 | * from the parent process the memory image, and all open files (including the TXT). |
---|
| 97 | * The child process becomes the TXT terminal owner. |
---|
| 98 | * The target cluster depends on the "fork_user" flag and "fork_cxy" variable that can be |
---|
| 99 | * stored in the calling thread descriptor by the specific fork_place() system call. |
---|
| 100 | * If not, the kernel function makes a query to the DQDT to select the target cluster. |
---|
| 101 | ***************************************************************************************** |
---|
| 102 | * @ if success, returns child process PID to parent, and return O to child. |
---|
| 103 | * @ if failure, returns -1 to parent / no child process is created. |
---|
| 104 | ****************************************************************************************/ |
---|
| 105 | int fork( void ); |
---|
| 106 | |
---|
| 107 | /***************************************************************************************** |
---|
[626] | 108 | * This function implements the "fsync" system call. |
---|
| 109 | * It causes all the modified data and attributes of file identified by the <fd> argument |
---|
| 110 | * to be copied from the file mapper and file descriptor to the IOC device. |
---|
[449] | 111 | ***************************************************************************************** |
---|
[626] | 112 | * @ fd : file descriptor index in fd_array. |
---|
| 113 | * @ return 0 if success / returns -1 if failure. |
---|
| 114 | ****************************************************************************************/ |
---|
| 115 | int fsync( int fd ); |
---|
| 116 | |
---|
| 117 | /***************************************************************************************** |
---|
| 118 | * This function implements the "getcwd" system call. |
---|
| 119 | * It returns the pathname of the current working directory. |
---|
| 120 | ***************************************************************************************** |
---|
[449] | 121 | * buf : buffer addres in user space. |
---|
| 122 | * nbytes : user buffer size in bytes. |
---|
| 123 | * @ return 0 if success / returns -1 if failure. |
---|
| 124 | ****************************************************************************************/ |
---|
| 125 | int getcwd( char * buf, |
---|
| 126 | unsigned int nbytes ); |
---|
| 127 | |
---|
| 128 | /***************************************************************************************** |
---|
[626] | 129 | * This function implements the "getpid" system call. |
---|
| 130 | * It returns the process identifier. |
---|
[589] | 131 | ***************************************************************************************** |
---|
| 132 | * @ returns the process PID for the calling thread process. |
---|
[449] | 133 | ****************************************************************************************/ |
---|
[589] | 134 | int getpid( void ); |
---|
[449] | 135 | |
---|
| 136 | /***************************************************************************************** |
---|
[626] | 137 | * This function implements the "isatty" system call. |
---|
| 138 | * It test whether a file descriptor refers to a terminal. |
---|
[589] | 139 | ***************************************************************************************** |
---|
| 140 | * @ fd : file descriptor index in fd_array. |
---|
| 141 | * @ returns 1 if fd is an open file descriptor referring to a terminal / 0 otherwise. |
---|
[449] | 142 | ****************************************************************************************/ |
---|
[589] | 143 | int isatty( int fd ); |
---|
[449] | 144 | |
---|
| 145 | /***************************************************************************************** |
---|
[626] | 146 | * This function implements the "lseek" system call. |
---|
| 147 | * It repositions the offset of the file descriptor identified by <fd>, |
---|
[449] | 148 | * according to the operation type defined by the <whence> argument. |
---|
| 149 | ***************************************************************************************** |
---|
| 150 | * @ fd : open file index in fd_array. |
---|
| 151 | * @ offset : used to compute new offset value. |
---|
[589] | 152 | * @ whence : operation type (SEEK_SET / SEEK_CUR / SEEK_END) |
---|
[449] | 153 | * @ return 0 if success / returns -1 if failure. |
---|
| 154 | ****************************************************************************************/ |
---|
| 155 | int lseek( int fd, |
---|
| 156 | unsigned int offset, |
---|
| 157 | int whence ); |
---|
| 158 | |
---|
| 159 | /***************************************************************************************** |
---|
[626] | 160 | * This function implements the "pause" system call. |
---|
| 161 | * It stops the calling process until a signal is received. |
---|
[449] | 162 | ***************************************************************************************** |
---|
| 163 | * @ return 0 if success / returns -1 if failure. |
---|
| 164 | ****************************************************************************************/ |
---|
[589] | 165 | int pause( void ); |
---|
[449] | 166 | |
---|
| 167 | /***************************************************************************************** |
---|
[626] | 168 | * This function implements the "pipe" system call. |
---|
| 169 | * It creates in the calling thread cluster an unnamed pipe, and two (read and write) |
---|
| 170 | * file descriptors to access this pipe. The argument is a pointer a fd[] array. |
---|
[449] | 171 | * TODO not implemented yet... |
---|
| 172 | ***************************************************************************************** |
---|
[459] | 173 | * @ fd[0] : [out] read only file descriptor index. |
---|
| 174 | * @ fd[1] : [out] write only file descriptor index. |
---|
[449] | 175 | * @ return 0 if success / return -1 if failure. |
---|
| 176 | ****************************************************************************************/ |
---|
| 177 | int pipe( int fd[2] ); |
---|
| 178 | |
---|
| 179 | /***************************************************************************************** |
---|
[626] | 180 | * This function implements the "read" system call. |
---|
| 181 | * It reads bytes from an open file identified by the <fd> file descriptor. |
---|
[589] | 182 | * This file can be a regular file or a character oriented device. |
---|
[449] | 183 | ***************************************************************************************** |
---|
[589] | 184 | * @ fd : open file index in fd_array. |
---|
| 185 | * @ buf : buffer virtual address in user space. |
---|
| 186 | * @ count : number of bytes. |
---|
| 187 | * @ return number of bytes actually read if success / returns -1 if failure. |
---|
[449] | 188 | ****************************************************************************************/ |
---|
[589] | 189 | int read( int fd, |
---|
| 190 | void * buf, |
---|
| 191 | unsigned int count ); |
---|
[449] | 192 | |
---|
| 193 | /***************************************************************************************** |
---|
[626] | 194 | * This function implements the "rmdir" system call. |
---|
| 195 | * It removes a directory file whose name is given by <pathname>. |
---|
| 196 | * The directory must not contain any entries other than `.' and `..'. |
---|
[449] | 197 | ***************************************************************************************** |
---|
| 198 | * @ pathname : pathname (can be relative or absolute). |
---|
| 199 | * @ return 0 if success / returns -1 if failure. |
---|
| 200 | ****************************************************************************************/ |
---|
| 201 | int rmdir( char * pathname ); |
---|
| 202 | |
---|
| 203 | /***************************************************************************************** |
---|
[626] | 204 | * This function implements the "sync" system call. |
---|
| 205 | * It forces all kernel mappers (file caches) to be copied to the IOC device. |
---|
| 206 | ****************************************************************************************/ |
---|
| 207 | void sync( void ); |
---|
| 208 | |
---|
| 209 | /***************************************************************************************** |
---|
| 210 | * This function implements the "unlink" system call. |
---|
| 211 | * It removes a directory entry identified by the <pathname> from the parent directory, |
---|
| 212 | * and decrement the link count of the file referenced by the link. |
---|
[589] | 213 | * If the link count reduces to zero, and no process has the file open, then all resources |
---|
| 214 | * associated with the file are released. If one or more process have the file open when |
---|
| 215 | * the last link is removed, the link is removed, but the removal of the file is delayed |
---|
| 216 | * until all references to it have been closed. |
---|
| 217 | ***************************************************************************************** |
---|
| 218 | * @ pathname : pathname (can be relative or absolute). |
---|
| 219 | * @ return 0 if success / returns -1 if failure. |
---|
[449] | 220 | ****************************************************************************************/ |
---|
[589] | 221 | int unlink( const char * pathname ); |
---|
[449] | 222 | |
---|
| 223 | /***************************************************************************************** |
---|
[626] | 224 | * This function implements the "write" system call. |
---|
| 225 | * It writes bytes to an open file identified by the <fd> file descriptor. |
---|
[589] | 226 | * This file can be a regular file or character oriented device. |
---|
| 227 | ***************************************************************************************** |
---|
| 228 | * @ fd : open file index in fd_array. |
---|
| 229 | * @ buf : buffer virtual address in user space. |
---|
| 230 | * @ count : number of bytes. |
---|
| 231 | * @ return number of bytes actually written if success / returns -1 if failure. |
---|
[449] | 232 | ****************************************************************************************/ |
---|
[589] | 233 | int write( int fd, |
---|
| 234 | const void * buf, |
---|
| 235 | unsigned int count ); |
---|
[449] | 236 | |
---|
| 237 | #endif |
---|