免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: triphop
打印 上一主题 下一主题

在kernel module中我可以调用 time 函数吗? [复制链接]

论坛徽章:
0
21 [报告]
发表于 2003-06-19 16:52 |只看该作者

在kernel module中我可以调用 time 函数吗?

原帖由 "triphop" 发表:
那看来我是无法实现那个module 了,因为我还不会将syscall 得来的那个数转化为 yy/mm/dd/hh/mm/ss了!

我没有思路
    去看看那几个C库函数的实现代码吧!不过可以想象必定是非常复杂的。

论坛徽章:
0
22 [报告]
发表于 2003-06-19 16:55 |只看该作者

在kernel module中我可以调用 time 函数吗?

你说libc也是动态link的?

论坛徽章:
0
23 [报告]
发表于 2003-06-19 17:00 |只看该作者

在kernel module中我可以调用 time 函数吗?

是不是动态的取决于你编译的时候用什么选项,如果指定static,就可以静态链接。还有一种办法是把lib文件里的函数单独提取出来成为一个.o文件再把它链接到你自己的程序里。

论坛徽章:
0
24 [报告]
发表于 2003-06-19 17:01 |只看该作者

在kernel module中我可以调用 time 函数吗?

呵呵
/* asctime(tvec))
* where tvec is produced by localtime
* returns a ptr to a character string
* that has the ascii time in the form
*        Thu Jan 01 00:00:00 1970n0\\
*        01234567890123456789012345
*        0          1            2
*
* ctime(t) just calls localtime, then asctime.
*/

#include <time.h>;
#include <sys/types.h>;
#include <sys/timeb.h>;

static        char        cbuf[26];
static        int        dmsize[12] =
{
        31,
        28,
        31,
        30,
        31,
        30,
        31,
        31,
        30,
        31,
        30,
        31
};

/*
* The following table is used for 1974 and 1975 and
* gives the day number of the first day after the Sunday of the
* change.
*/
static struct {
        int        daylb;
        int        dayle;
} daytab[] = {
        5,        333,        /* 1974: Jan 6 - last Sun. in Nov */
        58,        303,        /* 1975: Last Sun. in Feb - last Sun in Oct */
};

struct tm        *gmtime();
char                *ct_numb();
struct tm        *localtime();
char        *ctime();
char        *ct_num();
char        *asctime();

char *
ctime(t)
long *t;
{
        return(asctime(localtime(t)));
}

struct tm *
localtime(tim)
long *tim;
{
        register int dayno;
        register struct tm *ct;
        register daylbegin, daylend;
        long copyt;
        struct timeb systime;

        ftime(&systime);
        copyt = *tim - (long)systime.timezone*60;
        ct = gmtime(&copyt);
        dayno = ct->;tm_yday;
        daylbegin = 119;        /* last Sun in Apr */
        daylend = 303;                /* Last Sun in Oct */
        if (ct->;tm_year==74 || ct->;tm_year==75) {
                daylbegin = daytab[ct->;tm_year-74].daylb;
                daylend = daytab[ct->;tm_year-74].dayle;
        }
        daylbegin = sunday(ct, daylbegin);
        daylend = sunday(ct, daylend);
        if (systime.dstflag &&
            (dayno>;daylbegin || (dayno==daylbegin && ct->;tm_hour>;=2)) &&
            (dayno<daylend || (dayno==daylend && ct->;tm_hour<1))) {
                copyt += 1*60*60;
                ct = gmtime(&copyt);
                ct->;tm_isdst++;
        }
        return(ct);
}

/*
* The argument is a 0-origin day number.
* The value is the day number of the first
* Sunday on or after the day.
*/
static
sunday(t, d)
register struct tm *t;
register int d;
{
        if (d >;= 5
                d += dysize(t->;tm_year) - 365;
        return(d - (d - t->;tm_yday + t->;tm_wday + 700) % 7);
}

struct tm *
gmtime(tim)
long *tim;
{
        register int d0, d1;
        long hms, day;
        register int *tp;
        static struct tm xtime;

        /*
         * break initial number into days
         */
        hms = *tim % 86400;
        day = *tim / 86400;
        if (hms<0) {
                hms += 86400;
                day -= 1;
        }
        tp = (int *)&amp;

        /*
         * generate hours:minutes:seconds
         */
        *tp++ = hms%60;
        d1 = hms/60;
        *tp++ = d1%60;
        d1 /= 60;
        *tp++ = d1;

        /*
         * day is the day number.
         * generate day of the week.
         * The addend is 4 mod 7 (1/1/1970 was Thursday)
         */

        xtime.tm_wday = (day+7340036)%7;

        /*
         * year number
         */
        if (day>;=0) for(d1=70; day >;= dysize(d1); d1++)
                day -= dysize(d1);
        else for (d1=70; day<0; d1--)
                day += dysize(d1-1);
        xtime.tm_year = d1;
        xtime.tm_yday = d0 = day;

        /*
         * generate month
         */

        if (dysize(d1)==366)
                dmsize[1] = 29;
        for(d1=0; d0 >;= dmsize[d1]; d1++)
                d0 -= dmsize[d1];
        dmsize[1] = 28;
        *tp++ = d0+1;
        *tp++ = d1;
        xtime.tm_isdst = 0;
        return(&xtime);
}

char *
asctime(t)
struct tm *t;
{
        register char *cp, *ncp;
        register int *tp;

        cp = cbuf;
        for (ncp = "Day Mon 00 00:00:00 1900\n"; *cp++ = *ncp++;
        ncp = &"SunMonTueWedThuFriSat"[3*t->;tm_wday];
        cp = cbuf;
        *cp++ = *ncp++;
        *cp++ = *ncp++;
        *cp++ = *ncp++;
        cp++;
        tp = &t->;tm_mon;
        ncp = &"JanFebMarAprMayJunJulAugSepOctNovDec"[(*tp)*3];
        *cp++ = *ncp++;
        *cp++ = *ncp++;
        *cp++ = *ncp++;
        cp = ct_numb(cp, *--tp);
        cp = ct_numb(cp, *--tp+100);
        cp = ct_numb(cp, *--tp+100);
        cp = ct_numb(cp, *--tp+100);
        if (t->;tm_year>;=100) {
                cp[1] = '2';
                cp[2] = '0';
        }
        cp += 2;
        cp = ct_numb(cp, t->;tm_year+100);
        return(cbuf);
}

dysize(y)
{
        if((y%4) == 0)
                return(366);
        return(365);
}

static char *
ct_numb(cp, n)
register char *cp;
{
        cp++;
        if (n>;=10)
                *cp++ = (n/10)%10 + '0';
        else
                *cp++ = ' ';
        *cp++ = n%10 + '0';
        return(cp);
}

论坛徽章:
0
25 [报告]
发表于 2003-06-19 17:04 |只看该作者

在kernel module中我可以调用 time 函数吗?

我的初步思路是用相应的syscall 比如 ftime, gettimeofday。。。得到那个最叫我心烦的数值,然后使用下面的code,你说这么做如何?

论坛徽章:
0
26 [报告]
发表于 2003-06-19 17:05 |只看该作者

在kernel module中我可以调用 time 函数吗?

呵呵比想象的简单,不过localtime可能复杂一点吧?与什么时区之类的东东有关

论坛徽章:
0
27 [报告]
发表于 2003-06-19 17:08 |只看该作者

在kernel module中我可以调用 time 函数吗?

不,不要用syscall了,记住你现在是在内核里面了,再去调syscall是危险的,再说syscall需要有进程上下文存在,你的screen saver是不运行在任何进程的上下文里的!

论坛徽章:
0
28 [报告]
发表于 2003-06-19 17:17 |只看该作者

在kernel module中我可以调用 time 函数吗?

localtime在最上面, 我认为既然 ftime()函数原形在/sys/timeb.h下,那也就可以在我的  loadable kernel module 中直接用吧?

论坛徽章:
0
29 [报告]
发表于 2003-06-19 17:22 |只看该作者

在kernel module中我可以调用 time 函数吗?

这样说好象理由不太充分。我是想如果那些库函数直接能在user层搞定的话就应该可以搬到module里来用。/sys下声明的函数很多都是要调用系统调用来实现的吧?那就不行。得具体分析。

论坛徽章:
0
30 [报告]
发表于 2003-06-19 17:27 |只看该作者

在kernel module中我可以调用 time 函数吗?

我的console screensaver 就是个 要放到/module下的加载模块呀,也就是说当它被 kldload到kernel中后,它也就成为kernel的一部分了呀?
既然kernel可syscall,那我的module也应该可以syscall呀?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP