2014年4月24日 星期四

[Linux]How to execute reboot in the Linux driver module

 void rebootBMC(void) {
    int ret;
    char *cmd[2], *path[4];

    cmd[0] = "/sbin/reboot";
    cmd[1] = NULL;
    path[0] = "HOME=/";
    path[1] = "PWD=/";
    path[2] = "PATH=/sbin";
    path[3] = NULL;
    ret = call_usermodehelper(cnd[0], cnd, path, 0);
    printk(KERN_INFO "reboot to enable SMBus Sideband LAN (ret = %d).\n",  ret);
}


You can fill the parameter into cmd[1] and the cmd and path arrays should end with "NULL.


2014年4月10日 星期四

[Linux]Redirect stderr into stdout

Add 2>&1 in the command line.

e.g. ipmitool -H 10.99.6.8 -U USERID -P PASSW0RD sel 2>&1 | awk 'NR==1{print $2}'



2014年4月8日 星期二

[Linux]Read/Write config file in the driver module

#include linux/string.h

static int WriteBondConfToNCSI(void){
struct file *fp;
char buf[128],  *substring;
int indexofsubstring = 0;
mm_segment_t fs;

fp=filp_open("/conf/bond.conf",O_RDONLY,0);

fs = get_fs();
// Set segment descriptor associated to kernel space
set_fs(get_ds());

memset(buf, 0, sizeof(buf));

// Read the file
fp->f_op->read(fp, buf, 128, &fp->f_pos);
set_fs(fs);
filp_close(fp,NULL);

fp=filp_open("/conf/bond.conf",O_WRONLY | O_TRUNC,0);

substring = strstr(buf, "eth");
indexofsubstring = substring - buf;
buf[indexofsubstring+3] = '0';

fs = get_fs();
// Set segment descriptor associated to kernel space
set_fs(get_ds());

fp->f_op->write(fp, (char *)buf, strlen(buf), &fp->f_pos);

set_fs(fs);
filp_close(fp,NULL);

return 0;
}

如果想判斷某檔案是否存在,可利用下列方式:
filep=filp_open("/tmp/aa",O_RDONLY,0);
if(IS_ERR(filep)){
//Not exist
}
else{
//Exist
}

不可使用if(filep == NULL){}