����12-17 linux/fs/ioctl.c


  1 /*

  2  *  linux/fs/ioctl.c

  3  *

  4  *  (C) 1991  Linus Torvalds

  5  */

  6

  7 #include <string.h>       // �ַ���ͷ�ļ�����Ҫ������һЩ�й��ַ���������Ƕ�뺯����

  8 #include <errno.h>        // �����ͷ�ļ�������ϵͳ�и��ֳ����š�

  9 #include <sys/stat.h>     // �ļ�״̬ͷ�ļ��������ļ�״̬�ṹstat{}�ͳ�����

 10

 11 #include <linux/sched.h>  // ���ȳ���ͷ�ļ�������������ṹtask_struct������0���ݵȡ�

 12

 13 extern int tty_ioctl(int dev, int cmd, int arg);    // chr_drv/tty_ioctl.c����133�С�

 14 extern int pipe_ioctl(struct m_inode *pino, int cmd, int arg);  // fs/pipe.c����118�С�

 15

    // ���������������(ioctl)����ָ�����͡�

 16 typedef int (*ioctl_ptr)(int dev,int cmd,int arg);

 17

    // ȡϵͳ���豸�����ĺꡣ

 18 #define NRDEVS ((sizeof (ioctl_table))/(sizeof (ioctl_ptr)))

 19

    // ioctl��������ָ�����

 20 static ioctl_ptr ioctl_table[]={

 21         NULL,           /* nodev */

 22         NULL,           /* /dev/mem */

 23         NULL,           /* /dev/fd */

 24         NULL,           /* /dev/hd */

 25         tty_ioctl,      /* /dev/ttyx */

 26         tty_ioctl,      /* /dev/tty */

 27         NULL,           /* /dev/lp */

 28         NULL};          /* named pipes */

 29        

 30

    //// ϵͳ���ú��� - ����������ƺ�����

    // �ú��������жϲ����������ļ��������Ƿ���Ч��Ȼ����ݶ�Ӧi�ڵ����ļ������ж��ļ�

    // ���ͣ������ݾ����ļ����͵�����صĴ���������

    // ������fd - �ļ���������cmd - �����룻arg - ������

    // ���أ��ɹ��򷵻�0�����򷵻س����롣

 31 int sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)

 32 {      

 33         struct file * filp;

 34         int dev,mode;

 35

    // �����жϸ������ļ�����������Ч�ԡ�����ļ������������ɴ򿪵��ļ��������߶�Ӧ����

    // �����ļ��ṹָ��Ϊ�գ��򷵻س������˳���

 36         if (fd >= NR_OPEN || !(filp = current->filp[fd]))

 37                 return -EBADF;

    // ����ļ��ṹ��Ӧ���ǹܵ�i�ڵ㣬����ݽ����Ƿ���Ȩ�����ùܵ�ȷ���Ƿ�ִ�йܵ�IO

    // ���Ʋ���������Ȩִ�������pipe_ioctl()�����򷵻���Ч�ļ������롣

 38         if (filp->f_inode->i_pipe)

 39                 return (filp->f_mode&1)?pipe_ioctl(filp->f_inode,cmd,arg):-EBADF;

    // �������������ļ���ȡ��Ӧ�ļ������ԣ����ݴ��ж��ļ������͡�������ļ��Ȳ����ַ���

    // ���ļ���Ҳ���ǿ��豸�ļ����򷵻س������˳��������ַ�����豸�ļ�������ļ���i��

    // ����ȡ�豸�š�����豸�Ŵ���ϵͳ���е��豸�����򷵻س����š�

 40         mode=filp->f_inode->i_mode;

 41         if (!S_ISCHR(mode) && !S_ISBLK(mode))

 42                 return -EINVAL;

 43         dev = filp->f_inode->i_zone[0];

 44         if (MAJOR(dev) >= NRDEVS)

 45                 return -ENODEV;

    // Ȼ�����IO���Ʊ�ioctl_table��ö�Ӧ�豸��ioctl����ָ�룬�����øú������������

    // ����ioctl����ָ�����û�ж�Ӧ�������򷵻س����롣

 46         if (!ioctl_table[MAJOR(dev)])

 47                 return -ENOTTY;

 48         return ioctl_table[MAJOR(dev)](dev,cmd,arg);

 49 }

 50