How to hack it up ( Was: Anyway to mknod from kernel?)

sergey@memco.com
Tue, 26 Oct 1999 18:59:16 -0400


Hi,
Small example how to create a special file inside a module_init() entry.
It works perfectly on Intel Red Hat 6.0. What about another arch. ?
Be sure you have a MKNOD & UNLINK system calls and KERNEL_DS constant.

Sergey Tsybanov

#include<linux/sched.h>
#include<linux/mm.h>
#include<asm/unistd.h>
#include<asm/uaccess.h>

#define MY_DEVICE_NAME "/dev/MY_OWN_DRIVER_NAME"
#define MY_MAJOR_NUMBER 666
#define MY_MINOR_NUMBER 0
#define MY_DEVICE_MODE (0755| S_IFCHR)

typedef int (* syscall_func_t)();
extern syscall_func_t sys_call_table[];

#define sys_unlink sys_call_table[__NR_unlink]
#define sys_mknod sys_call_table[__NR_mknod ]

static int create_device(char *driver_name, int major, int minor, int modes)
{
int ret;
mm_segment_t old_fs;
ret= 0;
old_fs= get_fs();
set_fs (KERNEL_DS);
(void) sys_unlink( driver_name);
ret= sys_mknod( driver_name, modes, MKDEV(major, minor));
set_fs ( old_fs);
return ret;
}

int init_module(){
static char driver_name[]=MY_DEVICE_NAME;
int major, minor, modes;
major= MY_MAJOR_NUMBER;
minor= MY_MINOR_NUMBER;
modes= MY_DEVICE_MODE;
return create_device( driver_name, major, minor, modes);
}

void cleanup_module(){
}

char kernel_version[]="2.2.5-15";

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/