July 25, 2016

kern_path(): Failed to return parent path in new 4.4.* Linux Kernels

I'm trying to get parent path from kern_path() function, it is returning error -2 in new Linux kernels 4.4.*

How to get parent path in 4.4.* Linux kernel?

/tmp/TestFVT/files/fileXX-12345678989 ===> /tmp/TestFVT/files

Similar to this issue.
http://www.gossamer-threads.com/lists/linux/kernel/2460069?do=post_view_...

Below is the source code and output displayed.

hello.c

#include <linux/module.h> // included for all kernel modules
#include <linux/kernel.h> // included for KERN_INFO
#include <linux/init.h> // included for __init and __exit macros
#include <linux/mount.h>
#include <linux/path.h>
#include <linux/namei.h>
#include <linux/fs.h>
#include <linux/string.h>

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("A Simple Hello World module");

char *path_name1 = "/tmp/TestFVT/files/fileXX-1234567898"; //Existing File
char *path_name3 = "/tmp/TestFVT/files/fileXX-12345678989"; //NON Existing FILE
static int __init hello_init(void)
{
struct path path1, path3;
int err = kern_path(path_name1, LOOKUP_PARENT, &path1);
printk("Path name1 : %s, err: %d\n", path_name1, err);
err = kern_path(path_name3, LOOKUP_DIRECTORY, &path3);
printk("Path name3-1 : %s, err: %d\n", path_name3, err);
err = kern_path(path_name3, LOOKUP_PARENT, &path3);
printk("Path name3-2 : %s, err: %d\n", path_name3, err);
return 0; // Non-zero return means that the module couldn't be loaded.
}

static void __exit hello_cleanup(void)
{
printk(KERN_INFO "Cleaning up module.\n");
}

module_init(hello_init);
module_exit(hello_cleanup);
Output:

[ 120.167328] Path name1 : /tmp/TestFVT/files/fileXX-1234567898, err: 0
[ 120.184955] Path name3-1 : /tmp/TestFVT/files/fileXX-12345678989, err: -2
[ 120.189614] Path name3-2 : /tmp/TestFVT/files/fileXX-12345678989, err: -2
Expected output:

"/tmp/TestFVT/files" for non existing files, which is worked fine in previous versions.

Click Here!