source: trunk/kernel/syscalls/sys_isatty.c @ 566

Last change on this file since 566 was 566, checked in by alain, 5 years ago

Complete restructuration of kernel locks.

File size: 3.4 KB
Line 
1/*
2 * sys_isatty.c - test if a file descriptor is a TXT device.
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#include <kernel_config.h>
25#include <hal_kernel_types.h>
26#include <hal_uspace.h>
27#include <hal_irqmask.h>
28#include <hal_special.h>
29#include <errno.h>
30#include <vfs.h>
31#include <vmm.h>
32#include <chdev.h>
33#include <thread.h>
34#include <printk.h>
35#include <process.h>
36
37#include <syscalls.h>
38
39////////////////////////////////////
40int sys_isatty( uint32_t   file_id )
41{
42        xptr_t    file_xp;     // extended pointer on remote file descriptor
43    xptr_t    chdev_xp;    // extended pointer on remote chdev
44    cxy_t     chdev_cxy;
45    chdev_t * chdev_ptr;
46    uint32_t  chdev_func;
47    int       retval;
48
49        thread_t  *  this    = CURRENT_THREAD;
50        process_t *  process = this->process;
51 
52#if DEBUG_SYS_ISATTY
53uint64_t tm_start;
54uint64_t tm_end;
55tm_start = hal_get_cycles();
56if( DEBUG_SYS_ISATTY < tm_start )
57printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n"
58__FUNCTION__ , this, process->pid, (uint32_t)tm_start );
59#endif
60
61    // check file_id argument
62        if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
63        {
64
65#if DEBUG_SYSCALLS_ERROR
66printk("\n[ERROR] in %s : illegal file descriptor index = %d\n", __FUNCTION__ , file_id );
67#endif
68                this->errno = EBADFD;
69                return -1;
70        }
71
72    // get extended pointer on remote file descriptor
73    file_xp = process_fd_get_xptr( process , file_id );
74
75    if( file_xp == XPTR_NULL )
76    {
77
78#if DEBUG_SYSCALLS_ERROR
79printk("\n[ERROR] in %s : undefined fd_id %d in process %x\n",
80__FUNCTION__ , file_id , process->pid );
81#endif
82        this->errno = EBADFD;
83        return -1;
84    }
85
86    // get file descriptor cluster and local pointer
87    vfs_file_t * file_ptr = GET_PTR( file_xp );
88    cxy_t        file_cxy = GET_CXY( file_xp );
89
90    // get file type
91    vfs_inode_type_t type = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ) );
92
93    // action depend on file type
94    if( type != INODE_TYPE_DEV )      // not a device
95    {
96        retval = 0;
97        }
98    else                              // it is a device
99    {
100        // get cluster and pointers on chdev
101        chdev_xp  = chdev_from_file( file_xp );
102        chdev_cxy = GET_CXY( chdev_xp );
103        chdev_ptr = GET_PTR( chdev_xp );
104
105        // get chdev type
106        chdev_func = hal_remote_l32( XPTR( chdev_cxy , &chdev_ptr->func ) );
107
108        if( chdev_func == DEV_FUNC_TXT ) retval = 1;
109        else                               retval = 0;
110    }
111
112#if DEBUG_SYS_ISATTY
113tm_end = hal_get_cycles();
114if( DEBUG_SYS_ISATTY < tm_end )
115printk("\n[DBG] %s : thread %x exit / process %x / cost %d / cycle %d\n",
116__FUNCTION__, this, process->pid, (uint32_t)(tm_end - tm_start), (uint32_t)tm_end );
117#endif
118
119        return retval;
120
121}  // end sys_read()
Note: See TracBrowser for help on using the repository browser.