Kernel module - How to test if file doesn't exist


 
Thread Tools Search this Thread
Top Forums Programming Kernel module - How to test if file doesn't exist
# 15  
Old 03-03-2011
This should work for you.
Code:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/file.h>
#include <linux/fcntl.h>
#include <linux/uaccess.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Chris <chris@example.com>");
MODULE_DESCRIPTION("Demo kernel driver");

void openFile(void) {
    struct file *fp = (struct file *) NULL;

    fp = filp_open("/home/chris/Test/testt", O_RDONLY, 0);
    if (IS_ERR(fp) || fp == NULL) {
        printk(KERN_ALERT "ERROR: filp_open\n");
        return;
    }
    if (fp->f_dentry == NULL) {
        printk(KERN_INFO "ERROR: dentry\n");
        return;
    }
    printk(KERN_INFO "FILENAME: %s\n", fp->f_dentry->d_name.name);
}

static int __init testIntro(void) {
    printk(KERN_INFO "elfStart\n");
    openFile();
    return 0;
}

static void __exit testOutro(void) {
    printk(KERN_INFO "Closing down\n");
}

From this you should be able to figure out what is wrong with your code.
These 3 Users Gave Thanks to fpmurphy For This Post:
# 16  
Old 03-10-2011
Quote:
Originally Posted by disaster
openFile is just the function all the stuff happens in...
Here is the full code:
Code:
void openFile(void) {
    char *buf;
    int size;
    struct elfhdr elfH;
    Elf32_Shdr *elfSec;
    struct file *fp;
    fp = filp_open("/home/chris/Test/test", O_RDONLY, 0);
        
    if(fp == (void *)0L) {
        PRINT("FILE NICHT DA");
        return;
    }

    if(fp->f_dentry == (void *)0L) {
        PRINT("F_DENTRY NICHT DA");
        return;
    }

    if(!fp->f_dentry->d_name.name) {
        PRINT("F_DENTRY NICHT DA");
        return;
    }
    
    printk(KERN_INFO "FILENAME: %s\n", fp->f_dentry->d_name.name);
    
    if(!(buf = kmalloc(BINPRM_BUF_SIZE, GFP_KERNEL))) {
        PRINT("KMALLOC");
        return;
    }

    kernel_read(fp, 0, buf, BINPRM_BUF_SIZE);
    elfH = *((struct elfhdr *) buf);

    size = elfH.e_shnum * sizeof(Elf32_Shdr);
    if(!(elfSec = (Elf32_Shdr *) kmalloc(size, GFP_KERNEL))) {
        PRINT("KMALLOC2");
        kfree(buf);
        return;
    }

    readElf(&elfH, elfSec); // another function, works fine when we get here
    kfree(elfSec);
}

I copied the read_kernel from another program, not really sure yet what it does Smilie But as I told you: The crash happens in the part mentioned above
you are free'ing buf (allocated by kmalloc) conditionally. It can cause memory leak in kernel space.

Thanks,
Gaurav.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Group Doesn't Exist

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I'm able to create a group but when I'm trying to delete the group it keeps stating Group Doesn't Exist. I know... (2 Replies)
Discussion started by: GoBoyGo
2 Replies

2. Linux

Unload kernel module at boot time (Debian Wheezy 7.2, 3.2.0-4-686-pae kernel)

Hi everyone, I am trying to prevent the ehci_hcd kernel module to load at boot time. Here's what I've tried so far: 1) Add the following line to /etc/modprobe.d/blacklist.conf (as suggested here): 2) Blacklisted the module by adding the following string to 3) Tried to blacklist the module... (0 Replies)
Discussion started by: gacanepa
0 Replies

3. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

4. Programming

Kernel module - Check whether file (/dev node) exists

Hi, I'm pretty new to kernel coding and I'm working on a device driver that works with an existing framework. Basically my module will be loaded/unloaded multiple times and I'd like to create a register a class, driver, and create a /dev node on the first load only. The existing framework... (0 Replies)
Discussion started by: ThomasBrez
0 Replies

5. Solaris

User directory doesn't exist

Hii all, i create the user useradd -d /home/kk kk passwd kk when i tried to login to kk i get a error user directory doesn't exist then i tried useradd kkk passwd kkkwhen i tried to login to kkk i get the same error user directory doesn't exist. (4 Replies)
Discussion started by: vipinkumarr89
4 Replies

6. Shell Programming and Scripting

ln -s creates symlink in symlink, if [ -f ... ] says file that exists doesn't exist

Hi Forums, I got a little problem, I made a few modifications to the code of the launch script of a testing server(minecraft) and now updating is broken aswell as the automatic directory creation. These Lines somehow create an endless symlink that refers to itself and I don't know how to fix... (0 Replies)
Discussion started by: Xaymar
0 Replies

7. Shell Programming and Scripting

File exist test

Can someone please shed light on why this may not be working, file does exist, but I get an error if ] then echo "No ${source_path}/${file_mask} found - ">> ${logfile} result=1 check_result ${result} "Failed to find file... (4 Replies)
Discussion started by: Pokermad
4 Replies

8. Shell Programming and Scripting

sftp mget where file doesn't exist BASH

I have a script that is working: #!/bin/bash sftp user@domain.com <<EOF cd somedir mget *.csv quit EOF but on a crontab I want to only pull newer files, so I want to do something like: while read ls current dir local file != true do mget that new file but I'm not sure the syntax... (2 Replies)
Discussion started by: unclecameron
2 Replies

9. Linux

How to convert Linux Kernel built-in module into a loadable module

Hi all, I am working on USB data monitoring on Fedora Core 9. Kernel 2.6.25 has a built-in module (the one that isn't loadable, but compiles and links statically with the kernel during compilation) to snoop USB data. It is in <kernel_source_code>/drivers/usb/mon/. I need to know if I can... (0 Replies)
Discussion started by: anitemp
0 Replies

10. SuSE

max number of slabs per kernel module (kernel 2.6.17, suse)

Hi All, Is there a max number of slabs that can be used per kernel module? I'm having a tough time finding out that kind of information, but the array 'node_zonelists' (mmzone.h) has a size of 5. I just want to avoid buffer overruns and other bad stuff. Cheers, Brendan (4 Replies)
Discussion started by: Brendan Kennedy
4 Replies
Login or Register to Ask a Question