Why does call_usermodehelper fail most of the time

2020-02-14 11:23发布

问题:

From a kernel module, I am trying to use call_usermodehelper function to execute an executable sha1 which takes a file as argument and writes the SHA1 hash sum of the file to another file (named output). The executable works perfectly.

int result=-1;
name = "/home/file"
char *hargv[] = {"/home/sha1", name,NULL };
char *henvp[] = {"HOME=/", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
result = call_usermodehelper("/home/sha1", hargv, henvp, 1);

But most of the times call_usermodehelper returns -14 and fails to execute the executable. What could be the reason?

Sometimes it works, but then the output file created is locked (unlike what happens when sha1 is run directly) and I have to run chown before I can use it properly. How can this be prevented?

Is there anyway to do this operation without call_usermodehelper?

回答1:

The last argument for call_usermodehelper is actually some sort of enumeration:

#define UMH_NO_WAIT     0       /* don't wait at all */
#define UMH_WAIT_EXEC   1       /* wait for the exec, but not the process */
#define UMH_WAIT_PROC   2       /* wait for the process to complete */
#define UMH_KILLABLE    4       /* wait for EXEC/PROC killable */

As you can see, with wait=1 the function waits while exec is performed, but doesn't wait the process.

If no other constraints, value UMH_WAIT_PROC gives more stable results.