| [1] | 1 | /* |
|---|
| 2 | * kern/sys_open.c - open a named file |
|---|
| 3 | * |
|---|
| 4 | * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless |
|---|
| 5 | * Copyright (c) 2011,2012 UPMC Sorbonne Universites |
|---|
| 6 | * |
|---|
| 7 | * This file is part of ALMOS-kernel. |
|---|
| 8 | * |
|---|
| 9 | * ALMOS-kernel is free software; you can redistribute it and/or modify it |
|---|
| 10 | * under the terms of the GNU General Public License as published by |
|---|
| 11 | * the Free Software Foundation; version 2.0 of the License. |
|---|
| 12 | * |
|---|
| 13 | * ALMOS-kernel is distributed in the hope that it will be useful, but |
|---|
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 16 | * General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with ALMOS-kernel; if not, write to the Free Software Foundation, |
|---|
| 20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #include <errno.h> |
|---|
| 24 | #include <thread.h> |
|---|
| 25 | #include <vfs.h> |
|---|
| 26 | #include <sys-vfs.h> |
|---|
| 27 | #include <process.h> |
|---|
| 28 | #include <spinlock.h> |
|---|
| 29 | #include <cpu-trace.h> |
|---|
| 30 | |
|---|
| 31 | /////////////////////////////////// |
|---|
| 32 | int sys_open ( char * pathname, |
|---|
| 33 | uint32_t flags, |
|---|
| 34 | uint32_t mode ) |
|---|
| 35 | { |
|---|
| 36 | CPU_HW_TRACE(sys_open); |
|---|
| 37 | |
|---|
| 38 | error_t err; |
|---|
| 39 | struct vfs_file_s file; |
|---|
| 40 | struct ku_obj ku_path; |
|---|
| 41 | thread_t * this = current_thread; |
|---|
| 42 | process_t * process = current_process; |
|---|
| 43 | uint32_t fd = (uint32_t)-1; |
|---|
| 44 | |
|---|
| 45 | cpu_trace_write(current_cpu, sys_open); |
|---|
| 46 | |
|---|
| 47 | if( process_fd_aray_full( process ) ) |
|---|
| 48 | { |
|---|
| 49 | this->info.errno = ENFILE; |
|---|
| 50 | CPU_HW_TRACE(sys_open); |
|---|
| 51 | return fd; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | if( VFS_IS( flags , VFS_O_DIRECTORY ) ) VFS_SET( flags , VFS_DIR ); |
|---|
| 55 | |
|---|
| 56 | KU_BUFF( ku_path , pathname ); |
|---|
| 57 | |
|---|
| 58 | // get the cwd lock |
|---|
| 59 | rwlock_rdlock( &process->cwd_lock ); |
|---|
| 60 | |
|---|
| 61 | err = vfs_open( &process->vfs_cwd , &ku_path , flags , mode , &file ); |
|---|
| 62 | if( err ) |
|---|
| 63 | { |
|---|
| 64 | this->info.errno = (err < 0 ) ? -err : err; |
|---|
| 65 | rwlock_unlock( &process->cwd_lock ); |
|---|
| 66 | CPU_HW_TRACE(sys_open); |
|---|
| 67 | return fd; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | err = process_fd_set( process , &file , &fd ); |
|---|
| 71 | if( err ) |
|---|
| 72 | { |
|---|
| 73 | vfs_close(&file, NULL); |
|---|
| 74 | this->info.errno = err; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | rwlock_unlock( &process->cwd_lock ); |
|---|
| 78 | CPU_HW_TRACE(sys_open); |
|---|
| 79 | return fd; |
|---|
| 80 | } |
|---|