source: trunk/kernel/syscalls/sys_lseek.c @ 669

Last change on this file since 669 was 664, checked in by alain, 4 years ago
  • Introduce the sys_socket.c file implementing all socket related syscalls.
  • Improve the non-standard sys_get_config() function.
File size: 3.1 KB
Line 
1/*
2 * kern/sys_lseek.c - Kernel function implementing the lseek system call.
3 *
4 * AUthor        Alain Greiner (2016,2017,2018,2019)
5s
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#include <kernel_config.h>
25#include <hal_kernel_types.h>
26#include <hal_uspace.h>
27#include <errno.h>
28#include <vfs.h>
29#include <vmm.h>
30#include <thread.h>
31#include <printk.h>
32#include <process.h>
33
34/////////////////////////////////
35int sys_lseek ( uint32_t file_id,
36                uint32_t offset,
37                uint32_t whence )
38{
39        error_t    error;
40    xptr_t     file_xp;
41    uint32_t   new_offset;
42
43        thread_t  * this    = CURRENT_THREAD;
44        process_t * process = this->process;
45
46#if (DEBUG_SYS_LSEEK || CONFIG_INSTRUMENTATION_SYSCALLS)
47uint64_t     tm_start = hal_get_cycles();
48#endif
49
50#if DEBUG_SYS_LSEEK
51if( DEBUG_SYS_LSEEK < tm_start )
52printk("\n[%s] thread[%x,%x] enter / file_id %d / offset %d / cycle %d\n",
53__FUNCTION__, process->pid, this->trdid, file_id, offset, (uint32_t)tm_start );
54#endif
55
56    // check file_id argument
57        if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
58        {
59
60#if DEBUG_SYSCALLS_ERROR
61printk("\n[ERROR] in %s : thread[%x,%x] illegal file descriptor index %d\n",
62__FUNCTION__ , process->pid, this->trdid, file_id );
63#endif
64                this->errno = EBADFD;
65                return -1;
66        }
67
68    // get extended pointer on remote file descriptor
69    file_xp = process_fd_get_xptr_from_local( process , file_id );
70
71    if( file_xp == XPTR_NULL )
72    {
73
74#if DEBUG_SYSCALLS_ERROR
75printk("\n[ERROR] in %s : thread[%x,%x] undefined fd_id %d\n",
76__FUNCTION__, process->pid, this->trdid, file_id );
77#endif
78                this->errno = EBADFD;
79                return -1;
80    }
81
82// FIXME: file may be closed in parallel of seek/read/write/mmap ..etc
83
84    // call relevant VFS function
85        error = vfs_lseek( file_xp , offset , whence , &new_offset );
86
87        if( error )
88        {
89
90#if DEBUG_SYSCALLS_ERROR
91printk("\n[ERROR] in %s : thread[%x,%x] cannot seek file_id %d\n",
92__FUNCTION__, process->pid, this->trdid, file_id );
93#endif
94                this->errno = error;
95                return -1;
96        }
97
98#if (DEBUG_SYS_LSEEK || CONFIG_INSTRUMENTATION_SYSCALLS)
99uint64_t     tm_end = hal_get_cycles();
100#endif
101
102#if DEBUG_SYS_LSEEK
103if( DEBUG_SYS_LSEEK < tm_end )
104printk("\n[%s] thread[%x,%x] exit / cycle %d\n",
105__FUNCTION__ , process->pid, this->trdid, (uint32_t)tm_end );
106#endif
107
108#if CONFIG_INSTRUMENTATION_SYSCALLS
109hal_atomic_add( &syscalls_cumul_cost[SYS_LSEEK] , tm_end - tm_start );
110hal_atomic_add( &syscalls_occurences[SYS_LSEEK] , 1 );
111#endif
112 
113        return new_offset;
114}
Note: See TracBrowser for help on using the repository browser.