I've been trying to write an application for older versions of Mac OS X that mounts an NFS network share and ran into an interesting quirk when trying to use the mount(2)
function. According to the documentation it should has the following prototype:
int mount(const char *type, const char *dir, int flags, void *data);
As the documentation is vague on the value of type
, I've assumed that it was the network location, in the form of server.name:/some/location
. So I've written the following test code:
- (BOOL)mount:(NSString *)serverAddress mountPoint:(NSString *)path
{
int ret = mount([serverAddress UTF8String], [path UTF8String], 0, NULL);
NSLog(@"mount = %d", ret);
// Handle mounting errors.
if (ret != 0) {
NSString *errorMessage = [NSString stringWithUTF8String:strerror(errno)];
NSLog(@"errno = %d (%@)", errno, errorMessage);
}
return ret == 0;
}
When I run this example method under OS X Mavericks I get the following output from the console:
mount = -1
errno = 3 (No such process)
Failed to mount network share: No such process
Most interestingly is that if I run the same example under OS X Leopard on a Power Mac G5 I get the following output:
mount = -1
errno = 63 (File name too long)
Failed to mount network share: File name too long
On both machines I'm able to mount the network share using the mount(8)
command supplying the same parameters as I did with the example method. By the way, the type
location is 46 characters long and the dir
path is located under the user's home folder.
In order to try to replicate the issue on modern versions of macOS I've written the following test code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/param.h>
#include <sys/mount.h>
int main(int argc, char **argv) {
const char *type = "nfsserver.lan:/some/path";
const char *mntp = "/Users/localuser/tmpmount";
int ret = mount(type, mntp, 0, NULL);
printf("ret = %d\n", ret);
printf("errno = %d (%s)\n", errno, strerror(errno));
return 0;
}
Upon running this code on the Mavericks machine and on a Mac Pro running macOS 12.7.4 I got the following output:
ret = -1
errno = 63 (File name too long)
This shows that there is clearly something wrong with my call to mount(2)
, which parameters are wrong and how can they be fixed?