页面载入中

 
     
 
      placard
页面载入中
      calendar
<<  < 2007 - 11 >  >>
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
      comment
页面载入中
      newblog
页面载入中
      newmessage
页面载入中
      search

 

      login
页面载入中
      link
      info
页面载入中


 
 
页面载入中
   
 
 
[随笔]NULL
[ 2007-11-27 12:25:00 | By: Myth.asm ]
 
current->signal->tty->driver->write();

最近有些失落的感觉
计划不写blog了(奇怪 那我为什么还想打这些无聊的字呢?)

 
 
 
[随笔]重定向
[ 2007-11-11 11:03:00 | By: Myth.asm ]
 
[learn from 《APUE》]
一个小技巧~:
make > err 2>&1 > out
解释一下:
> err 把当前的标准输出 stdout 定向到文件 err
2>&1 把标准错误 stderr 定向到标准输出 stdout,也就是文件 err
> out 再一次把标准输出 stdout 定向到文件 out
------------------------------------------------------
QMD:一连两天没自习了~

 
 
 
[随笔]Linux-GDT
[ 2007-11-9 16:35:00 | By: Myth.asm ]
 

刚刚翻了一下在几个月前豁然开朗后写的几个关于保护模式的程序~
突然发现现在把它们翻译到linux下居然是这么的容易啊~
只改了两处:
(1) __asm("sgdt %0":"=m"(gdt_info));
(2) printk

me.c:

#i nclude <linux/init.h>
#i nclude <linux/module.h>

#i nclude "me.h"

MODULE_LICENSE("GPL");

int listGDT(void)
{
    GDTINFO gdt_info;
    GDTENTRY *gdt_entries, *c_entry;
    DWORD gdt, gdt_cnt;
    DWORD base, limit, S, DPL, P, AVL, L, D, G, type;
    char *t_msg = 0, *cd_msg = 0;
 
    // Segment Descriptors
    static char * sd_type[] = {"Reserved", "Code16", "Data16", "Code32", "Data32"};
 
    // System-Segment and Gate-Descriptor Types
    static char * ss_gd_type[] =
    {
        "Reserved", "AvailTSS16", "LDT", "BusyTSS16", "CallG16", "TaskG16",
        "IntG16",    "TrapG16", "Reserved", "AvailTSS32", "Reserved",
        "BusyTSS32", "CallG32", "Reserved","IntG32", "TrapG32",
    };
 
    __asm("sgdt %0":"=m"(gdt_info));
    gdt_entries = (GDTENTRY *)((gdt_info.hi_base<<16) | gdt_info.lo_base);
    gdt_cnt = (DWORD)((gdt_info.limit+1)>>3);
 
    for(gdt=0; gdt<gdt_cnt; gdt++)
    {
        c_entry = &(gdt_entries[gdt]);
 
        base = GETBASE(c_entry);
        limit = GETLIMIT(c_entry);
        S = c_entry->S;
        DPL = c_entry->DPL;
        P = c_entry->P;
        AVL = c_entry->AVL;
        L = c_entry->L;
        D = c_entry->D;
        G = c_entry->G;
        type = c_entry->type;
 
        if(P)
        {
            if(S)
            {
                if(D && (type & 0x8))
                    t_msg = sd_type[3];
                else if(D && !(type & 0x8))
                    t_msg = sd_type[4];
                else if(!D && (type & 0x8))
                    t_msg = sd_type[1];
                else
                    t_msg = sd_type[2];
 
                if(G)
                    limit = ((limit+1)<<12)-1;
            }
            else
            {
                t_msg = ss_gd_type[type];
            }
        }
        else
        {
            t_msg = sd_type[0];
        }
        printk(KERN_ALERT "%04x   %-12s Base:%08x   Limit:%08x   DPL:%d  P:%d  L:%d  type:%d%d%d%d\n", (gdt<<3) | DPL, t_msg, base, limit, DPL, P, L, (type & 0x8) && 1, (type & 0x4) && 1, (type & 0x2) && 1, (type & 0x1) && 1);
    }

    return 0;
}

static int __init hello_init(void)
{
    listGDT();

    return 0;
}

static void  __exit hello_exit(void)
{
}

module_init(hello_init);
module_exit(hello_exit);

me.h:

#ifndef _GDT_H_
#define _GDT_H_

typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;

typedef struct _GDTINFO
{
    WORD limit;
    WORD lo_base;
    WORD hi_base;
}GDTINFO;

typedef struct _GDTENTRY
{
    WORD lo_limit;
    WORD lo_base;
    BYTE mi_base;

    BYTE type:4;
    BYTE S:1;
    BYTE DPL:2;
    BYTE P:1;
    BYTE hi_limit:4;
    BYTE AVL:1;
    BYTE L:1;
    BYTE D:1;
    BYTE G:1;
    BYTE hi_base;
}GDTENTRY;

#define GETBASE(seg) ((seg->hi_base<<24) | (seg->mi_base<<16) | seg->lo_base)
#define GETLIMIT(seg) ((seg->hi_limit<<16) | seg->lo_limit)

#endif

Makefile:
 
obj-m := me.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
Default:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
    @echo insmod me.ko to love me
    @echo then rmmod me to leave me
clean:
    rm -rf me.o me.mod.o me.mod.c Module.symvers me.ko .Makefile.swp .me.ko.cmd .me.mod.o.cmd .me.o.cmd .tmp_versions




 
 
 
[随笔][zz] Let the kernel tell us what we need
[ 2007-11-6 15:27:00 | By: Myth.asm ]
 
[zz from book:<linux kernel in a nutshell>]
Let the kernel tell us what we need
Now that we have gone through all of the steps of poking around in sysfs and
following symlinks to module names, here is a very simple script that will do all of
that work, in a different way:
#!/bin/bash
#
# find_all_modules.sh
#
for i in `find /sys/ -name modalias -exec cat {} \;`; do
/sbin/modprobe --config /dev/null --show-depends $i ;
done | rev | cut -f 1 -d '/' | rev | sort -u
You can download an example file containing this script from the book’s web site,
provided in the “How to Contact Us” section of the Preface.
This script goes through sysfs and finds all files called modalias.The modalias file
contains the module alias that tells the modprobe command which module should
be loaded to control this device.The module alias is made up of a combination of
device manufacturer, ID, class type, and other unique identifiers for that specific
type of device.All kernel driver modules have an internal list of devices that they
support that is generated automatically by the list of devices the driver tells the
kernel it supports.The modprobe looks through this list of devices by all drivers
and tries to match it up with the alias it has.If it finds a match, it will then load
the module (this procedure is how the automatic driver loading functionality in
Linux works).
The script has the modprobe program stop before actually loading the module,
and just print out what actions it would take.This gives us a list of all of the
modules that are needed to control all devices in the system.A little cleaning up of
the list, by sorting it and finding the proper field to display, results in this output:
$ find_all_modules.sh
8139cp.ko
8139too.ko
ehci-hcd.ko
firmware_class.ko
i2c-i801.ko
ieee80211.ko
ieee80211_crypt.ko
ipw2200.ko
mii.ko
mmc_core.ko
pcmcia_core.ko
rsrc_nonstatic.ko
sdhci.ko
snd-hda-codec.ko
snd-hda-intel.ko
snd-page-alloc.ko
snd-pcm.ko
snd-timer.ko
snd.ko
soundcore.ko
uhci-hcd.ko
usbcore.ko
yenta_socket.ko
This is a list of all of the modules that are needed to control the hardware in the
machine.
The script will also probably print out some error messages that look like:
FATAL: Module pci:v00008086d00002592sv000010CFsd000012E2bc03sc00i00 not
found.
FATAL: Module serio:ty01pr00id00ex00 not found.
Which means that it could not find a module that can control that device.Do not
be concerned about this, as some devices do not have kernel drivers that will work
for them.
------------------------------------------------------
QMD:转载真容易~ctrl-a  ctrl-c ctrl-v~
 
 
 
[随笔]做一个广告~
[ 2007-10-8 18:01:00 | By: Myth.asm ]
 

那就是:http://acc.yeemu.com/index.htm

亿目加速器~

用着很方便,界面也相当pp~

尽管现在也收费了 不过每天还有1.5个小时的免费时间 浏览网页还是很不错的~

 
 
 
[随笔]续上次
[ 2007-10-6 21:04:00 | By: Myth.asm ]
 
在家吃完回来了,应该能胖了一些吧~
觉得有好多想写的,但现在突然全忘了~
等想起来再写吧~
------------------------------------------------------
QMD:葡萄香 月饼香 ....
 
 
 
[随笔][zz]关于Linux静态和动态链接库的创建及使用
[ 2007-9-29 18:57:00 | By: Myth.asm ]
 

[zz from http://www.xker.com/page/e2007/0928/35122.html]

和Windows系统一样Linux也有静态/动态链接库,下面介绍创建和使用方法:

假设有下面几个文件:

头文件String.h,声明相关函数原形,内容如下:

Strlen.c:函数Strlen的实现,获取给定字符串的长度,内容如下:

Strlnen.c:函数StrNlen的实现,获取给定字符串的长度,如果输入字符串的长度大于指定的最大长度,则返回最大长度,否者返回字符串的实际长度,内容如下:

生成静态库:

利用GCC生成对应目标文件:

gcc –c Strlen.c Strnlen.c

如果对应的文件没有错误,gcc会对文件进行编译生成Strlen.o和Strnlen.o两个目标文件(相当于windows下的obj文件)。然后用ar创建一个名字为libstr.a的库文件,并把Strlen.o 和Strnlen.o的内容插入到对应的库文件中。,相关命令如下:

ar –rc libstr.a Strlen.o Strnlen.o

命令执行成功以后,对应的静态库libstr.a已经成功生成。

/***********************************

Filename : String.h

Description :

Author   : HCJ

Date     : 2006-5-7

************************************/

int Strlen(char *pStr);

int StrNlen(char *pStr, unsigned long ulMaxLen);

/**************************************

Filename    : get string length

Description  : 

Author      : HCJ

Date        : 2006/5/7

**************************************/

#i nclude<stdio.h>

#i nclude<assert.h>

int Strlen(char *pStr)

{

    unsigned long ulLength;

    assert(NULL != pStr);

 

    ulLength = 0;

    while(*pStr++)

    {

        ulLength++;

    }

 

    return ulLength;

}

**********************************************

Fileneme: mystrnlen.c

Description: get input string length,if string large

             max length input return max length,

             else real length

Author: HCJ

Date  : 2006-5-7

**********************************************/

#i nclude<stdio.h>

#i nclude<assert.h>

int StrNlen(char *pStr, unsigned long ulMaxLen)

{

    unsigned long ulLength;

 

    assert(NULL != pStr);

 

    if(ulMaxLen <= 0)

    {

        printf("Wrong Max Length!\n");

        return -1;

    }

 

    ulLength = 0;

    while(*pStr++ &&  ulLength < ulMaxLen)

    {

        ulLength++;

    }

 

    return ulLength;

}

生成动态链接库:

gcc -fpic -shared -o libstr.so Strlen.c Strnlen.c

-fpic 使输出的对象模块是按照可重定位地址方式生成的。

-shared指定把对应的源文件生成对应的动态链接库文件libstr.so文件。

对应的链接库已经生成,下面看一下如何使用对应的链接库。

静态库的使用:

假设有下面的文件要使用对应的的静态库:

编译生成对应的目标文件:

gcc -c -I/home/hcj/xxxxxxxx main.c

生成可执行文件:

gcc -o main1 -L/home/hcj/xxxxxxxx main.o libstr.a

其中-I/home/hcj/xxxxxxxx和-L/home/hcj/xxxxxxxx是通过-I和-L指定对应的头文件和库文件的路径。libstr.a是对应的静态库的名称。这样对应的静态库已经编译到对应的可执行程序中。执行对应的可执行文件便可以对应得函数调用的结果。

/*****************************************

FileName: main.c

Description: test static/dynamic library

Author: HCJ

Date  : 2005-5-7

******************************************/

#i nclude<stdio.h>

#i nclude <String.h>   //静态库对应函数的头文件

 

int main(int argc, char* argv[])

{

    char str[] = {"hello world"};

    unsigned long ulLength = 0;

 

    printf("The string is : %s\n", str);

    ulLength = Strlen(str);

    printf("The string length is : %d(use Strlen)\n", ulLength);

    ulLength = StrNlen(str, 10);

    printf("The string length is : %d(use StrNlen)\n", ulLength);

 

    return 0;

}

动态库的分为隐式调用和显式调用两种调用方法:

隐式调用的使用使用方法和静态库的调用差不多,具体方法如下:

gcc -c -I/home/hcj/xxxxxxxx main.c

gcc -o main1 -L/home/hcj/xxxxxxxx main.o libstr.so //这里是*.so

在这种调用方式中,需要维护动态链接库的配置文件/etc/ld.so.conf来让动态链接库为系统所使用,通常将动态链接库所在目录名追加到动态链接库配置文件中。否则在执行相关的可执行文件的时候就会出现载入动态链接库失败的现象。在编译所引用的动态库时,可以在gcc采用 –l或-L选项或直接引用所需的动态链接库方式进行编译。在Linux里面,可以采用ldd命令来检查程序依赖共享库。

显式调用:

/*****************************************

FileName: main2.c

Description: test static/dynamic library

Author: HCJ

Date  : 2005-5-7

******************************************/

#i nclude<stdio.h>

#i nclude<dlfcn.h>

 

int main(int argc, char* argv[])

{

    //define function pointor

    int (*pStrlenFun)(char* pStr);     //声明对应的函数的函数指针

    int (*pStrnlenFun)(char* pStr, int ulMaxLen);

 

    char str[] = {"hello world"};

    unsigned long ulLength = 0;

 

    void *pdlHandle;

    char *pszErr;

 

    pdlHandle = dlopen("./libstr.so", RTLD_LAZY);  //加载链接库/libstr.so

    if(!pdlHandle)

    {

        printf("Failed load library\n");

    }

    pszErr = dlerror();

    if(pszErr != NULL)

    {

        printf("%s\n", pszErr);

        return 0;

    }

 

    //get function from lib

    pStrlenFun = dlsym(pdlHandle, "Strlen"); //获取函数的地址

    pszErr = dlerror();

    if(pszErr != NULL)

    {

        printf("%s\n", pszErr);

        return 0;

    }

 

    pStrnlenFun = dlsym(pdlHandle, "StrNlen");

    pszErr = dlerror();

    if(pszErr != NULL)

    {

        printf("%s\n", pszErr);

        return 0;

    }

 

    printf("The string is : %s\n", str);

    ulLength = pStrlenFun(str);   //调用相关的函数

    printf("The string length is : %d(use Strlen)\n", ulLength);

    ulLength = pStrnlenFun(str, 10);

    printf("The string length is : %d(use StrNlen)\n", ulLength);

 dlclose(pdlHandle);

    return 0;

}

gcc -o mian2 -ldl main2.c

用gcc编译对应的源文件生成可执行文件,-ldl选项,表示生成的对象模块需要使用共享库。执行对应得文件同样可以得到正确的结果。

相关函数的说明如下:

(1)dlopen()

第一个参数:指定共享库的名称,将会在下面位置查找指定的共享库。

-环境变量LD_LIBRARY_PATH列出的用分号间隔的所有目录。

-文件/etc/ld.so.cache中找到的库的列表,用ldconfig维护。

-目录usr/lib。

-目录/lib。

-当前目录。

第二个参数:指定如何打开共享库。

-RTLD_NOW:将共享库中的所有函数加载到内存

-RTLD_LAZY:会推后共享库中的函数的加载操作,直到调用dlsym()时方加载某函数

(2)dlsym()

调用dlsym时,利用dlopen()返回的共享库的phandle以及函数名称作为参数,返回要加载函数的入口地址。

(3)dlerror()

该函数用于检查调用共享库的相关函数出现的错误。

这样我们就用简单的例子说明了在Linux下静态/动态库的创建和使用。

(责任编辑:云子)

 
 
 
[随笔]打扫 + 计划
[ 2007-9-27 19:54:00 | By: Myth.asm ]
 

(1) 下午理了个发 居然也涨价了 高达10RMB....

(2) 整理了下衣柜 扔了很多东西 反而干净了不少....

(3) 十一开始复习专业课 主要是组成原理吧 算法似乎很容易....

(4) 国庆回家 参加old brother的婚礼啦~~

 
 
 
[随笔]一个疑问
[ 2007-9-24 11:29:00 | By: Myth.asm ]
 

windows xp下正在运行的程序为什么删除不了呢。。

用process explorer看文件句柄里似乎也没打开自己的文件哇,听说win 2k CloseHandle (4)就能关闭自己,但xp不知改成了啥样,既然文件句柄里没,那又是在哪里做了判断了呢。。

------------------------------------------------------
QMD: 尽管linux更有意思,但想到的问题总也得知道哇~
 
 
 
[随笔]Linux文件系统的隐藏 chattr lsattr
[ 2007-9-24 11:11:00 | By: Myth.asm ]
 

[zz from http://www.xker.com/edu/os/5/index_1.html]

对于某些有特殊要求的档案(如服务器日志)还可以追加隐藏权限的设定。这些隐藏权限包括:

Append only (a), compressed (c), no dump (d), immutable (i), data journalling (j),secure deletion (s), no tail-merging (t), undeletable (u), no atime updates (A), synchronous directory updates (D), synchronous updates (S), and top of directory hierarchy (T).


大部分属性在文件系统的安全管理方面起很重要的作用。关于以上属性的详细描述请兄弟们查阅chattr的在线帮助man,注意多数属性须要由root来施加。


通过chattr设置档案的隐藏权限。

[root]#chattr --help

Usage: chattr [-RV] [-+=AacDdijsSu] [-v version] files...


参数或选项描述:

-R:递归处理,将指定目录下的所有文件及子目录一并处理。

-V:显示详细过程有版本编号。

-v:设定文件或目录版本(version)。


+ :在原有参数设定基础上,追加参数。

- :在原有参数设定基础上,移除参数。

= :更新为指定参数设定。


A:文件或目录的 atime (access time)不可被修改(modified), 可以有效预防例如手提电脑磁盘I/O错误的发生。

S:硬盘I/O同步选项,功能类似sync。

a:即append,设定该参数后,只能向文件中添加数据,而不能删除,多用于服务器日志文 件安全,只有root才能设定这个属性。

c:即compresse,设定文件是否经压缩后再存储。读取时需要经过自动解压操作。

d:即no dump,设定文件不能成为dump程序的备份目标。

i:设定文件不能被删除、改名、设定链接关系,同时不能写入或新增内容。i参数对于文件 系统的安全设置有很大帮助。

j:即journal,设定此参数使得当通过mount参数:data=ordered 或者 data=writeback 挂 载的文件系统,文件在写入时会先被记录(在journal中)。如果filesystem被设定参数为 data=journal,则该参数自动失效。

s:保密性地删除文件或目录,即硬盘空间被全部收回。

u:与s相反,当设定为u时,数据内容其实还存在磁盘中,可以用于undeletion.


各参数选项中常用到的是a和i。a选项强制只可添加不可删除,多用于日志系统的安全设定。而i是更为严格的安全设定,只有superuser (root) 或具有CAP_LINUX_IMMUTABLE处理能力(标识)的进程能够施加该选项。我们来举一个例子:


[root]#touch chattr_test

[root]#chattr +i chattr_test

[root]#rm chattr_test

rm: remove write-protected regular empty file `chattr_test`? y

rm: cannot remove `chattr_test`: Operation not permitted


呵,此时连root本身都不能直接进行删除操作,必须先去除i设置后再删除。


chattr命令的在线帮助详细描述了各参数选项的适用范围及bug提示,使用时建议兄弟们仔细查阅。由于上述的这些属性是隐藏的,查看时需要使用lsattr命令,以下简述之。


lsattr命令格式:

[root]#lsattr [-RVadlv] [files...]


参数或选项说明:

-R:递归列示目录及文件属性。

-V:显示程序版本号。

-a:显示所有文件属性,包括隐藏文件(.)、当时目录(./)及上层目录(../)。

-d:仅列示目录属性。

-l:(此参数目前没有任何作用)。

-v:显示文件或目录版本。


例:

[root]#chattr +aij lsattr_test

[root]#lsattr

----ia---j--- ./lsattr_test

关于lsattr的用法,详情请参阅在线帮助man。

(责任编辑:云子)

------------------------------------------------------
QMD: +i 居然要root权限...
 
 
首页 上一页 下一页 尾页 页次:1/8页  10篇日志/页 转到:
 
     
   
     
Powered by Oblog.