sysinfo.texi 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. @node System Management, System Configuration, Users and Groups, Top
  2. @c %MENU% Controlling the system and getting information about it
  3. @chapter System Management
  4. This chapter describes facilities for controlling the system that
  5. underlies a process (including the operating system and hardware) and
  6. for getting information about it. Anyone can generally use the
  7. informational facilities, but usually only a properly privileged process
  8. can make changes.
  9. @menu
  10. * Host Identification:: Determining the name of the machine.
  11. * Platform Type:: Determining operating system and basic
  12. machine type
  13. * Filesystem Handling:: Controlling/querying mounts
  14. * System Parameters:: Getting and setting various system parameters
  15. @end menu
  16. To get information on parameters of the system that are built into the
  17. system, such as the maximum length of a filename, @ref{System
  18. Configuration}.
  19. @node Host Identification
  20. @section Host Identification
  21. This section explains how to identify the particular system on which your
  22. program is running. First, let's review the various ways computer systems
  23. are named, which is a little complicated because of the history of the
  24. development of the Internet.
  25. Every Unix system (also known as a host) has a host name, whether it's
  26. connected to a network or not. In its simplest form, as used before
  27. computer networks were an issue, it's just a word like @samp{chicken}.
  28. @cindex host name
  29. But any system attached to the Internet or any network like it conforms
  30. to a more rigorous naming convention as part of the Domain Name System
  31. (DNS). In the DNS, every host name is composed of two parts:
  32. @cindex DNS
  33. @cindex Domain Name System
  34. @enumerate
  35. @item
  36. hostname
  37. @cindex hostname
  38. @item
  39. domain name
  40. @cindex domain name
  41. @end enumerate
  42. You will note that ``hostname'' looks a lot like ``host name'', but is
  43. not the same thing, and that people often incorrectly refer to entire
  44. host names as ``domain names.''
  45. In the DNS, the full host name is properly called the FQDN (Fully Qualified
  46. Domain Name) and consists of the hostname, then a period, then the
  47. domain name. The domain name itself usually has multiple components
  48. separated by periods. So for example, a system's hostname may be
  49. @samp{chicken} and its domain name might be @samp{ai.mit.edu}, so
  50. its FQDN (which is its host name) is @samp{chicken.ai.mit.edu}.
  51. @cindex FQDN
  52. Adding to the confusion, though, is that the DNS is not the only name space
  53. in which a computer needs to be known. Another name space is the
  54. NIS (aka YP) name space. For NIS purposes, there is another domain
  55. name, which is called the NIS domain name or the YP domain name. It
  56. need not have anything to do with the DNS domain name.
  57. @cindex YP
  58. @cindex NIS
  59. @cindex NIS domain name
  60. @cindex YP domain name
  61. Confusing things even more is the fact that in the DNS, it is possible for
  62. multiple FQDNs to refer to the same system. However, there is always
  63. exactly one of them that is the true host name, and it is called the
  64. canonical FQDN.
  65. In some contexts, the host name is called a ``node name.''
  66. For more information on DNS host naming, see @ref{Host Names}.
  67. @pindex hostname
  68. @pindex hostid
  69. @pindex unistd.h
  70. Prototypes for these functions appear in @file{unistd.h}.
  71. The programs @code{hostname}, @code{hostid}, and @code{domainname} work
  72. by calling these functions.
  73. @deftypefun int gethostname (char *@var{name}, size_t @var{size})
  74. @standards{BSD, unistd.h}
  75. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  76. @c Direct syscall on unix; implemented in terms of uname on posix and of
  77. @c hurd_get_host_config on hurd.
  78. This function returns the host name of the system on which it is called,
  79. in the array @var{name}. The @var{size} argument specifies the size of
  80. this array, in bytes. Note that this is @emph{not} the DNS hostname.
  81. If the system participates in the DNS, this is the FQDN (see above).
  82. The return value is @code{0} on success and @code{-1} on failure. In
  83. @theglibc{}, @code{gethostname} fails if @var{size} is not large
  84. enough; then you can try again with a larger array. The following
  85. @code{errno} error condition is defined for this function:
  86. @table @code
  87. @item ENAMETOOLONG
  88. The @var{size} argument is less than the size of the host name plus one.
  89. @end table
  90. @pindex sys/param.h
  91. On some systems, there is a symbol for the maximum possible host name
  92. length: @code{MAXHOSTNAMELEN}. It is defined in @file{sys/param.h}.
  93. But you can't count on this to exist, so it is cleaner to handle
  94. failure and try again.
  95. @code{gethostname} stores the beginning of the host name in @var{name}
  96. even if the host name won't entirely fit. For some purposes, a
  97. truncated host name is good enough. If it is, you can ignore the
  98. error code.
  99. @end deftypefun
  100. @deftypefun int sethostname (const char *@var{name}, size_t @var{length})
  101. @standards{BSD, unistd.h}
  102. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  103. @c Direct syscall on unix; implemented in terms of hurd_set_host_config
  104. @c on hurd.
  105. The @code{sethostname} function sets the host name of the system that
  106. calls it to @var{name}, a string with length @var{length}. Only
  107. privileged processes are permitted to do this.
  108. Usually @code{sethostname} gets called just once, at system boot time.
  109. Often, the program that calls it sets it to the value it finds in the
  110. file @code{/etc/hostname}.
  111. @cindex /etc/hostname
  112. Be sure to set the host name to the full host name, not just the DNS
  113. hostname (see above).
  114. The return value is @code{0} on success and @code{-1} on failure.
  115. The following @code{errno} error condition is defined for this function:
  116. @table @code
  117. @item EPERM
  118. This process cannot set the host name because it is not privileged.
  119. @end table
  120. @end deftypefun
  121. @deftypefun int getdomainnname (char *@var{name}, size_t @var{length})
  122. @standards{???, unistd.h}
  123. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  124. @c Syscalls uname, then strlen and memcpy.
  125. @cindex NIS domain name
  126. @cindex YP domain name
  127. @code{getdomainname} returns the NIS (aka YP) domain name of the system
  128. on which it is called. Note that this is not the more popular DNS
  129. domain name. Get that with @code{gethostname}.
  130. The specifics of this function are analogous to @code{gethostname}, above.
  131. @end deftypefun
  132. @deftypefun int setdomainname (const char *@var{name}, size_t @var{length})
  133. @standards{???, unistd.h}
  134. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  135. @c Direct syscall.
  136. @cindex NIS domain name
  137. @cindex YP domain name
  138. @code{setdomainname} sets the NIS (aka YP) domain name of the system
  139. on which it is called. Note that this is not the more popular DNS
  140. domain name. Set that with @code{sethostname}.
  141. The specifics of this function are analogous to @code{sethostname}, above.
  142. @end deftypefun
  143. @deftypefun {long int} gethostid (void)
  144. @standards{BSD, unistd.h}
  145. @safety{@prelim{}@mtsafe{@mtshostid{} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{} @acsfd{}}}
  146. @c On HURD, calls _hurd_get_host_config and strtol. On Linux, open
  147. @c HOSTIDFILE, reads an int32_t and closes; if that fails, it calls
  148. @c gethostname and gethostbyname_r to use the h_addr.
  149. This function returns the ``host ID'' of the machine the program is
  150. running on. By convention, this is usually the primary Internet IP address
  151. of that machine, converted to a @w{@code{long int}}. However, on some
  152. systems it is a meaningless but unique number which is hard-coded for
  153. each machine.
  154. This is not widely used. It arose in BSD 4.2, but was dropped in BSD 4.4.
  155. It is not required by POSIX.
  156. The proper way to query the IP address is to use @code{gethostbyname}
  157. on the results of @code{gethostname}. For more information on IP addresses,
  158. @xref{Host Addresses}.
  159. @end deftypefun
  160. @deftypefun int sethostid (long int @var{id})
  161. @standards{BSD, unistd.h}
  162. @safety{@prelim{}@mtunsafe{@mtasuconst{:@mtshostid{}}}@asunsafe{}@acunsafe{@acucorrupt{} @acsfd{}}}
  163. The @code{sethostid} function sets the ``host ID'' of the host machine
  164. to @var{id}. Only privileged processes are permitted to do this. Usually
  165. it happens just once, at system boot time.
  166. The proper way to establish the primary IP address of a system
  167. is to configure the IP address resolver to associate that IP address with
  168. the system's host name as returned by @code{gethostname}. For example,
  169. put a record for the system in @file{/etc/hosts}.
  170. See @code{gethostid} above for more information on host ids.
  171. The return value is @code{0} on success and @code{-1} on failure.
  172. The following @code{errno} error conditions are defined for this function:
  173. @table @code
  174. @item EPERM
  175. This process cannot set the host name because it is not privileged.
  176. @item ENOSYS
  177. The operating system does not support setting the host ID. On some
  178. systems, the host ID is a meaningless but unique number hard-coded for
  179. each machine.
  180. @end table
  181. @end deftypefun
  182. @node Platform Type
  183. @section Platform Type Identification
  184. You can use the @code{uname} function to find out some information about
  185. the type of computer your program is running on. This function and the
  186. associated data type are declared in the header file
  187. @file{sys/utsname.h}.
  188. @pindex sys/utsname.h
  189. As a bonus, @code{uname} also gives some information identifying the
  190. particular system your program is running on. This is the same information
  191. which you can get with functions targeted to this purpose described in
  192. @ref{Host Identification}.
  193. @deftp {Data Type} {struct utsname}
  194. @standards{POSIX.1, sys/utsname.h}
  195. The @code{utsname} structure is used to hold information returned
  196. by the @code{uname} function. It has the following members:
  197. @table @code
  198. @item char sysname[]
  199. This is the name of the operating system in use.
  200. @item char release[]
  201. This is the current release level of the operating system implementation.
  202. @item char version[]
  203. This is the current version level within the release of the operating
  204. system.
  205. @item char machine[]
  206. This is a description of the type of hardware that is in use.
  207. Some systems provide a mechanism to interrogate the kernel directly for
  208. this information. On systems without such a mechanism, @theglibc{}
  209. fills in this field based on the configuration name that was
  210. specified when building and installing the library.
  211. GNU uses a three-part name to describe a system configuration; the three
  212. parts are @var{cpu}, @var{manufacturer} and @var{system-type}, and they
  213. are separated with dashes. Any possible combination of three names is
  214. potentially meaningful, but most such combinations are meaningless in
  215. practice and even the meaningful ones are not necessarily supported by
  216. any particular GNU program.
  217. Since the value in @code{machine} is supposed to describe just the
  218. hardware, it consists of the first two parts of the configuration name:
  219. @samp{@var{cpu}-@var{manufacturer}}. For example, it might be one of these:
  220. @quotation
  221. @code{"sparc-sun"},
  222. @code{"i386-@var{anything}"},
  223. @code{"m68k-hp"},
  224. @code{"m68k-sony"},
  225. @code{"m68k-sun"},
  226. @code{"mips-dec"}
  227. @end quotation
  228. @item char nodename[]
  229. This is the host name of this particular computer. In @theglibc{},
  230. the value is the same as that returned by @code{gethostname};
  231. see @ref{Host Identification}.
  232. @code{gethostname} is implemented with a call to @code{uname}.
  233. @item char domainname[]
  234. This is the NIS or YP domain name. It is the same value returned by
  235. @code{getdomainname}; see @ref{Host Identification}. This element
  236. is a relatively recent invention and use of it is not as portable as
  237. use of the rest of the structure.
  238. @c getdomainname() is implemented with a call to uname().
  239. @end table
  240. @end deftp
  241. @deftypefun int uname (struct utsname *@var{info})
  242. @standards{POSIX.1, sys/utsname.h}
  243. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  244. @c Direct syscall on unix; the posix fallback is to call gethostname and
  245. @c then fills in the other fields with constants; on HURD, it calls
  246. @c proc_uname and then gethostname.
  247. The @code{uname} function fills in the structure pointed to by
  248. @var{info} with information about the operating system and host machine.
  249. A non-negative return value indicates that the data was successfully stored.
  250. @code{-1} as the return value indicates an error. The only error possible is
  251. @code{EFAULT}, which we normally don't mention as it is always a
  252. possibility.
  253. @end deftypefun
  254. @node Filesystem Handling
  255. @section Controlling and Querying Mounts
  256. All files are in filesystems, and before you can access any file, its
  257. filesystem must be mounted. Because of Unix's concept of
  258. @emph{Everything is a file}, mounting of filesystems is central to doing
  259. almost anything. This section explains how to find out what filesystems
  260. are currently mounted and what filesystems are available for mounting,
  261. and how to change what is mounted.
  262. The classic filesystem is the contents of a disk drive. The concept is
  263. considerably more abstract, though, and lots of things other than disk
  264. drives can be mounted.
  265. Some block devices don't correspond to traditional devices like disk
  266. drives. For example, a loop device is a block device whose driver uses
  267. a regular file in another filesystem as its medium. So if that regular
  268. file contains appropriate data for a filesystem, you can by mounting the
  269. loop device essentially mount a regular file.
  270. Some filesystems aren't based on a device of any kind. The ``proc''
  271. filesystem, for example, contains files whose data is made up by the
  272. filesystem driver on the fly whenever you ask for it. And when you
  273. write to it, the data you write causes changes in the system. No data
  274. gets stored.
  275. @c It would be good to mention NFS mounts here.
  276. @menu
  277. * Mount Information:: What is or could be mounted?
  278. * Mount-Unmount-Remount:: Controlling what is mounted and how
  279. @end menu
  280. @node Mount Information, Mount-Unmount-Remount, , Filesystem Handling
  281. @subsection Mount Information
  282. For some programs it is desirable and necessary to access information
  283. about whether a certain filesystem is mounted and, if it is, where, or
  284. simply to get lists of all the available filesystems. @Theglibc{}
  285. provides some functions to retrieve this information portably.
  286. Traditionally Unix systems have a file named @file{/etc/fstab} which
  287. describes all possibly mounted filesystems. The @code{mount} program
  288. uses this file to mount at startup time of the system all the
  289. necessary filesystems. The information about all the filesystems
  290. actually mounted is normally kept in a file named either
  291. @file{/var/run/mtab} or @file{/etc/mtab}. Both files share the same
  292. syntax and it is crucial that this syntax is followed all the time.
  293. Therefore it is best to never directly write to the files. The functions
  294. described in this section can do this and they also provide the
  295. functionality to convert the external textual representation to the
  296. internal representation.
  297. Note that the @file{fstab} and @file{mtab} files are maintained on a
  298. system by @emph{convention}. It is possible for the files not to exist
  299. or not to be consistent with what is really mounted or available to
  300. mount, if the system's administration policy allows it. But programs
  301. that mount and unmount filesystems typically maintain and use these
  302. files as described herein.
  303. @vindex _PATH_FSTAB
  304. @vindex _PATH_MNTTAB
  305. @vindex _PATH_MOUNTED
  306. @vindex FSTAB
  307. @vindex MNTTAB
  308. @vindex MOUNTED
  309. The filenames given above should never be used directly. The portable
  310. way to handle these files is to use the macros @code{_PATH_FSTAB},
  311. defined in @file{fstab.h}, or @code{_PATH_MNTTAB}, defined in
  312. @file{mntent.h} and @file{paths.h}, for @file{fstab}; and the macro
  313. @code{_PATH_MOUNTED}, also defined in @file{mntent.h} and
  314. @file{paths.h}, for @file{mtab}. There are also two alternate macro
  315. names @code{FSTAB}, @code{MNTTAB}, and @code{MOUNTED} defined but
  316. these names are deprecated and kept only for backward compatibility.
  317. The names @code{_PATH_MNTTAB} and @code{_PATH_MOUNTED} should always be used.
  318. @menu
  319. * fstab:: The @file{fstab} file
  320. * mtab:: The @file{mtab} file
  321. * Other Mount Information:: Other (non-libc) sources of mount information
  322. @end menu
  323. @node fstab
  324. @subsubsection The @file{fstab} file
  325. The internal representation for entries of the file is @w{@code{struct
  326. fstab}}, defined in @file{fstab.h}.
  327. @deftp {Data Type} {struct fstab}
  328. @standards{BSD, fstab.h}
  329. This structure is used with the @code{getfsent}, @code{getfsspec}, and
  330. @code{getfsfile} functions.
  331. @table @code
  332. @item char *fs_spec
  333. This element describes the device from which the filesystem is mounted.
  334. Normally this is the name of a special device, such as a hard disk
  335. partition, but it could also be a more or less generic string. For
  336. @dfn{NFS} it would be a hostname and directory name combination.
  337. Even though the element is not declared @code{const} it shouldn't be
  338. modified. The missing @code{const} has historic reasons, since this
  339. function predates @w{ISO C}. The same is true for the other string
  340. elements of this structure.
  341. @item char *fs_file
  342. This describes the mount point on the local system. I.e., accessing any
  343. file in this filesystem has implicitly or explicitly this string as a
  344. prefix.
  345. @item char *fs_vfstype
  346. This is the type of the filesystem. Depending on what the underlying
  347. kernel understands it can be any string.
  348. @item char *fs_mntops
  349. This is a string containing options passed to the kernel with the
  350. @code{mount} call. Again, this can be almost anything. There can be
  351. more than one option, separated from the others by a comma. Each option
  352. consists of a name and an optional value part, introduced by an @code{=}
  353. character.
  354. If the value of this element must be processed it should ideally be done
  355. using the @code{getsubopt} function; see @ref{Suboptions}.
  356. @item const char *fs_type
  357. This name is poorly chosen. This element points to a string (possibly
  358. in the @code{fs_mntops} string) which describes the modes with which the
  359. filesystem is mounted. @file{fstab} defines five macros to describe the
  360. possible values:
  361. @vtable @code
  362. @item FSTAB_RW
  363. The filesystem gets mounted with read and write enabled.
  364. @item FSTAB_RQ
  365. The filesystem gets mounted with read and write enabled. Write access
  366. is restricted by quotas.
  367. @item FSTAB_RO
  368. The filesystem gets mounted read-only.
  369. @item FSTAB_SW
  370. This is not a real filesystem, it is a swap device.
  371. @item FSTAB_XX
  372. This entry from the @file{fstab} file is totally ignored.
  373. @end vtable
  374. Testing for equality with these values must happen using @code{strcmp}
  375. since these are all strings. Comparing the pointer will probably always
  376. fail.
  377. @item int fs_freq
  378. This element describes the dump frequency in days.
  379. @item int fs_passno
  380. This element describes the pass number on parallel dumps. It is closely
  381. related to the @code{dump} utility used on Unix systems.
  382. @end table
  383. @end deftp
  384. To read the entire content of the of the @file{fstab} file @theglibc{}
  385. contains a set of three functions which are designed in the usual way.
  386. @deftypefun int setfsent (void)
  387. @standards{BSD, fstab.h}
  388. @safety{@prelim{}@mtunsafe{@mtasurace{:fsent}}@asunsafe{@ascuheap{} @asucorrupt{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
  389. @c setfsent @mtasurace:fsent @ascuheap @asucorrupt @asulock @acucorrupt @aculock @acsmem @acsfd
  390. @c fstab_init(1) @mtasurace:fsent @ascuheap @asucorrupt @asulock @acucorrupt @aculock @acsmem @acsfd
  391. @c malloc dup @ascuheap @acsmem
  392. @c rewind dup @asucorrupt @acucorrupt [no @aculock]
  393. @c setmntent dup @ascuheap @asulock @acsmem @acsfd @aculock
  394. This function makes sure that the internal read pointer for the
  395. @file{fstab} file is at the beginning of the file. This is done by
  396. either opening the file or resetting the read pointer.
  397. Since the file handle is internal to the libc this function is not
  398. thread-safe.
  399. This function returns a non-zero value if the operation was successful
  400. and the @code{getfs*} functions can be used to read the entries of the
  401. file.
  402. @end deftypefun
  403. @deftypefun void endfsent (void)
  404. @standards{BSD, fstab.h}
  405. @safety{@prelim{}@mtunsafe{@mtasurace{:fsent}}@asunsafe{@ascuheap{} @asucorrupt{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
  406. @c endfsent @mtasurace:fsent @ascuheap @asucorrupt @asulock @acucorrupt @aculock @acsmem @acsfd
  407. @c endmntent dup @ascuheap @asulock @aculock @acsmem @acsfd
  408. This function makes sure that all resources acquired by a prior call to
  409. @code{setfsent} (explicitly or implicitly by calling @code{getfsent}) are
  410. freed.
  411. @end deftypefun
  412. @deftypefun {struct fstab *} getfsent (void)
  413. @standards{BSD, fstab.h}
  414. @safety{@prelim{}@mtunsafe{@mtasurace{:fsent} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{}}}
  415. @c getfsent @mtasurace:fsent @mtslocale @asucorrupt @ascuheap @asulock @acucorrupt @aculock @acsmem
  416. @c fstab_init(0) dup @mtasurace:fsent @ascuheap @asucorrupt @asulock @acucorrupt @aculock @acsmem @acsfd
  417. @c fstab_fetch @mtasurace:fsent @mtslocale @asucorrupt @ascuheap @acucorrupt @aculock @acsmem
  418. @c getmntent_r dup @mtslocale @asucorrupt @ascuheap @acucorrupt @aculock @acsmem
  419. @c fstab_convert @mtasurace:fsent
  420. @c hasmntopt dup ok
  421. This function returns the next entry of the @file{fstab} file. If this
  422. is the first call to any of the functions handling @file{fstab} since
  423. program start or the last call of @code{endfsent}, the file will be
  424. opened.
  425. The function returns a pointer to a variable of type @code{struct
  426. fstab}. This variable is shared by all threads and therefore this
  427. function is not thread-safe. If an error occurred @code{getfsent}
  428. returns a @code{NULL} pointer.
  429. @end deftypefun
  430. @deftypefun {struct fstab *} getfsspec (const char *@var{name})
  431. @standards{BSD, fstab.h}
  432. @safety{@prelim{}@mtunsafe{@mtasurace{:fsent} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{}}}
  433. @c getffsspec @mtasurace:fsent @mtslocale @asucorrupt @ascuheap @asulock @acucorrupt @aculock @acsmem
  434. @c fstab_init(1) dup @mtasurace:fsent @ascuheap @asucorrupt @asulock @acucorrupt @aculock @acsmem @acsfd
  435. @c fstab_fetch dup @mtasurace:fsent @mtslocale @asucorrupt @ascuheap @acucorrupt @aculock @acsmem
  436. @c strcmp dup ok
  437. @c fstab_convert dup @mtasurace:fsent
  438. This function returns the next entry of the @file{fstab} file which has
  439. a string equal to @var{name} pointed to by the @code{fs_spec} element.
  440. Since there is normally exactly one entry for each special device it
  441. makes no sense to call this function more than once for the same
  442. argument. If this is the first call to any of the functions handling
  443. @file{fstab} since program start or the last call of @code{endfsent},
  444. the file will be opened.
  445. The function returns a pointer to a variable of type @code{struct
  446. fstab}. This variable is shared by all threads and therefore this
  447. function is not thread-safe. If an error occurred @code{getfsent}
  448. returns a @code{NULL} pointer.
  449. @end deftypefun
  450. @deftypefun {struct fstab *} getfsfile (const char *@var{name})
  451. @standards{BSD, fstab.h}
  452. @safety{@prelim{}@mtunsafe{@mtasurace{:fsent} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{}}}
  453. @c getffsfile @mtasurace:fsent @mtslocale @asucorrupt @ascuheap @asulock @acucorrupt @aculock @acsmem
  454. @c fstab_init(1) dup @mtasurace:fsent @ascuheap @asucorrupt @asulock @acucorrupt @aculock @acsmem @acsfd
  455. @c fstab_fetch dup @mtasurace:fsent @mtslocale @asucorrupt @ascuheap @acucorrupt @aculock @acsmem
  456. @c strcmp dup ok
  457. @c fstab_convert dup @mtasurace:fsent
  458. This function returns the next entry of the @file{fstab} file which has
  459. a string equal to @var{name} pointed to by the @code{fs_file} element.
  460. Since there is normally exactly one entry for each mount point it
  461. makes no sense to call this function more than once for the same
  462. argument. If this is the first call to any of the functions handling
  463. @file{fstab} since program start or the last call of @code{endfsent},
  464. the file will be opened.
  465. The function returns a pointer to a variable of type @code{struct
  466. fstab}. This variable is shared by all threads and therefore this
  467. function is not thread-safe. If an error occurred @code{getfsent}
  468. returns a @code{NULL} pointer.
  469. @end deftypefun
  470. @node mtab
  471. @subsubsection The @file{mtab} file
  472. The following functions and data structure access the @file{mtab} file.
  473. @deftp {Data Type} {struct mntent}
  474. @standards{BSD, mntent.h}
  475. This structure is used with the @code{getmntent}, @code{getmntent_r},
  476. @code{addmntent}, and @code{hasmntopt} functions.
  477. @table @code
  478. @item char *mnt_fsname
  479. This element contains a pointer to a string describing the name of the
  480. special device from which the filesystem is mounted. It corresponds to
  481. the @code{fs_spec} element in @code{struct fstab}.
  482. @item char *mnt_dir
  483. This element points to a string describing the mount point of the
  484. filesystem. It corresponds to the @code{fs_file} element in
  485. @code{struct fstab}.
  486. @item char *mnt_type
  487. @code{mnt_type} describes the filesystem type and is therefore
  488. equivalent to @code{fs_vfstype} in @code{struct fstab}. @file{mntent.h}
  489. defines a few symbolic names for some of the values this string can have.
  490. But since the kernel can support arbitrary filesystems it does not
  491. make much sense to give them symbolic names. If one knows the symbol
  492. name one also knows the filesystem name. Nevertheless here follows the
  493. list of the symbols provided in @file{mntent.h}.
  494. @vtable @code
  495. @item MNTTYPE_IGNORE
  496. This symbol expands to @code{"ignore"}. The value is sometimes used in
  497. @file{fstab} files to make sure entries are not used without removing them.
  498. @item MNTTYPE_NFS
  499. Expands to @code{"nfs"}. Using this macro sometimes could make sense
  500. since it names the default NFS implementation, in case both version 2
  501. and 3 are supported.
  502. @item MNTTYPE_SWAP
  503. This symbol expands to @code{"swap"}. It names the special @file{fstab}
  504. entry which names one of the possibly multiple swap partitions.
  505. @end vtable
  506. @item char *mnt_opts
  507. The element contains a string describing the options used while mounting
  508. the filesystem. As for the equivalent element @code{fs_mntops} of
  509. @code{struct fstab} it is best to use the function @code{getsubopt}
  510. (@pxref{Suboptions}) to access the parts of this string.
  511. The @file{mntent.h} file defines a number of macros with string values
  512. which correspond to some of the options understood by the kernel. There
  513. might be many more options which are possible so it doesn't make much sense
  514. to rely on these macros but to be consistent here is the list:
  515. @vtable @code
  516. @item MNTOPT_DEFAULTS
  517. Expands to @code{"defaults"}. This option should be used alone since it
  518. indicates all values for the customizable values are chosen to be the
  519. default.
  520. @item MNTOPT_RO
  521. Expands to @code{"ro"}. See the @code{FSTAB_RO} value, it means the
  522. filesystem is mounted read-only.
  523. @item MNTOPT_RW
  524. Expands to @code{"rw"}. See the @code{FSTAB_RW} value, it means the
  525. filesystem is mounted with read and write permissions.
  526. @item MNTOPT_SUID
  527. Expands to @code{"suid"}. This means that the SUID bit (@pxref{How
  528. Change Persona}) is respected when a program from the filesystem is
  529. started.
  530. @item MNTOPT_NOSUID
  531. Expands to @code{"nosuid"}. This is the opposite of @code{MNTOPT_SUID},
  532. the SUID bit for all files from the filesystem is ignored.
  533. @item MNTOPT_NOAUTO
  534. Expands to @code{"noauto"}. At startup time the @code{mount} program
  535. will ignore this entry if it is started with the @code{-a} option to
  536. mount all filesystems mentioned in the @file{fstab} file.
  537. @end vtable
  538. As for the @code{FSTAB_*} entries introduced above it is important to
  539. use @code{strcmp} to check for equality.
  540. @item mnt_freq
  541. This elements corresponds to @code{fs_freq} and also specifies the
  542. frequency in days in which dumps are made.
  543. @item mnt_passno
  544. This element is equivalent to @code{fs_passno} with the same meaning
  545. which is uninteresting for all programs beside @code{dump}.
  546. @end table
  547. @end deftp
  548. For accessing the @file{mtab} file there is again a set of three
  549. functions to access all entries in a row. Unlike the functions to
  550. handle @file{fstab} these functions do not access a fixed file and there
  551. is even a thread safe variant of the get function. Besides this @theglibc{}
  552. contains functions to alter the file and test for specific options.
  553. @deftypefun {FILE *} setmntent (const char *@var{file}, const char *@var{mode})
  554. @standards{BSD, mntent.h}
  555. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@acsmem{} @acsfd{} @aculock{}}}
  556. @c setmntent @ascuheap @asulock @acsmem @acsfd @aculock
  557. @c strlen dup ok
  558. @c mempcpy dup ok
  559. @c memcpy dup ok
  560. @c fopen dup @ascuheap @asulock @acsmem @acsfd @aculock
  561. @c fsetlocking dup ok [no @mtasurace:stream @asulock: exclusive stream]
  562. The @code{setmntent} function prepares the file named @var{FILE} which
  563. must be in the format of a @file{fstab} and @file{mtab} file for the
  564. upcoming processing through the other functions of the family. The
  565. @var{mode} parameter can be chosen in the way the @var{opentype}
  566. parameter for @code{fopen} (@pxref{Opening Streams}) can be chosen. If
  567. the file is opened for writing the file is also allowed to be empty.
  568. If the file was successfully opened @code{setmntent} returns a file
  569. handle for future use. Otherwise the return value is @code{NULL}
  570. and @code{errno} is set accordingly.
  571. @end deftypefun
  572. @deftypefun int endmntent (FILE *@var{stream})
  573. @standards{BSD, mntent.h}
  574. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
  575. @c endmntent @ascuheap @asulock @aculock @acsmem @acsfd
  576. @c fclose dup @ascuheap @asulock @aculock @acsmem @acsfd
  577. This function takes for the @var{stream} parameter a file handle which
  578. previously was returned from the @code{setmntent} call.
  579. @code{endmntent} closes the stream and frees all resources.
  580. The return value is @math{1} unless an error occurred in which case it
  581. is @math{0}.
  582. @end deftypefun
  583. @deftypefun {struct mntent *} getmntent (FILE *@var{stream})
  584. @standards{BSD, mntent.h}
  585. @safety{@prelim{}@mtunsafe{@mtasurace{:mntentbuf} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asuinit{}}@acunsafe{@acuinit{} @acucorrupt{} @aculock{} @acsmem{}}}
  586. @c getmntent @mtasurace:mntentbuf @mtslocale @asucorrupt @ascuheap @asuinit @acuinit @acucorrupt @aculock @acsmem
  587. @c libc_once @ascuheap @asuinit @acuinit @acsmem
  588. @c allocate @ascuheap @acsmem
  589. @c malloc dup @ascuheap @acsmem
  590. @c getmntent_r dup @mtslocale @asucorrupt @ascuheap @acucorrupt @aculock @acsmem
  591. The @code{getmntent} function takes as the parameter a file handle
  592. previously returned by a successful call to @code{setmntent}. It returns
  593. a pointer to a static variable of type @code{struct mntent} which is
  594. filled with the information from the next entry from the file currently
  595. read.
  596. The file format used prescribes the use of spaces or tab characters to
  597. separate the fields. This makes it harder to use names containing one
  598. of these characters (e.g., mount points using spaces). Therefore
  599. these characters are encoded in the files and the @code{getmntent}
  600. function takes care of the decoding while reading the entries back in.
  601. @code{'\040'} is used to encode a space character, @code{'\011'} to
  602. encode a tab character, @code{'\012'} to encode a newline character,
  603. and @code{'\\'} to encode a backslash.
  604. If there was an error or the end of the file is reached the return value
  605. is @code{NULL}.
  606. This function is not thread-safe since all calls to this function return
  607. a pointer to the same static variable. @code{getmntent_r} should be
  608. used in situations where multiple threads access the file.
  609. @end deftypefun
  610. @deftypefun {struct mntent *} getmntent_r (FILE *@var{stream}, struct mntent *@var{result}, char *@var{buffer}, int @var{bufsize})
  611. @standards{BSD, mntent.h}
  612. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{}}}
  613. @c getmntent_r @mtslocale @asucorrupt @ascuheap @acucorrupt @aculock @acsmem
  614. @c flockfile dup @aculock
  615. @c fgets_unlocked dup @asucorrupt @acucorrupt [locked, so no @mtsrace:stream]
  616. @c funlockfile dup @aculock
  617. @c strchr dup ok
  618. @c strspn dup ok
  619. @c strsep dup ok
  620. @c decode_name ok
  621. @c sscanf dup @mtslocale @ascuheap @acsmem
  622. The @code{getmntent_r} function is the reentrant variant of
  623. @code{getmntent}. It also returns the next entry from the file and
  624. returns a pointer. The actual variable the values are stored in is not
  625. static, though. Instead the function stores the values in the variable
  626. pointed to by the @var{result} parameter. Additional information (e.g.,
  627. the strings pointed to by the elements of the result) are kept in the
  628. buffer of size @var{bufsize} pointed to by @var{buffer}.
  629. Escaped characters (space, tab, backslash) are converted back in the
  630. same way as it happens for @code{getmentent}.
  631. The function returns a @code{NULL} pointer in error cases. Errors could be:
  632. @itemize @bullet
  633. @item
  634. error while reading the file,
  635. @item
  636. end of file reached,
  637. @item
  638. @var{bufsize} is too small for reading a complete new entry.
  639. @end itemize
  640. @end deftypefun
  641. @deftypefun int addmntent (FILE *@var{stream}, const struct mntent *@var{mnt})
  642. @standards{BSD, mntent.h}
  643. @safety{@prelim{}@mtsafe{@mtsrace{:stream} @mtslocale{}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  644. @c addmntent @mtasurace:stream @mtslocale @asucorrupt @acucorrupt
  645. @c fseek dup @asucorrupt @acucorrupt [no @aculock]
  646. @c encode_name ok
  647. @c fprintf dup @mtslocale @asucorrupt @acucorrupt [no @ascuheap @acsmem, no @aculock]
  648. @c fflush dup @asucorrupt @acucorrupt [no @aculock]
  649. The @code{addmntent} function allows adding a new entry to the file
  650. previously opened with @code{setmntent}. The new entries are always
  651. appended. I.e., even if the position of the file descriptor is not at
  652. the end of the file this function does not overwrite an existing entry
  653. following the current position.
  654. The implication of this is that to remove an entry from a file one has
  655. to create a new file while leaving out the entry to be removed and after
  656. closing the file remove the old one and rename the new file to the
  657. chosen name.
  658. This function takes care of spaces and tab characters in the names to be
  659. written to the file. It converts them and the backslash character into
  660. the format described in the @code{getmntent} description above.
  661. This function returns @math{0} in case the operation was successful.
  662. Otherwise the return value is @math{1} and @code{errno} is set
  663. appropriately.
  664. @end deftypefun
  665. @deftypefun {char *} hasmntopt (const struct mntent *@var{mnt}, const char *@var{opt})
  666. @standards{BSD, mntent.h}
  667. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  668. @c hasmntopt ok
  669. @c strlen dup ok
  670. @c strstr dup ok
  671. @c strchr dup ok
  672. This function can be used to check whether the string pointed to by the
  673. @code{mnt_opts} element of the variable pointed to by @var{mnt} contains
  674. the option @var{opt}. If this is true a pointer to the beginning of the
  675. option in the @code{mnt_opts} element is returned. If no such option
  676. exists the function returns @code{NULL}.
  677. This function is useful to test whether a specific option is present but
  678. when all options have to be processed one is better off with using the
  679. @code{getsubopt} function to iterate over all options in the string.
  680. @end deftypefun
  681. @node Other Mount Information
  682. @subsubsection Other (Non-libc) Sources of Mount Information
  683. On a system with a Linux kernel and the @code{proc} filesystem, you can
  684. get information on currently mounted filesystems from the file
  685. @file{mounts} in the @code{proc} filesystem. Its format is similar to
  686. that of the @file{mtab} file, but represents what is truly mounted
  687. without relying on facilities outside the kernel to keep @file{mtab} up
  688. to date.
  689. @node Mount-Unmount-Remount, , Mount Information, Filesystem Handling
  690. @subsection Mount, Unmount, Remount
  691. This section describes the functions for mounting, unmounting, and
  692. remounting filesystems.
  693. Only the superuser can mount, unmount, or remount a filesystem.
  694. These functions do not access the @file{fstab} and @file{mtab} files. You
  695. should maintain and use these separately. @xref{Mount Information}.
  696. The symbols in this section are declared in @file{sys/mount.h}.
  697. @deftypefun {int} mount (const char *@var{special_file}, const char *@var{dir}, const char *@var{fstype}, unsigned long int @var{options}, const void *@var{data})
  698. @standards{SVID, sys/mount.h}
  699. @standards{BSD, sys/mount.h}
  700. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  701. @c Direct syscall.
  702. @code{mount} mounts or remounts a filesystem. The two operations are
  703. quite different and are merged rather unnaturally into this one function.
  704. The @code{MS_REMOUNT} option, explained below, determines whether
  705. @code{mount} mounts or remounts.
  706. For a mount, the filesystem on the block device represented by the
  707. device special file named @var{special_file} gets mounted over the mount
  708. point @var{dir}. This means that the directory @var{dir} (along with any
  709. files in it) is no longer visible; in its place (and still with the name
  710. @var{dir}) is the root directory of the filesystem on the device.
  711. As an exception, if the filesystem type (see below) is one which is not
  712. based on a device (e.g. ``proc''), @code{mount} instantiates a
  713. filesystem and mounts it over @var{dir} and ignores @var{special_file}.
  714. For a remount, @var{dir} specifies the mount point where the filesystem
  715. to be remounted is (and remains) mounted and @var{special_file} is
  716. ignored. Remounting a filesystem means changing the options that control
  717. operations on the filesystem while it is mounted. It does not mean
  718. unmounting and mounting again.
  719. For a mount, you must identify the type of the filesystem with
  720. @var{fstype}. This type tells the kernel how to access the filesystem
  721. and can be thought of as the name of a filesystem driver. The
  722. acceptable values are system dependent. On a system with a Linux kernel
  723. and the @code{proc} filesystem, the list of possible values is in the
  724. file @file{filesystems} in the @code{proc} filesystem (e.g. type
  725. @kbd{cat /proc/filesystems} to see the list). With a Linux kernel, the
  726. types of filesystems that @code{mount} can mount, and their type names,
  727. depends on what filesystem drivers are configured into the kernel or
  728. loaded as loadable kernel modules. An example of a common value for
  729. @var{fstype} is @code{ext2}.
  730. For a remount, @code{mount} ignores @var{fstype}.
  731. @c This is traditionally called "rwflag" for historical reasons.
  732. @c No point in confusing people today, though.
  733. @var{options} specifies a variety of options that apply until the
  734. filesystem is unmounted or remounted. The precise meaning of an option
  735. depends on the filesystem and with some filesystems, an option may have
  736. no effect at all. Furthermore, for some filesystems, some of these
  737. options (but never @code{MS_RDONLY}) can be overridden for individual
  738. file accesses via @code{ioctl}.
  739. @var{options} is a bit string with bit fields defined using the
  740. following mask and masked value macros:
  741. @vtable @code
  742. @item MS_MGC_MASK
  743. This multibit field contains a magic number. If it does not have the value
  744. @code{MS_MGC_VAL}, @code{mount} assumes all the following bits are zero and
  745. the @var{data} argument is a null string, regardless of their actual values.
  746. @item MS_REMOUNT
  747. This bit on means to remount the filesystem. Off means to mount it.
  748. @c There is a mask MS_RMT_MASK in mount.h that says only two of the options
  749. @c can be reset by remount. But the Linux kernel has its own version of
  750. @c MS_RMT_MASK that says they all can be reset. As far as I can tell,
  751. @c libc just passes the arguments straight through to the kernel.
  752. @item MS_RDONLY
  753. This bit on specifies that no writing to the filesystem shall be allowed
  754. while it is mounted. This cannot be overridden by @code{ioctl}. This
  755. option is available on nearly all filesystems.
  756. @item MS_NOSUID
  757. This bit on specifies that Setuid and Setgid permissions on files in the
  758. filesystem shall be ignored while it is mounted.
  759. @item MS_NOEXEC
  760. This bit on specifies that no files in the filesystem shall be executed
  761. while the filesystem is mounted.
  762. @item MS_NODEV
  763. This bit on specifies that no device special files in the filesystem
  764. shall be accessible while the filesystem is mounted.
  765. @item MS_SYNCHRONOUS
  766. This bit on specifies that all writes to the filesystem while it is
  767. mounted shall be synchronous; i.e., data shall be synced before each
  768. write completes rather than held in the buffer cache.
  769. @item MS_MANDLOCK
  770. This bit on specifies that mandatory locks on files shall be permitted while
  771. the filesystem is mounted.
  772. @item MS_NOATIME
  773. This bit on specifies that access times of files shall not be updated when
  774. the files are accessed while the filesystem is mounted.
  775. @item MS_NODIRATIME
  776. This bit on specifies that access times of directories shall not be updated
  777. when the directories are accessed while the filesystem in mounted.
  778. @c there is also S_QUOTA Linux fs.h (mount.h still uses its former name
  779. @c S_WRITE), but I can't see what it does. Turns on quotas, I guess.
  780. @end vtable
  781. Any bits not covered by the above masks should be set off; otherwise,
  782. results are undefined.
  783. The meaning of @var{data} depends on the filesystem type and is controlled
  784. entirely by the filesystem driver in the kernel.
  785. Example:
  786. @smallexample
  787. @group
  788. #include <sys/mount.h>
  789. mount("/dev/hdb", "/cdrom", MS_MGC_VAL | MS_RDONLY | MS_NOSUID, "");
  790. mount("/dev/hda2", "/mnt", MS_MGC_VAL | MS_REMOUNT, "");
  791. @end group
  792. @end smallexample
  793. Appropriate arguments for @code{mount} are conventionally recorded in
  794. the @file{fstab} table. @xref{Mount Information}.
  795. The return value is zero if the mount or remount is successful. Otherwise,
  796. it is @code{-1} and @code{errno} is set appropriately. The values of
  797. @code{errno} are filesystem dependent, but here is a general list:
  798. @table @code
  799. @item EPERM
  800. The process is not superuser.
  801. @item ENODEV
  802. The file system type @var{fstype} is not known to the kernel.
  803. @item ENOTBLK
  804. The file @var{dev} is not a block device special file.
  805. @item EBUSY
  806. @itemize @bullet
  807. @item
  808. The device is already mounted.
  809. @item
  810. The mount point is busy. (E.g. it is some process' working directory or
  811. has a filesystem mounted on it already).
  812. @item
  813. The request is to remount read-only, but there are files open for writing.
  814. @end itemize
  815. @item EINVAL
  816. @itemize @bullet
  817. @item
  818. A remount was attempted, but there is no filesystem mounted over the
  819. specified mount point.
  820. @item
  821. The supposed filesystem has an invalid superblock.
  822. @end itemize
  823. @item EACCES
  824. @itemize @bullet
  825. @item
  826. The filesystem is inherently read-only (possibly due to a switch on the
  827. device) and the process attempted to mount it read/write (by setting the
  828. @code{MS_RDONLY} bit off).
  829. @item
  830. @var{special_file} or @var{dir} is not accessible due to file permissions.
  831. @item
  832. @var{special_file} is not accessible because it is in a filesystem that is
  833. mounted with the @code{MS_NODEV} option.
  834. @end itemize
  835. @item EM_FILE
  836. The table of dummy devices is full. @code{mount} needs to create a
  837. dummy device (aka ``unnamed'' device) if the filesystem being mounted is
  838. not one that uses a device.
  839. @end table
  840. @end deftypefun
  841. @deftypefun {int} umount2 (const char *@var{file}, int @var{flags})
  842. @standards{GNU, sys/mount.h}
  843. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  844. @c Direct syscall.
  845. @code{umount2} unmounts a filesystem.
  846. You can identify the filesystem to unmount either by the device special
  847. file that contains the filesystem or by the mount point. The effect is
  848. the same. Specify either as the string @var{file}.
  849. @var{flags} contains the one-bit field identified by the following
  850. mask macro:
  851. @vtable @code
  852. @item MNT_FORCE
  853. This bit on means to force the unmounting even if the filesystem is
  854. busy, by making it unbusy first. If the bit is off and the filesystem is
  855. busy, @code{umount2} fails with @code{errno} = @code{EBUSY}. Depending
  856. on the filesystem, this may override all, some, or no busy conditions.
  857. @end vtable
  858. All other bits in @var{flags} should be set to zero; otherwise, the result
  859. is undefined.
  860. Example:
  861. @smallexample
  862. @group
  863. #include <sys/mount.h>
  864. umount2("/mnt", MNT_FORCE);
  865. umount2("/dev/hdd1", 0);
  866. @end group
  867. @end smallexample
  868. After the filesystem is unmounted, the directory that was the mount point
  869. is visible, as are any files in it.
  870. As part of unmounting, @code{umount2} syncs the filesystem.
  871. If the unmounting is successful, the return value is zero. Otherwise, it
  872. is @code{-1} and @code{errno} is set accordingly:
  873. @table @code
  874. @item EPERM
  875. The process is not superuser.
  876. @item EBUSY
  877. The filesystem cannot be unmounted because it is busy. E.g. it contains
  878. a directory that is some process's working directory or a file that some
  879. process has open. With some filesystems in some cases, you can avoid
  880. this failure with the @code{MNT_FORCE} option.
  881. @item EINVAL
  882. @var{file} validly refers to a file, but that file is neither a mount
  883. point nor a device special file of a currently mounted filesystem.
  884. @end table
  885. This function is not available on all systems.
  886. @end deftypefun
  887. @deftypefun {int} umount (const char *@var{file})
  888. @standards{SVID, sys/mount.h}
  889. @standards{GNU, sys/mount.h}
  890. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  891. @c Direct syscall or wrapper for umount2.
  892. @code{umount} does the same thing as @code{umount2} with @var{flags} set
  893. to zeroes. It is more widely available than @code{umount2} but since it
  894. lacks the possibility to forcefully unmount a filesystem is deprecated
  895. when @code{umount2} is also available.
  896. @end deftypefun
  897. @node System Parameters
  898. @section System Parameters
  899. This section describes the @code{sysctl} function, which gets and sets
  900. a variety of system parameters.
  901. The symbols used in this section are declared in the file @file{sys/sysctl.h}.
  902. @deftypefun int sysctl (int *@var{names}, int @var{nlen}, void *@var{oldval}, size_t *@var{oldlenp}, void *@var{newval}, size_t @var{newlen})
  903. @standards{BSD, sys/sysctl.h}
  904. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  905. @c Direct syscall, Linux only.
  906. @code{sysctl} gets or sets a specified system parameter. There are so
  907. many of these parameters that it is not practical to list them all here,
  908. but here are some examples:
  909. @itemize @bullet
  910. @item network domain name
  911. @item paging parameters
  912. @item network Address Resolution Protocol timeout time
  913. @item maximum number of files that may be open
  914. @item root filesystem device
  915. @item when kernel was built
  916. @end itemize
  917. The set of available parameters depends on the kernel configuration and
  918. can change while the system is running, particularly when you load and
  919. unload loadable kernel modules.
  920. The system parameters with which @code{sysctl} is concerned are arranged
  921. in a hierarchical structure like a hierarchical filesystem. To identify
  922. a particular parameter, you specify a path through the structure in a
  923. way analogous to specifying the pathname of a file. Each component of
  924. the path is specified by an integer and each of these integers has a
  925. macro defined for it by @file{sys/sysctl.h}. @var{names} is the path, in
  926. the form of an array of integers. Each component of the path is one
  927. element of the array, in order. @var{nlen} is the number of components
  928. in the path.
  929. For example, the first component of the path for all the paging
  930. parameters is the value @code{CTL_VM}. For the free page thresholds, the
  931. second component of the path is @code{VM_FREEPG}. So to get the free
  932. page threshold values, make @var{names} an array containing the two
  933. elements @code{CTL_VM} and @code{VM_FREEPG} and make @var{nlen} = 2.
  934. The format of the value of a parameter depends on the parameter.
  935. Sometimes it is an integer; sometimes it is an ASCII string; sometimes
  936. it is an elaborate structure. In the case of the free page thresholds
  937. used in the example above, the parameter value is a structure containing
  938. several integers.
  939. In any case, you identify a place to return the parameter's value with
  940. @var{oldval} and specify the amount of storage available at that
  941. location as *@var{oldlenp}. *@var{oldlenp} does double duty because it
  942. is also the output location that contains the actual length of the
  943. returned value.
  944. If you don't want the parameter value returned, specify a null pointer
  945. for @var{oldval}.
  946. To set the parameter, specify the address and length of the new value
  947. as @var{newval} and @var{newlen}. If you don't want to set the parameter,
  948. specify a null pointer as @var{newval}.
  949. If you get and set a parameter in the same @code{sysctl} call, the value
  950. returned is the value of the parameter before it was set.
  951. Each system parameter has a set of permissions similar to the
  952. permissions for a file (including the permissions on directories in its
  953. path) that determine whether you may get or set it. For the purposes of
  954. these permissions, every parameter is considered to be owned by the
  955. superuser and Group 0 so processes with that effective uid or gid may
  956. have more access to system parameters. Unlike with files, the superuser
  957. does not invariably have full permission to all system parameters, because
  958. some of them are designed not to be changed ever.
  959. @code{sysctl} returns a zero return value if it succeeds. Otherwise, it
  960. returns @code{-1} and sets @code{errno} appropriately. Besides the
  961. failures that apply to all system calls, the following are the
  962. @code{errno} codes for all possible failures:
  963. @table @code
  964. @item EPERM
  965. The process is not permitted to access one of the components of the
  966. path of the system parameter or is not permitted to access the system parameter
  967. itself in the way (read or write) that it requested.
  968. @c There is some indication in the Linux 2.2 code that the code is trying to
  969. @c return EACCES here, but the EACCES value never actually makes it to the
  970. @c user.
  971. @item ENOTDIR
  972. There is no system parameter corresponding to @var{name}.
  973. @item EFAULT
  974. @var{oldval} is not null, which means the process wanted to read the parameter,
  975. but *@var{oldlenp} is zero, so there is no place to return it.
  976. @item EINVAL
  977. @itemize @bullet
  978. @item
  979. The process attempted to set a system parameter to a value that is not valid
  980. for that parameter.
  981. @item
  982. The space provided for the return of the system parameter is not the right
  983. size for that parameter.
  984. @end itemize
  985. @item ENOMEM
  986. This value may be returned instead of the more correct @code{EINVAL} in some
  987. cases where the space provided for the return of the system parameter is too
  988. small.
  989. @end table
  990. @end deftypefun
  991. If you have a Linux kernel with the @code{proc} filesystem, you can get
  992. and set most of the same parameters by reading and writing to files in
  993. the @code{sys} directory of the @code{proc} filesystem. In the @code{sys}
  994. directory, the directory structure represents the hierarchical structure
  995. of the parameters. E.g. you can display the free page thresholds with
  996. @smallexample
  997. cat /proc/sys/vm/freepages
  998. @end smallexample
  999. @c In Linux, the sysctl() and /proc instances of the parameter are created
  1000. @c together. The proc filesystem accesses the same data structure as
  1001. @c sysctl(), which has special fields in it for /proc. But it is still
  1002. @c possible to create a sysctl-only parameter.
  1003. Some more traditional and more widely available, though less general,
  1004. @glibcadj{} functions for getting and setting some of the same system
  1005. parameters are:
  1006. @itemize @bullet
  1007. @item
  1008. @code{getdomainname}, @code{setdomainname}
  1009. @item
  1010. @code{gethostname}, @code{sethostname} (@xref{Host Identification}.)
  1011. @item
  1012. @code{uname} (@xref{Platform Type}.)
  1013. @end itemize