July 26, 2016

kern_path(): Failed to return parent path in new 4.4.* Linux Kernels ? Please help me any alternative solution to get parent path for non existing file ?

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

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

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

Similar to this issue.

Below is the source code and output displayed.


#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_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");


[ 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!