Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Saturday, May 23, 2009

d-bus

d-bus  资料在这里有很多.
使用示例可用文档: Using the D-Bus C API(http://dbus.freedesktop.org/doc/dbus/libdbus-tutorial.html)所附的dbus-example.c.
编译如下:
gcc dbus-example.c -o dbus-example `pkg-config --cflags --libs dbus-1`

pkg-config

pkg-config 可用来取得使用某个库来进行开发时所需的编译参数. 如:
[dybbuk@localhost tmp]$ pkg-config --cflags --libs dbus-1
-I/usr/include/dbus-1.0 -I/usr/lib/dbus-1.0/include  -L/lib -ldbus-1 

库的名字必須跟/usr/lib/pkgconfig 相应的.pc文件的名字一样.  否则就无法取得.

Thursday, February 12, 2009

检查根文件系统是否为网络文件系统

追踪网络问题时,看到这么一段bash脚本,发现应该很有用.

      # Don't shut the network down if root is on NFS or a network
    # block device.
        rootfs=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $3; }}' /etc/mtab)
        rootopts=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' /etc/mtab)
   
    if [[ "$rootfs" =~ ^nfs ]] || [[ "$rootopts" =~ "_netdev|_rnetdev" ]] ; then
        exit 1
    fi

关于/etc/mtab 的作用,可以看busybox关于mount命令是否支持/etc/mtab这个配置选项的描述:
config FEATURE_MTAB_SUPPORT
    bool "Support for the old /etc/mtab file"
    default n
    depends on MOUNT || UMOUNT
    select FEATURE_MOUNT_FAKE
    help
      Historically, Unix systems kept track of the currently mounted
      partitions in the file "/etc/mtab". These days, the kernel exports
      the list of currently mounted partitions in "/proc/mounts", rendering
      the old mtab file obsolete. (In modern systems, /etc/mtab should be
      a symlink to /proc/mounts.)

      The only reason to have mount maintain an /etc/mtab file itself is if
      your stripped-down embedded system does not have a /proc directory.
      If you must use this, keep in mind it's inherently brittle (for
      example a mount under chroot won't update it), can't handle modern
      features like separate per-process filesystem namespaces, requires
      that your /etc directory be writeable, tends to get easily confused
      by --bind or --move mounts, won't update if you rename a directory
      that contains a mount point, and so on. (In brief: avoid.)

      About the only reason to use this is if you've removed /proc from
      your kernel.

endmenu


Wednesday, February 11, 2009

printk

printk可以在任何上下文中调用.其有16K(具体数值menuconfig可以配置)的ring buffer. dmesg显示的就是这部分数据.故如果打印过多,dmesg只能查看到最新的打印,无法得到自开机以来全部的内核打印.

printk的信息级别:
#define    KERN_EMERG    "<0>"    /* system is unusable            */
#define    KERN_ALERT    "<1>"    /* action must be taken immediately    */
#define    KERN_CRIT    "<2>"    /* critical conditions            */
#define    KERN_ERR    "<3>"    /* error conditions            */
#define    KERN_WARNING    "<4>"    /* warning conditions            */
#define    KERN_NOTICE    "<5>"    /* normal but significant condition    */
#define    KERN_INFO    "<6>"    /* informational            */
#define    KERN_DEBUG    "<7>"    /* debug-level messages            */

当不指定级别时,kernel会默认成KERN_WARNING级别

printk最后直接调用或间接调用_call_console_drivers把字符输出到console. 只代码中可以看到:
只有msg_log_level小于console_loglevel的信息才会输出.kernel默认console_loglevel为7( 即 KERN_DEBUG, 故KERN_DEBUG的信息不会输出到console).
/*
 * Write out chars from start to end - 1 inclusive
 */
static void _call_console_drivers(unsigned long start,
                unsigned long end, int msg_log_level)
{
    if (msg_log_level < console_loglevel &&
            console_drivers && start != end) {
        if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
            /* wrapped write */
            __call_console_drivers(start & LOG_BUF_MASK,
                        log_buf_len);
            __call_console_drivers(0, end & LOG_BUF_MASK);
        } else {
            __call_console_drivers(start, end);
        }
    }
}

故要查看在console上看不到的内核信息,即 msg level >= console_loglevel 的信息,可以使用dmesg.

要配置console_loglevel,可以在启动kernel时加上loglevel=n这个选项.也可以在应用层通过syslog系统调用(glibc对应的函数为klogctl)来动态修改.


printk 支持同时向多个console输出字符, 其维护一个console_drivers链表,分别调用
void register_console(struct console *console)
int unregister_console(struct console *console)
来添加或删除console.
其实这个console只是提供内核打印输出的功能.  在串口驱动初始化时, 一般会注册一个console.


Tuesday, February 10, 2009

fedora发行版覆盖的kernel版本

fedora 5 kernel-2.6.15-1.2054 kernel-2.6.20-1.2320
fedora 6 kernel-2.6.18-1.2798 kernel-2.6.22.14-72
fedora 7 kernel-2.6.21-1.3194 kernel-2.6.23.17-88
fedora 8 kernel-2.6.23.1-42 kernel-2.6.26.8-57

Saturday, November 01, 2008

qemu试用

最近学习memtest86, 故需要一台主机作为测试. 自然想到了qemu. 说实话以前还没怎么用过虚拟机软件. 所以还是感觉到新鲜的.

创建硬盘:
qemu-img create -f qcow2 c.img 3G

从cd-rom启动:
qemu -cdrom /dev/cdrom -hda c.img -m 256 -boot d
从iso启动:
qemu -cdrom my_os_install.iso -hda c.img -m 256 -boot d

直接启动整个主机的linux 系统:
sudo qemu -kernel /boot/vmlinuz-2.6.25-14.fc9.i686 -initrd /boot/initrd-2.6.25-14.fc9.i686.img -hda /dev/sda -append "ro root=UUID=b53ebbb4-dc5e-4b04-b88b-93f5c892bb29 rhgb quiet"
警告:
WARNING: unless you know what you do, it is better to only make READ-ONLY accesses to the hard disk otherwise you may corrupt your host data (use the `-snapshot' command line option or modify the device permissions accordingly).

run memtest86 under qemu:
http://www.coreboot.org/Memtest86

Thursday, October 16, 2008

Ptherad Cancelability

Ptherad Cancelability 的标准说明见: POSIX.1 2.9.5 Thread Cancellation.

相关接口有:
int pthread_setcancelstate(int state, int *oldstate);
int pthread_setcanceltype(int type, int *oldtype);
void pthread_testcancel(void);

其中:
pthread_setcancelstate 来控制是否允许 cancel.
pthread_setcanceltype 用来控制 Ptherad Cancelability Type: 有deferred 和 asynchronous 两种. deferred 的实现方式为当调用 pthead_cancel时, 在pthread 管理结构中置上一个标志, pthread调用一些libc库函数时,会默认检测和处理这个标志, 也可以显示调用pthread_testcancel 来检测并处理这个标志. asynchronous 的实现方式为 通过 tgkill 系统调用来发送 SIGCANCEL 信号(此信号不能通过pthead_kill发送,无法通过参数检查).

Monday, September 15, 2008

address space resource

内核中有很多资源,但属于IO资源的有:
#define IORESOURCE_IO        0x00000100    /* Resource type */
#define IORESOURCE_MEM        0x00000200
#define IORESOURCE_IRQ        0x00000400
#define IORESOURCE_DMA        0x00000800

本文我主要研究IORESOURCE_IO IORESOURCE_MEM,及地址空间的管理,涉及的文件只有kernel/resource.c.
这两种资源本质上都是一段地址空间. 只是类型不一样
IORESOURCE_IO 指的是IO地址空间,这个空间从kernel编程上来看,只能通过专门的接口函数才能访问.硬件层面上,cpu需要用特殊指令才能访问或需要用特殊访问方 式才能访问,不能直接用指针来寻址.在PC机上,其指的就是PCI/CPU IO address space.在嵌入式中,基本上没有io address space.
IORESOURCE_MEM 指的是属于外设或者用于和设备通讯的支持直接寻址的地址空间.PC机上,主板上北桥上连的内存都是交给kernel直接管理,或者都是用于软件执行,所以 这部分内存不属于IORESOURCE_MEM, IORESOURCE_MEM主要是指PCI设备的 memory address space. 但在嵌入式上, 主板上的sdram一般是设备与cpu共享的. 故交给kernel直接管理的内存只是一部分.余下的内存以及寄存器空间都作为IORESOURCE_MEM来管理.

只所以需要管理,是 因为像PCI总路线设备的这些地址空间是设备向系统申请的,故是可配置的,并且设备并身可能热插拨或更换,故其变成一种可分配的资源. 故内核用算法来管理分配与释放操作,防止冲突和便于查询维护.但实际PC中,BIOS一般会做分配操作,内核需要是把分配结果添加进来,故提供了注册(或 者叫做添加)接口. 在嵌入式系统中,外设的地址也通常是固定的,只需要添加即可.

这两种资源,内核采用同样的管理算法--二叉树.相当于内核维护两个独立的二叉树. 按地址基地址与地址长度范围作为管理数据.可以添加,分配,释放节点.分配过程中可以避
免空间冲突,添加时可以识别空间冲突.根节点在kernel/resource.c中以全局变量方式定义.根节点用于限制地址空间的范围.

主要接口:
int insert_resource(struct resource *parent, struct resource *new)
int adjust_resource(struct resource *res, unsigned long start, unsigned long size)
int allocate_resource(struct resource *root, struct resource *new,
              unsigned long size,
              unsigned long min, unsigned long max,
              unsigned long align,
              void (*alignf)(void *, struct resource *,
                     unsigned long, unsigned long),
              void *alignf_data)
int release_resource(struct resource *old)
int request_resource(struct resource *root, struct resource *new)

上面的接口对上述4种资源类型都有效,对于IORESOURCE_IO 和 IORESOURCE_MEM这两种资源,使用这两个接口更为方便
request_region(start,n,name)
release_region(start,n)
        io space
request_mem_region(start,n,name)
release_mem_region(start,n)
        mem space

在 /proc 文件系统中,ioports和iomem分别显示系统当前这两种资源.


嵌入式设备的外设一般先作为platform device添加到platform bus上,其主要目的就是向系统注册资源,在platform_add_devices时做,
如:
/* Watchdog timer parameters */
static struct resource wdt_resource[] = {
    /* Watchdog timer only needs a register address */
    [0] = {
        .start = 0xFFC00008,
        .end = 0xFFC00010,
        .flags = IORESOURCE_MEM,
    }
};

struct platform_device wdt_device = {
    .name = "wdt",
    .id = -1,
    .num_resources = ARRAY_SIZE(wdt_resource),
    .resource = wdt_resource,
};
platform_add_devices(wdt_device,1);


故如已知设备的物理地址,并不一定需要request_mem_region,因为这个操作并没有涉及到任何硬件,与软件访问也没有任何关系..但一般还是推荐做.

I/O port 访问流程( 不用mem模拟方式):
request_region()   #在设备驱动模块加载或opn() 函数中进行,物理i/o port地址由pci bios模块分配
inb(),outb()等     #在设备驱动初始化,write(),red(),ioctl() 等函数中进行
relase_region      #在设备驱动模块卸载或release() 函数中进行

I/O port 访问流程( 用mem模拟方式):
request_region()
ioport_map         #在设备驱动模块加载或opn() 函数中进行,物理i/o port地址由pci bios模块分配
ioread8,iowrite8等 #在设备驱动初始化,write(),red(),ioctl() 等函数中进行
ioport_unmap()
relase_region      #在设备驱动模块卸载或release() 函数中进行


request_mem_region
ioremap         #在设备驱动模块加载或opn() 函数中进行,物理i/o port地址由pci bios模块分配
ioread8,iowrite8等 #在设备驱动初始化,write(),red(),ioctl() 等函数中进行
iounmap()
relase_region      #在设备驱动模块卸载或release() 函数中进行

内核如何识别root参数所指定的根文件系统设备

把内核的sysfs配置选择去掉后,发现无法mount根文件系统了. 于是,看内核是怎么识别root参数的,以及其与sysfs的关系.
 root 参数的处理在是do_mount.c的prepare_namespace函数里. 基本原理为:先munt devfs到/dev目录,然后根据root参数中指定的设备文件名字,从devfs中找到设备文件对应的dev对象,这样就可以开始mount操作了. 不过devfs文件系统已被废弃了. 相应的功能由sysfs取代. 逻辑差不多.
  另外 /dev/nfs 并不是实际设备文件,只是根代表文件系统为nfs文件系统. 故处理逻辑中不会用到devfs或sysfs. 所以去掉 sysfs 后,内核仍然能mount 上nfs根文件系统,而无法mount上flash mtd partition上的根文件系统.

(移动)硬盘安装fedora9

拿着公司的笔记本回来装fedora9,发现光驱不能用. 故重新温习了一下硬盘安装linux的操作.
以下为笔记:

是内置硬盘,还是usb移动硬盘,方法都是一样的. 故这里不加区分.

基本原理: 开机BIOS加载硬盘上的引导程序grub,gurb从硬盘上引导一个kernel和简单安装程序. 简单安装程序启动后,此时有足够的设备驱动, 再由你来选择具体安装方式: 硬盘安装(ISO)或网络安装(NFS),并进行相应的配置.

上 面提到的安装程序的kernek和简单安装程序都在ISO文件的isolinux目录下,分别叫vmlinuz和initrd.img. 我们将其解出到硬盘的某个分区上(gurb不支持NTFS文件系统). 简单安装程序不支持NTFS文件系统,所以ISO文件要存放在非NTFS分区上.

因此第一步要安装grub.由于gurb要由 BIOS引导,故gurb要安装在任何可启动的磁盘驱动器上. 如你的内置硬盘,或USB存储介质(如BIOS支持的话). 如果电脑的内置硬盘上已经安装有windows,可以在windows里进行安装gurb. 下载WINGURB,选择grub-install,设备选你的内置硬盘即可. 由于grub支持命令行编辑启动,所以无需配置,只要安装上就行. 重启后即进入到grub启动界面,按c进入命令行模式, 用root命令设定vmlinuxz以及initrd.img所在的分区为root分区. 接着用kernel命令和initrd分别选定 vmlinuxz和initrd.img. 最后输入 boot 即可启动. 配置完语言和键盘后,即选择安装方式,选硬盘安装并且指定好ISO所在分区和目录即开始常规的安装过程.

openoffices tip

以前一直习惯于用文本文件来作笔记. 回过头来看这些笔记的时候发现表现力还是不够,或者不够清晰. 于是开始转到openoffice来. 摸索了一阵后,终于可以应付简单的排版了. 以下为备忘:

页面横向纵向设置:
在 Format --> Page 菜单里,  对应英文为Orientation:  Portrait(纵向)  Landscape(横向)

linux用电驴

amule 和 emule 的区别在于 2.1.3的amule不支持upnp端口映射. 故需要要路由器器上去作固定端口映射,速度才上得去.

安装amule:
yum install amule.

所需要做的配置:
1,更新服务器列表
http://www.emule.org.cn/server.met
2,更新 kad 网络
http://www.emule-inside.net/nodes.dat
3,更新用户名前缀
[CHN][VeryCD] 否则连不上 VeryCD 的这个服务器(no1.eserver.emule.org.cn)这 2 个服务器对于国内
用户是相当不错的选择。



浏览器关联:
在地址栏输入 about:config
新建"布尔"
名称为:network.protocol-handler.external.ed2k
值为:true
新建"字符串"
名称为:network.protocol-handler.app.ed2k
值为:/usr/bin/ed2k

shell脚本的路径问题

从网上找到如何从得到shell script的绝对路径的方法:

#! /bin/bash
echo "Path to $(basename $0) is $(readlink -f $0)"

但这种方法只适以子shell方式执行的脚本,不适用于在当前shell中执行的脚本.

难道就没有方法了吗? 

Latex初探

    前段时间要给公司新员工培训linux方面的知识,故使用openoffice来做. 不知道是不是我的版本有问题还是什么原因,设置格式什么的不太好用.当然对于微软的office,我也不太熟悉.

    偶然间在水木上进去到latex的版块,简单了解一下后也就有了想学的冲动. 立刻找资料.但估计这个东西和emacs一样,学习曲线都会是十分陡的. 所以学习笔记先就不在这里记了. 等以后感觉学到某个阶段,再把整理好的笔记补上.

tor

firefox 使用 tor :

1. yum install tor
2. install foxyproxy add-on in firefox
2. restart system or start tor manually
3. configure the autoproxy(firefox add-on) to using tor with specify URL.
4. enjory

yum使用代理

添加 一行:proxy=http://x.x.x.x:x在/etc/yum.conf.

thread

A thread may be created as a joinable thread (the default) or as a detached thread. A joinable thread, like a process, is not automatically cleaned up by GNU/Linux when it terminates. Instead, the thread's exit state hangs around in the system (kind of like a zombie process) until another thread calls pthread_join to obtain its return value. Only then are its resources released. A detached thread, in contrast, is cleaned up automatically when it terminates. Because a detached thread is immediately cleaned up, another thread may not synchronize on its completion by using pthread_join or obtain its return value.

When create thread with pthread_create, the attr argument can also be NULL, in which case default attributes are used: the created thread is joinable (not detached) and has an ordinary (not realtime) scheduling policy.

关于linux 实时性的 好文章
http://blog.tom.com/blog/read.php?bloggerid=993876&blogid=57349

compile and debug directfb

First is something about my develpment environment:
[root@localhost bin]# uname -a
Linux localhost.localdomain 2.6.20-1.2952.fc6 #1 SMP Wed May 16 18:59:18 EDT 2007 i686 i686 i386 GNU/Linux
[root@localhost bin]# rpm -qa | grep gcc
libgcc-4.1.1-30
gcc-4.1.1-30
gcc-gfortran-4.1.1-30
gcc-c++-4.1.1-30

1. Enable Frame buffer device dirver in your machine.

DirectFB version: DirectFB-1.0.0 DirectFB-examples-1.0.0
2. DirectFB:
./configure --prefix=/home/dybbuk/study/DirectFB_install/ --enable-x11 --enable-debug
make
make install

3. DirectFB-examples
./configure --prefix=/home/dybbuk/study/DirectFB_install/ DIRECTFB_CFLAGS=-I/home/dybbuk/study/DirectFB_install/include/directfb/ DIRECTFB_LIBS="-L/home/dybbuk/study/DirectFB_install/lib/ -ldirectfb"
make
make install

Test(under char mode):
export $LD_LIBRARY_PATH=/home/dybbuk/study/DirectFB_install/lib/
/home/dybbuk/study/DirectFB_install/bin/df_window

4. deubg:
add a line "system=x11"  in  the /etc/directfbrc file. 
debug directfb application under X.

For more information see the maillist below:


A copy-paste regarding X method. But I don't know a way without X, and with one machine. If u come to know, plz post it :-)
-----------

Use X

Just for the sake of development, DirectFB actually provides an X backend. In that mode, an X window is the directfb screen, and all directfb display goes to that X window. That is convenient, because
  • We can simultaneously work with whatever other X applications we want
  • Much easier to debug,
  • In situations like crashing or hanging up, you can just close the window, or in the worst case, just killall -9 program-name

Using the X backend

If you did not build directfb with --enable-x11 option, you need to rebuild it with that option(the option is given to configure script). Then, in the /etc/directfbrc file, you need to add a new line: system=x11

Then you can run the same directfb application from X

For performance analysis, and final testing, I would recommend using virtual console, and not X, because thats the way directfb will finally work...



Colin Newell wrote:
What is the best way to run a debugger to debug a directfb program?

One choice is to open an xterm into the machine supporting directfb, and
run gdb in the xterm. If you can see the screen of the directfb machine,
and the Xserver's screen you can efficiently debug.

What if you only have the directfb machine to work on?

If I run gdb on a directfb program directly on the machine with
directfb, then as soon as directfb starts up, gdb loses control of the
keyboard and screen (and then I need to actually kill the program via
an xterm). Is there a clever way to allow use of gdb directly on a
machine running directfb?

Any ideas appreciated.

Regards
Colin Newell

_______________________________________________
directfb-users mailing list
directfb-users@directfb.org
http://mail.directfb.org/cgi-bin/mailman/listinfo/directfb-users



mplayer on frame buffer

The mplayer rpm package form livna is compiled without frame buffer output support( 'mplayer -vo help' for a list of output drivers supported).

So recompile mplayer is needed.
1. enable frame buffer device driver in kernel
For FC6, only append vga=791 to kernel parameter when start kernel can enable frame buffer device driver.
run 'fbset -i' to see some infomation about current frame buffer.
[dybbuk@localhost mplayer_fc6]$ cat /proc/cmdline
ro root=LABEL=/ rhgb quiet vga=791

[dybbuk@localhost mplayer_fc6]$ /usr/sbin/fbset -i
mode "1024x768-76"
    # D: 78.653 MHz, H: 59.949 kHz, V: 75.694 Hz
    geometry 1024 768 1024 768 16
    timings 12714 128 32 16 4 128 4
    rgba 5/11,6/5,5/0,0/0
endmode

Frame buffer device information:
    Name        : VESA VGA
    Address     : 0xc0000000
    Size        : 3145728
    Type        : PACKED PIXELS
    Visual      : TRUECOLOR
    XPanStep    : 0
    YPanStep    : 0
    YWrapStep   : 0
    LineLength  : 2048
    Accelerator : No


2. recompile mplayer
Get lattest developent version from the mplayer svn repository. when configure the source tree, check fbdev list in the enabled video output drivers.

3. test
./mplayer -vo fbdev -vf scale=1024:768 ../../The_Code-Linux.avi

Using Directfb:
   
1. Install directfb and driverfb-dev package
2. modify /etc/fb.modes, add the current frame buffer mode as the first mode.
3. reconfigure and recompile mplayer. make sure directfb list in the enabled video output drivers
4. ./mplayer -vo directfb -vf format=bgr16 ../../The_Code-Linux.avi
(without specify bgr16 format, the picture will all messed up with a lot of green. my graphic chipset is intel 945GM ).

dot emacs

今天双给我的 .emacs 添加了一点新的内容

;; 向前删除一个单词,相应的把默认绑定的 Kill-region 换到其它键
(global-set-key "\C-w" 'backward-kill-word)
(global-set-key "\C-x\C-k" 'kill-region)
(global-set-key "\C-c\C-k" 'kill-region)

(global-set-key (kbd "M-g") 'goto-line)

;; 当文件被修改后相应文件buffer自动刷新
(global-auto-revert-mode 1)

;; Changes all yes/no questions to y/n type
(defalias 'yes-or-no-p 'y-or-n-p)

;; Scroll down with the cursor,move down the buffer one line at a time, instead of in larger amounts.
(setq scroll-step 1)

;; do not make *~ backup files 实际上我很少用到这个备份文件
(setq make-backup-files nil)


;; displays the time in the status bar
(display-time)


不过没有两个问题没有解决:
1. 第一次woman时,其有个 building completion list of all manual topics 的过程,这个过程还是要耗点时间.有没有办法去掉这个过程?

2. woman一个词时,可能会有很多区的manual命中,有没有可能根据当前buffer mode来自动选择一个区.如
pritnf存在于这几个区:
/usr/share/man/man1/printf.1.gz /usr/share/man/man1p/printf.1p.gz
/usr/share/man/man3/printf.3.gz /usr/share/man/man3p/printf.3p.gz.
当我在c-mode下使用woman printf时,则自动选择man3.

3. dired 模式下,按C可以选择拷贝当前选中的文件夹到其它目录,有没有操作可以拷贝一个其它目录到当前目录.

4. 怎么设置 M-x gdb 时是新开一个frame? 因为我想达到一个窗口编辑编译,一个窗口调试的效果..