LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-17-2018, 04:21 AM   #1
eldiener
Member
 
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106

Rep: Reputation: 17
The "mount" function versus the "mount" command


In the "mount" function (http://man7.org/linux/man-pages/man2/mount.2.html), as I understand it, the programmer is required to pass a non-empty C-string for the filesystem type as the third parameter. Yet in the "mount" command (http://man7.org/linux/man-pages/man8/mount.8.html) it is optional whether or not the '-t fstype' is passed to the command.

Why design a function call which requires the programmer to figure out the filesystem type, whereas the equivalent command does not require it ?

I used libblkid to pick up the filesystem type to pass to the "mount" function successfully in my code, but I do not understand why the "mount" function could not have been designed so that when a null pointer is passed as the 3rd parameter it acts like the "mount" command and figures out the filesystem type to use itself.
 
Old 08-17-2018, 05:29 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Well, mount(8) program does more than just calling mount(2) system-call, for example:

Quote:
If no -t option is given, or if the auto type is specified, mount will try to guess the desired type. Mount uses the
blkid library for guessing the filesystem type; if that does not turn up anything that looks familiar, mount will try
to read the file /etc/filesystems, or, if that does not exist, /proc/filesystems. All of the filesystem types listed
there will be tried, except for those that are labeled "nodev" (e.g., devpts, proc and nfs). If /etc/filesystems ends
in a line with a single *, mount will read /proc/filesystems afterwards. While trying, all filesystem types will be
mounted with the mount option silent.
 
1 members found this post helpful.
Old 08-17-2018, 07:52 PM   #3
eldiener
Member
 
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by NevemTeve View Post
Well, mount(8) program does more than just calling mount(2) system-call, for example:
I thought that was what I said. But why was not the 'mount' function designed in the same way as the 'mount' command if a null ptr is passed as the 3rd parameter to the 'mount' function ?
 
Old 08-17-2018, 08:35 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
As NevemTeve said, they are designed for different uses:

Quote:
mount(8) program does more than just calling mount(2) system-call...
The better question would be, "What were the criteria for implementing the mount() function?", and a similar question for the mount program.

An even better question might be, "Why should one expect the mount() library function to work like the mount program?". They are different animals, produced by different people at different times for different reasons. Only the name is the same.

Why do you expect them to work the same?

If the developers of the mount program had chosen a different name, fsmnt for example, would you have asked, "Why does fsmnt handle args differently than mount()"?

Last edited by astrogeek; 08-17-2018 at 08:53 PM. Reason: tpos, typs, typos, added thought...
 
Old 08-17-2018, 11:12 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Well, it is common that a system-call does simpler things than a utility-program. I think OP wishes for a 'libmount' component. Which does exist.
 
Old 08-18-2018, 08:31 AM   #6
eldiener
Member
 
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by astrogeek View Post
As NevemTeve said, they are designed for different uses:



The better question would be, "What were the criteria for implementing the mount() function?", and a similar question for the mount program.

An even better question might be, "Why should one expect the mount() library function to work like the mount program?". They are different animals, produced by different people at different times for different reasons. Only the name is the same.

Why do you expect them to work the same?

If the developers of the mount program had chosen a different name, fsmnt for example, would you have asked, "Why does fsmnt handle args differently than mount()"?
I expect them to work the same because the purpose of both is the same. I guess it was foolish to even bring this up, even if it is an anomaly in Linux. The answer I am always given is, essentially, "it is what it is", meaning "it may be foolish that they work different but nobody is going to do anything about it." Clearly the "mount" function could work just like the "mount" command, but this would make things much easier for programmers and we don't want to do that, do we ?
 
Old 08-18-2018, 08:44 AM   #7
eldiener
Member
 
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by NevemTeve View Post
Well, it is common that a system-call does simpler things than a utility-program. I think OP wishes for a 'libmount' component. Which does exist.
There is no inherent reason why a function call should be simpler or more complex than an operating system command.
 
Old 08-18-2018, 10:03 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,789

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
Quote:
Originally Posted by eldiener View Post
There is no inherent reason why a function call should be simpler or more complex than an operating system command.
hm. Why do you think that? Where is it coming from?
There is no [inherent] reason to make a function call identical to a command. Isn't there?
 
Old 08-18-2018, 11:09 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Also there might be a reason why I keep repeating the term system call
https://en.wikipedia.org/wiki/System_call

Last edited by NevemTeve; 08-18-2018 at 11:12 AM.
 
Old 08-18-2018, 01:20 PM   #10
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by eldiener View Post
I thought that was what I said. But why was not the 'mount' function designed in the same way as the 'mount' command if a null ptr is passed as the 3rd parameter to the 'mount' function ?
The system call does not impose policy...

In this case, that is the job of the application. It can make any type of decisions it wants to decide what to do.

Burying that in the system call would make it very difficult to change the policy. That was one of the reasons for the /etc/filesystems file - it specifies how the application is to respond. If missing then the application looks elsewhere.
 
2 members found this post helpful.
Old 08-19-2018, 04:35 AM   #11
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by eldiener View Post
I expect them to work the same because the purpose of both is the same. I guess it was foolish to even bring this up, even if it is an anomaly in Linux.
It is not an anomaly, but a rather common feature that the higher you go the abstraction layer the more complex in behaviour functions get. ‘open’ system call gives you a binary stream, C’s ‘fopen’ will handle line breaks for you transparently, Python’s ‘open’ will further decode binary stream into Unicode. This is not limited to system calls. In user space libraries more complex libraries are built on top of simpler ones to provide more features. That’s how software engineering works.

Kernel introduces another aspect. ‘mount’ system call does not detect file system because that can be done in user space. Generally anything that can be done in user space without big impact on performance should be as it makes the overall system more secure.
 
2 members found this post helpful.
Old 08-31-2018, 02:15 PM   #12
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,138

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
If you want something that works just like the mount command, and you don't care how long it takes, then just call the mount command.
Code:
system("mount foo /baz");
 
Old 09-02-2018, 07:24 AM   #13
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by smallpond View Post
If you want something that works just like the mount command, and you don't care how long it takes, then just call the mount command.
Code:
system("mount foo /baz");
Poor to REALLY BAD security.

The problem is that it depends on three things... the environment PATH to find the mount command, the shell the system uses, and the mount command itself. Any of which can redirect to some unexpected program.

System (along with popen) are considered bad for these reasons.
 
  


Reply

Tags
mount


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] What is the Linux mount() function command for command line mount ... "errors=continue" ? soebo Linux - Software 3 08-22-2016 07:54 AM
why only "mount /dev/sda /mnt/usb" works and not "mount /dev/sda1..." ? gromot Linux - General 7 05-19-2009 10:18 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
"fc5" mount does not recognize "smbfs" file system? bdplays Linux - Networking 6 06-28-2006 11:21 AM
Common problems explained: "kernel panic - not syncing", "unable to mount..." sundialsvcs Linux - Newbie 2 03-01-2006 12:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration