C/C++教程

kernel源码(十五)sys.c

本文主要是介绍kernel源码(十五)sys.c,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

该源码包含了很多系统调用的函数实现

源码

/*
 *  linux/kernel/sys.c
 *
 *  (C) 1991  Linus Torvalds
 */

#include <errno.h>

#include <linux/sched.h>
#include <linux/tty.h>
#include <linux/kernel.h>
#include <asm/segment.h>
#include <sys/times.h>
#include <sys/utsname.h>

int sys_ftime()
{
    return -ENOSYS;
}

int sys_break()
{
    return -ENOSYS;
}

int sys_ptrace()
{
    return -ENOSYS;
}

int sys_stty()
{
    return -ENOSYS;
}

int sys_gtty()
{
    return -ENOSYS;
}

int sys_rename()
{
    return -ENOSYS;
}

int sys_prof()
{
    return -ENOSYS;
}

int sys_setregid(int rgid, int egid)
{
    if (rgid>0) {
        if ((current->gid == rgid) || 
            suser())
            current->gid = rgid;
        else
            return(-EPERM);
    }
    if (egid>0) {
        if ((current->gid == egid) ||
            (current->egid == egid) ||
            (current->sgid == egid) ||
            suser())
            current->egid = egid;
        else
            return(-EPERM);
    }
    return 0;
}

int sys_setgid(int gid)
{
    return(sys_setregid(gid, gid));
}

int sys_acct()
{
    return -ENOSYS;
}

int sys_phys()
{
    return -ENOSYS;
}

int sys_lock()
{
    return -ENOSYS;
}

int sys_mpx()
{
    return -ENOSYS;
}

int sys_ulimit()
{
    return -ENOSYS;
}

int sys_time(long * tloc)
{
    int i;

    i = CURRENT_TIME;
    if (tloc) {
        verify_area(tloc,4);
        put_fs_long(i,(unsigned long *)tloc);
    }
    return i;
}

/*
 * Unprivileged users may change the real user id to the effective uid
 * or vice versa.
 */
int sys_setreuid(int ruid, int euid)
{
    int old_ruid = current->uid;
    
    if (ruid>0) {
        if ((current->euid==ruid) ||
                    (old_ruid == ruid) ||
            suser())
            current->uid = ruid;
        else
            return(-EPERM);
    }
    if (euid>0) {
        if ((old_ruid == euid) ||
                    (current->euid == euid) ||
            suser())
            current->euid = euid;
        else {
            current->uid = old_ruid;
            return(-EPERM);
        }
    }
    return 0;
}

int sys_setuid(int uid)
{
    return(sys_setreuid(uid, uid));
}

int sys_stime(long * tptr)
{
    if (!suser())
        return -EPERM;
    startup_time = get_fs_long((unsigned long *)tptr) - jiffies/HZ;
    return 0;
}

int sys_times(struct tms * tbuf)
{
    if (tbuf) {
        verify_area(tbuf,sizeof *tbuf);
        put_fs_long(current->utime,(unsigned long *)&tbuf->tms_utime);
        put_fs_long(current->stime,(unsigned long *)&tbuf->tms_stime);
        put_fs_long(current->cutime,(unsigned long *)&tbuf->tms_cutime);
        put_fs_long(current->cstime,(unsigned long *)&tbuf->tms_cstime);
    }
    return jiffies;
}

int sys_brk(unsigned long end_data_seg)
{
    if (end_data_seg >= current->end_code &&
        end_data_seg < current->start_stack - 16384)
        current->brk = end_data_seg;
    return current->brk;
}

/*
 * This needs some heave checking ...
 * I just haven't get the stomach for it. I also don't fully
 * understand sessions/pgrp etc. Let somebody who does explain it.
 */
int sys_setpgid(int pid, int pgid)
{
    int i;

    if (!pid)
        pid = current->pid;
    if (!pgid)
        pgid = current->pid;
    for (i=0 ; i<NR_TASKS ; i++)
        if (task[i] && task[i]->pid==pid) {
            if (task[i]->leader)
                return -EPERM;
            if (task[i]->session != current->session)
                return -EPERM;
            task[i]->pgrp = pgid;
            return 0;
        }
    return -ESRCH;
}

int sys_getpgrp(void)
{
    return current->pgrp;
}

int sys_setsid(void)
{
    if (current->leader && !suser())
        return -EPERM;
    current->leader = 1;
    current->session = current->pgrp = current->pid;
    current->tty = -1;
    return current->pgrp;
}

int sys_uname(struct utsname * name)
{
    static struct utsname thisname = {
        "linux .0","nodename","release ","version ","machine "
    };
    int i;

    if (!name) return -ERROR;
    verify_area(name,sizeof *name);
    for(i=0;i<sizeof *name;i++)
        put_fs_byte(((char *) &thisname)[i],i+(char *) name);
    return 0;
}

int sys_umask(int mask)
{
    int old = current->umask;

    current->umask = mask & 0777;
    return (old);
}
View Code

下面这些函数都是0.11内核当中没有实现的函数,都返回-ENOSYS

int sys_ftime()
{
    return -ENOSYS;
}

int sys_break()
{
    return -ENOSYS;
}

int sys_ptrace()
{
    return -ENOSYS;
}

int sys_stty()
{
    return -ENOSYS;
}

int sys_gtty()
{
    return -ENOSYS;
}

int sys_rename()
{
    return -ENOSYS;
}

int sys_prof()
{
    return -ENOSYS;
}

设置当前任务实际的组id以及有效的组id

int sys_setregid(int rgid, int egid)
{
    if (rgid>0) { //实际的组id
        if ((current->gid == rgid) || 
            suser()) //如果当前任务的组id==rgid 或者是超级用户,则设置rgid
            current->gid = rgid;
        else
            return(-EPERM);
    }
    if (egid>0) { 
        if ((current->gid == egid) ||
            (current->egid == egid) ||
            (current->sgid == egid) ||
            suser()) //如果当前任务的组id==egid或者当前任务的egid==egid或者当前任务的sgid==egid或者是超级用户
            current->egid = egid; //则设置当前任务的egid=egid 
        else
            return(-EPERM);
    }
    return 0;
}

设置进程的组号

int sys_setgid(int gid)
{
    return(sys_setregid(gid, gid));
}

返回1970年1月1日以来的时间值

int sys_time(long * tloc)
{
    int i;

    i = CURRENT_TIME;
    if (tloc) {
        verify_area(tloc,4);
        put_fs_long(i,(unsigned long *)tloc);
    }
    return i;
}

设置任务实际的用户ID或者有效的用户ID

int sys_setreuid(int ruid, int euid)
{
    int old_ruid = current->uid;
    
    if (ruid>0) { //实际的用户id
        if ((current->euid==ruid) ||
                    (old_ruid == ruid) ||
            suser())
            current->uid = ruid;
        else
            return(-EPERM);
    }
    if (euid>0) { //有效的用户id
        if ((old_ruid == euid) ||
                    (current->euid == euid) ||
            suser())
            current->euid = euid;
        else {
            current->uid = old_ruid;
            return(-EPERM);
        }
    }
    return 0;
}

设置用户的id

int sys_setuid(int uid)
{
    return(sys_setreuid(uid, uid));
}

设置系统的开机时间

int sys_stime(long * tptr)
{
    if (!suser())
        return -EPERM;
    startup_time = get_fs_long((unsigned long *)tptr) - jiffies/HZ;
    return 0;
}

获取当前任务运行时间

int sys_times(struct tms * tbuf)
{
    if (tbuf) {
        verify_area(tbuf,sizeof *tbuf);
        put_fs_long(current->utime,(unsigned long *)&tbuf->tms_utime);
        put_fs_long(current->stime,(unsigned long *)&tbuf->tms_stime);
        put_fs_long(current->cutime,(unsigned long *)&tbuf->tms_cutime);
        put_fs_long(current->cstime,(unsigned long *)&tbuf->tms_cstime);
    }
    return jiffies;
}

 

int sys_brk(unsigned long end_data_seg)
{
    if (end_data_seg >= current->end_code &&
        end_data_seg < current->start_stack - 16384)
        current->brk = end_data_seg;
    return current->brk;
}

设置进程号pid和进程组号pgid

int sys_setpgid(int pid, int pgid)
{
    int i;

    if (!pid)
        pid = current->pid;
    if (!pgid)
        pgid = current->pid;
    for (i=0 ; i<NR_TASKS ; i++)
        if (task[i] && task[i]->pid==pid) {
            if (task[i]->leader)
                return -EPERM;
            if (task[i]->session != current->session)
                return -EPERM;
            task[i]->pgrp = pgid;
            return 0;
        }
    return -ESRCH;
}

返回当前进程的组号

int sys_getpgrp(void)
{
    return current->pgrp;
}

创建一个会话,并且设置会话号=组号

int sys_setsid(void)
{
    if (current->leader && !suser())
        return -EPERM;
    current->leader = 1;
    current->session = current->pgrp = current->pid;
    current->tty = -1; //设置当前进程没有控制终端
    return current->pgrp;
}

获取系统名称的一些信息

int sys_uname(struct utsname * name)
{
    static struct utsname thisname = {
        "linux .0","nodename","release ","version ","machine "
    };
    int i;

    if (!name) return -ERROR;
    verify_area(name,sizeof *name);
    for(i=0;i<sizeof *name;i++)
        put_fs_byte(((char *) &thisname)[i],i+(char *) name);
    return 0;
}

设置当前进程创建文件的属性

int sys_umask(int mask)
{
    int old = current->umask;

    current->umask = mask & 0777;
    return (old);
}

 

这篇关于kernel源码(十五)sys.c的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!