priv: Define and use PRIV_SETHOSTNAME
[dragonfly.git] / lib / libstand / libstand.3
1 .\" Copyright (c) Michael Smith
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\"
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 .\" SUCH DAMAGE.
24 .\"
25 .\" $FreeBSD: src/lib/libstand/libstand.3,v 1.5.2.11 2002/06/26 19:14:43 schweikh Exp $
26 .\" $DragonFly: src/lib/libstand/libstand.3,v 1.9 2008/11/23 21:55:52 swildner Exp $
27 .\"
28 .Dd February 15, 2009
29 .Dt LIBSTAND 3
30 .Os
31 .Sh NAME
32 .Nm libstand
33 .Nd support library for standalone executables
34 .Sh LIBRARY
35 .Lb libstand
36 .Sh SYNOPSIS
37 .In stand.h
38 .Sh DESCRIPTION
39 .Nm
40 provides a set of supporting functions for standalone
41 applications, mimicking where possible the standard
42 .Bx
43 programming
44 environment.
45 The following sections group these functions by kind.
46 Unless specifically described here, see the corresponding section 3
47 manpages for the given functions.
48 .Sh STRING FUNCTIONS
49 String functions are available as documented in
50 .Xr string 3
51 and
52 .Xr bstring 3 .
53 .Sh MEMORY ALLOCATION
54 .Bl -hang -width 10n
55 .It Xo
56 .Ft "void *"
57 .Fn malloc "size_t size"
58 .Xc
59 .Pp
60 Allocate
61 .Fa size
62 bytes of memory from the heap using a best-fit algorithm.
63 .It Xo
64 .Ft void
65 .Fn free "void *ptr"
66 .Xc
67 .Pp
68 Free the allocated object at
69 .Fa ptr .
70 .It Xo
71 .Ft void
72 .Fn setheap "void *start" "void *limit"
73 .Xc
74 .Pp
75 Initialise the heap.
76 This function must be called before calling
77 .Fn alloc
78 for the first time.
79 The region between
80 .Fa start
81 and
82 .Fa limit
83 will be used for the heap; attempting to allocate beyond this will result
84 in a panic.
85 .It Xo
86 .Ft "char *"
87 .Fn sbrk "int junk"
88 .Xc
89 .Pp
90 Provides the behaviour of
91 .Fn sbrk 0 ,
92 ie. returns the highest point that the heap has reached.
93 This value can
94 be used during testing to determine the actual heap usage.
95 The
96 .Fa junk
97 argument is ignored.
98 .El
99 .Sh ENVIRONMENT
100 A set of functions are provided for manipulating a flat variable space similar
101 to the traditional shell-supported environment.
102 Major enhancements are support
103 for set/unset hook functions.
104 .Bl -hang -width 10n
105 .It Xo
106 .Ft "char *"
107 .Fn getenv "const char *name"
108 .Xc
109 .It Xo
110 .Ft int
111 .Fn setenv "const char *name" "char *value" "int overwrite"
112 .Xc
113 .It Xo
114 .Ft int
115 .Fn putenv "const char *string"
116 .Xc
117 .It Xo
118 .Ft int
119 .Fn unsetenv "const char *name"
120 .Xc
121 .Pp
122 These functions behave similarly to their standard library counterparts.
123 .It Xo
124 .Ft "struct env_var *"
125 .Fn env_getenv "const char *name"
126 .Xc
127 .Pp
128 Looks up a variable in the environment and returns its entire
129 data structure.
130 .It Xo
131 .Ft int
132 .Fn env_setenv "const char *name" "int flags" "char *value" "ev_sethook_t sethook" "ev_unsethook_t unsethook"
133 .Xc
134 .Pp
135 Creates a new or sets an existing environment variable called
136 .Fa name .
137 If creating a new variable, the
138 .Fa sethook
139 and
140 .Fa unsethook
141 arguments may be specified.
142 .Pp
143 The set hook is invoked whenever an attempt
144 is made to set the variable, unless the
145 .Dv EV_NOHOOK
146 flag is set.
147 Typically
148 a set hook will validate the
149 .Fa value
150 argument, and then call
151 .Fn env_setenv
152 again with
153 .Dv EV_NOHOOK
154 set to actually save the value.
155 The predefined function
156 .Fn env_noset
157 may be specified to refuse all attempts to set a variable.
158 .Pp
159 The unset hook is invoked when an attempt is made to unset a variable.
160 If it
161 returns zero, the  variable will be unset.
162 The predefined function
163 .Fn env_nounset
164 may be used to prevent a variable being unset.
165 .El
166 .Sh STANDARD LIBRARY SUPPORT
167 .Bl -hang -width 10n
168 .It Xo
169 .Ft int
170 .Fn getopt "int argc" "char * const *argv" "const char *optstring"
171 .Xc
172 .It Xo
173 .Ft long
174 .Fn strtol "const char *nptr" "char **endptr" "int base"
175 .Xc
176 .It Xo
177 .Ft void
178 .Fn srandom "unsigned long seed"
179 .Xc
180 .It Xo
181 .Ft "unsigned long"
182 .Fn random void
183 .Xc
184 .It Xo
185 .Ft "char *"
186 .Fn strerror "int error"
187 .Xc
188 .Pp
189 Returns error messages for the subset of
190 .Va errno
191 values supported by
192 .Nm .
193 .It Fn assert expression
194 .Pp
195 Requires
196 .In assert.h .
197 .It Xo
198 .Ft int
199 .Fn setjmp "jmp_buf env"
200 .Xc
201 .It Xo
202 .Ft void
203 .Fn longjmp "jmp_buf env" "int val"
204 .Xc
205 .Pp
206 Defined as
207 .Fn _setjmp
208 and
209 .Fn _longjmp
210 respectively as there is no signal state to manipulate.
211 Requires
212 .In setjmp.h .
213 .El
214 .Sh CHARACTER I/O
215 .Bl -hang -width 10n
216 .It Xo
217 .Ft void
218 .Fn gets "char *buf"
219 .Xc
220 .Pp
221 Read characters from the console into
222 .Fa buf .
223 All of the standard cautions apply to this function.
224 .It Xo
225 .Ft void
226 .Fn ngets "char *buf" "size_t size"
227 .Xc
228 .Pp
229 Read at most
230 .Fa size
231 - 1 characters from the console into
232 .Fa buf .
233 If
234 .Fa size
235 is less than 1, the function's behaviour is as for
236 .Fn gets .
237 .It Xo
238 .Ft int
239 .Fn fgetstr "char *buf" "int size" "int fd"
240 .Xc
241 .Pp
242 Read a line of at most
243 .Fa size
244 characters into
245 .Fa buf .
246 Line terminating characters are stripped, and the buffer is always
247 nul-terminated.
248 Returns the number of characters in
249 .Fa buf
250 if successful, or -1 if a read error occurs.
251 .It Xo
252 .Ft int
253 .Fn printf "const char *fmt" "..."
254 .Xc
255 .It Xo
256 .Ft void
257 .Fn vprintf "const char *fmt" "va_list ap"
258 .Xc
259 .It Xo
260 .Ft int
261 .Fn sprintf "char *buf" "const char *fmt" "..."
262 .Xc
263 .It Xo
264 .Ft void
265 .Fn vsprintf "char *buf" "const char *fmt" "va_list ap"
266 .Xc
267 .Pp
268 The *printf functions implement a subset of the standard
269 .Fn printf
270 family functionality and some extensions.
271 The following standard conversions
272 are supported:
273 .Cm c , d , n , o , p , s , u , x .
274 The following modifiers are supported:
275 .Cm + , - , # , * , 0 , field width,  precision,  l .
276 .Pp
277 The
278 .Cm b
279 conversion is provided to decode error registers.
280 Its usage is:
281 .Bd -ragged -offset indent
282 printf(
283 .Qq reg=%b\en ,
284 regval,
285 .Qq <base><arg>*
286 );
287 .Ed
288 .Pp
289 where <base> is the output expressed as a control character, e.g. \e10 gives
290 octal, \e20 gives hex.
291 Each <arg> is a sequence of characters, the first of
292 which gives the bit number to be inspected (origin 1) and the next characters
293 (up to a character less than 32) give the text to be displayed if the bit is set.
294 Thus
295 .Bd -ragged -offset indent
296 printf(
297 .Qq reg=%b\en ,
298 3,
299 .Qq \e10\e2BITTWO\e1BITONE\en
300 );
301 .Ed
302 .Pp
303 would give the output
304 .Bd -ragged -offset indent
305 reg=3<BITTWO,BITONE>
306 .Ed
307 .Pp
308 The
309 .Cm D
310 conversion provides a hexdump facility, eg.
311 .Bd -ragged -offset indent
312 printf(
313 .Qq %6D ,
314 ptr,
315 .Qq \&:
316 );  gives
317 .Qq XX:XX:XX:XX:XX:XX
318 .Ed
319 .Bd -ragged -offset indent
320 printf(
321 .Qq %*D ,
322 len,
323 ptr,
324 .Qq "\ "
325 );  gives
326 .Qq XX XX XX ...
327 .Ed
328 .El
329 .Sh CHARACTER TESTS AND CONVERSIONS
330 .Bl -hang -width 10n
331 .It Xo
332 .Ft int
333 .Fn isupper "int c"
334 .Xc
335 .It Xo
336 .Ft int
337 .Fn islower "int c"
338 .Xc
339 .It Xo
340 .Ft int
341 .Fn isspace "int c"
342 .Xc
343 .It Xo
344 .Ft int
345 .Fn isdigit "int c"
346 .Xc
347 .It Xo
348 .Ft int
349 .Fn isxdigit "int c"
350 .Xc
351 .It Xo
352 .Ft int
353 .Fn isascii "int c"
354 .Xc
355 .It Xo
356 .Ft int
357 .Fn isalpha "int c"
358 .Xc
359 .It Xo
360 .Ft int
361 .Fn toupper "int c"
362 .Xc
363 .It Xo
364 .Ft int
365 .Fn tolower "int c"
366 .Xc
367 .El
368 .Sh FILE I/O
369 .Bl -hang -width 10n
370 .It Xo
371 .Ft int
372 .Fn open "const char *path" "int flags"
373 .Xc
374 .Pp
375 Similar to the behaviour as specified in
376 .Xr open 2 ,
377 except that file creation is not supported, so the mode parameter is not
378 required.
379 The
380 .Fa flags
381 argument may be one of
382 .Dv O_RDONLY ,
383 .Dv O_WRONLY
384 and
385 .Dv O_RDWR
386 (although no filesystems currently support writing).
387 .It Xo
388 .Ft int
389 .Fn close "int fd"
390 .Xc
391 .It Xo
392 .Ft void
393 .Fn closeall void
394 .Xc
395 .Pp
396 Close all open files.
397 .It Xo
398 .Ft ssize_t
399 .Fn read "int fd" "void *buf" "size_t len"
400 .Xc
401 .It Xo
402 .Ft ssize_t
403 .Fn write "int fd" "void *buf" "size_t len"
404 .Xc
405 .Pp
406 (No filesystems currently support writing.)
407 .It Xo
408 .Ft off_t
409 .Fn lseek "int fd" "off_t offset" "int whence"
410 .Xc
411 .Pp
412 Files being automatically uncompressed during reading cannot seek backwards
413 from the current point.
414 .It Xo
415 .Ft int
416 .Fn stat "const char *path" "struct stat *sb"
417 .Xc
418 .It Xo
419 .Ft int
420 .Fn fstat "int fd" "struct stat *sb"
421 .Xc
422 .Pp
423 The
424 .Fn stat
425 and
426 .Fn fstat
427 functions only fill out the following fields in the
428 .Fa sb
429 structure:
430 .Fa st_mode , st_nlink , st_uid , st_gid , st_size .
431 The
432 .Nm tftp
433 filesystem cannot provide meaningful values for this call, and the
434 .Nm cd9660
435 filesystem always reports files having uid/gid of zero.
436 .El
437 .Sh PAGER
438 .Nm
439 supplies a simple internal pager to ease reading the output of large commands.
440 .Bl -hang -width 10n
441 .It Xo
442 .Ft void
443 .Fn pager_open
444 .Xc
445 .Pp
446 Initialises the pager and tells it that the next line output will be the
447 top of the display.
448 The environment variable LINES is consulted to determine the number of
449 lines to be displayed before pausing.
450 .It Xo
451 .Ft void
452 .Fn pager_close void
453 .Xc
454 .Pp
455 Closes the pager.
456 .It Xo
457 .Ft int
458 .Fn pager_output "char *lines"
459 .Xc
460 .Pp
461 Sends the lines in the nul-terminated buffer at
462 .Fa lines
463 to the pager.
464 Newline characters are counted in order to determine the number
465 of lines being output (wrapped lines are not accounted for).
466 .Fn pager_output
467 will return zero when all of the lines have been output, or nonzero if the
468 display was paused and the user elected to quit.
469 .It Xo
470 .Ft int
471 .Fn pager_file "char *fname"
472 .Xc
473 .Pp
474 Attempts to open and display the file
475 .Fa fname .
476 Returns -1 on error, 0 at
477 .Dv EOF ,
478 or 1 if the user elects to quit while reading.
479 .El
480 .Sh MISC
481 .Bl -hang -width 10n
482 .It Xo
483 .Ft void
484 .Fn twiddle void
485 .Xc
486 .Pp
487 Successive calls emit the characters in the sequence |, /, -, \\ followed by a
488 backspace in order to provide reassurance to the user.
489 .El
490 .Sh REQUIRED LOW-LEVEL SUPPORT
491 The following resources are consumed by
492 .Nm
493 - stack, heap, console and devices.
494 .Pp
495 The stack must be established before
496 .Nm
497 functions can be invoked.
498 Stack requirements vary depending on the functions
499 and filesystems used by the consumer and the support layer functions detailed
500 below.
501 .Pp
502 The heap must be established before calling
503 .Fn alloc
504 or
505 .Fn open
506 by calling
507 .Fn setheap .
508 Heap usage will vary depending on the number of simultaneously open files,
509 as well as client behaviour.
510 Automatic decompression will allocate more
511 than 64K of data per open file.
512 .Pp
513 Console access is performed via the
514 .Fn getchar ,
515 .Fn putchar
516 and
517 .Fn ischar
518 functions detailed below.
519 .Pp
520 Device access is initiated via
521 .Fn devopen
522 and is performed through the
523 .Fn dv_strategy ,
524 .Fn dv_ioctl
525 and
526 .Fn dv_close
527 functions in the device switch structure that
528 .Fn devopen
529 returns.
530 .Pp
531 The consumer must provide the following support functions:
532 .Bl -hang -width 10n
533 .It Xo
534 .Ft int
535 .Fn getchar void
536 .Xc
537 .Pp
538 Return a character from the console, used by
539 .Fn gets ,
540 .Fn ngets
541 and pager functions.
542 .It Xo
543 .Ft int
544 .Fn ischar void
545 .Xc
546 .Pp
547 Returns nonzero if a character is waiting from the console.
548 .It Xo
549 .Ft void
550 .Fn putchar int
551 .Xc
552 .Pp
553 Write a character to the console, used by
554 .Fn gets ,
555 .Fn ngets ,
556 .Fn *printf ,
557 .Fn panic
558 and
559 .Fn twiddle
560 and thus by many other functions for debugging and informational output.
561 .It Xo
562 .Ft int
563 .Fn devopen "struct open_file *of" "const char *name" "char **file"
564 .Xc
565 .Pp
566 Open the appropriate device for the file named in
567 .Fa name ,
568 returning in
569 .Fa file
570 a pointer to the remaining body of
571 .Fa name
572 which does not refer to the device.
573 The
574 .Va f_dev
575 field in
576 .Fa of
577 will be set to point to the
578 .Vt devsw
579 structure for the opened device if successful.
580 Device identifiers must
581 always precede the path component, but may otherwise be arbitrarily formatted.
582 Used by
583 .Fn open
584 and thus for all device-related I/O.
585 .It Xo
586 .Ft int
587 .Fn devclose "struct open_file *of"
588 .Xc
589 .Pp
590 Close the device allocated for
591 .Fa of .
592 The device driver itself will already have been called for the close;
593 this call should clean up any allocation made by
594 .Fn devopen
595 only.
596 .It Xo
597 .Ft void
598 .Fn panic "const char *msg" "..."
599 .Xc
600 .Pp
601 Signal a fatal and unrecoverable error condition.
602 The
603 .Fa msg ...
604 arguments are as for
605 .Fn printf .
606 .El
607 .Sh INTERNAL FILESYSTEMS
608 Internal filesystems are enabled by the consumer exporting the array
609 .Vt struct fs_ops *file_system[] ,
610 which should be initialised with pointers
611 to
612 .Vt struct fs_ops
613 structures.
614 The following filesystem handlers are supplied by
615 .Nm ,
616 the consumer may supply other filesystems of their own:
617 .Bl -hang -width ".Va cd9660_fsops"
618 .It Va ufs_fsops
619 The
620 .Bx
621 .Xr UFS 5 .
622 .It Va hammer_fsops
623 .Xr HAMMER 5
624 filesystem.
625 .It Va ext2fs_fsops
626 Linux ext2fs filesystem.
627 .It Va msdos_fsops
628 MS-DOS filesystem.
629 .It Va tftp_fsops
630 File access via TFTP.
631 .It Va nfs_fsops
632 File access via NFS.
633 .It Va cd9660_fsops
634 ISO 9660 (CD-ROM) filesystem.
635 .It Va zipfs_fsops
636 Stacked filesystem supporting gzipped files.
637 When trying the zipfs filesystem,
638 .Nm
639 appends
640 .Li .gz
641 to the end of the filename, and then tries to locate the file using the other
642 filesystems.
643 Placement of this filesystem in the
644 .Va file_system[]
645 array determines whether gzipped files will be opened in preference to non-gzipped
646 files.
647 It is only possible to seek a gzipped file forwards, and
648 .Fn stat
649 and
650 .Fn fstat
651 on gzipped files will report an invalid length.
652 .It Va bzipfs_fsops
653 The same as
654 .Va zipfs_fsops ,
655 but for
656 .Xr bzip2 1 Ns -compressed
657 files.
658 .El
659 .Pp
660 The array of
661 .Vt struct fs_ops
662 pointers should be terminated with a NULL.
663 .Sh DEVICES
664 Devices are exported by the supporting code via the array
665 .Vt struct devsw *devsw[]
666 which is a NULL terminated array of pointers to device switch structures.
667 .Sh HISTORY
668 .Nm
669 contains contributions from many sources, including:
670 .Bl -bullet -compact
671 .It
672 .Nm libsa
673 from
674 .Nx
675 .It
676 .Nm libc
677 and
678 .Nm libkern
679 from
680 .Fx 3.0 .
681 .It
682 .Nm zalloc
683 from
684 .An Matthew Dillon Aq dillon@backplane.com
685 .El
686 .Pp
687 The reorganisation and port to
688 .Fx 3.0 ,
689 the environment functions and this manpage were written by
690 .An Mike Smith Aq msmith@FreeBSD.org .
691 .Sh BUGS
692 The lack of detailed memory usage data is unhelpful.