Search code examples
clinuxmount

mount server folder C code


Hi I am trying to write C program that mounts server folder. I tried the following code but it didn't work( print message error 1 Operation not permitted, once I run it with sudo I got error 22 ivalid argument). This look very weird for me. Any ideas / suggestions ?

#include <sys/mount.h>
int main(){ 
const char* server_src_path="ip_address:/myfolder";
const char* local_path="/myLocalFolder";
const char* filesystem="nfs";
mount(server_src_path, local_path,filesystem, MS_MGC_VAL | MS_RDONLY | MS_NOSUID, ""); 
printf("error message %d %s\n",errno, strerror(errno));
return 0; 
}

Solution

  • Referring briefly to the mount manpage (in 2, the section for syscall APIs), which you can read with man 2 mount

    1. You are missing the third argument, filesystemtype, which should be a compile-error unless your prototypes are badly broken.
    2. Your server_src_path and local_path are undeclared identifiers, which is a compile-error.
    3. You aren't checking the return value or errno. Usually perror is helpful for getting a readable description of the error.

    When you do determine what error you're getting, that same manpage will tell you under what conditions this function generates that error.