1 .\" Copyright (c) Michael Smith
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
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.
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
30 .Nd support library for standalone executables
36 library provides a set of supporting functions for standalone
37 applications, mimicking where possible the standard
41 The following sections group these functions by kind.
42 Unless specifically described here, see the corresponding section 3
43 manpages for the given functions.
45 String functions are available as documented in
53 .Fn malloc "size_t size"
58 bytes of memory from the heap using a best-fit algorithm.
64 Free the allocated object at
68 .Fn setheap "void *start" "void *limit"
72 This function must be called before calling
79 will be used for the heap; attempting to allocate beyond this will result
86 Provides the behaviour of
88 i.e., returns the highest point that the heap has reached.
90 be used during testing to determine the actual heap usage.
96 A set of functions are provided for manipulating a flat variable space similar
97 to the traditional shell-supported environment.
98 Major enhancements are support
99 for set/unset hook functions.
103 .Fn getenv "const char *name"
107 .Fn setenv "const char *name" "const char *value" "int overwrite"
111 .Fn putenv "char *string"
115 .Fn unsetenv "const char *name"
118 These functions behave similarly to their standard library counterparts.
120 .Ft "struct env_var *"
121 .Fn env_getenv "const char *name"
124 Looks up a variable in the environment and returns its entire
128 .Fn env_setenv "const char *name" "int flags" "const void *value" "ev_sethook_t sethook" "ev_unsethook_t unsethook"
131 Creates a new or sets an existing environment variable called
133 If creating a new variable, the
137 arguments may be specified.
139 The set hook is invoked whenever an attempt
140 is made to set the variable, unless the EV_NOHOOK flag is set.
142 a set hook will validate the
144 argument, and then call
146 again with EV_NOHOOK set to actually save the value.
147 The predefined function
149 may be specified to refuse all attempts to set a variable.
151 The unset hook is invoked when an attempt is made to unset a variable.
153 returns zero, the variable will be unset.
154 The predefined function
156 may be used to prevent a variable being unset.
158 .Sh STANDARD LIBRARY SUPPORT
166 .Fn getopt "int argc" "char * const *argv" "const char *optstring"
170 .Fn strtol "const char *nptr" "char **endptr" "int base"
174 .Fn strtoll "const char *nptr" "char **endptr" "int base"
178 .Fn strtoul "const char *nptr" "char **endptr" "int base"
182 .Fn strtoull "const char *nptr" "char **endptr" "int base"
186 .Fn srandom "unsigned int seed"
194 .Fn strerror "int error"
197 Returns error messages for the subset of errno values supported by
199 .It Fn assert expression
205 .Fn setjmp "jmp_buf env"
209 .Fn longjmp "jmp_buf env" "int val"
216 respectively as there is no signal state to manipulate.
227 Read characters from the console into
229 All of the standard cautions apply to this function.
232 .Fn ngets "char *buf" "int size"
237 - 1 characters from the console into
241 is less than 1, the function's behaviour is as for
245 .Fn fgetstr "char *buf" "int size" "int fd"
248 Read a line of at most
252 Line terminating characters are stripped, and the buffer is always
255 Returns the number of characters in
257 if successful, or -1 if a read error occurs.
260 .Fn printf "const char *fmt" "..."
264 .Fn vprintf "const char *fmt" "va_list ap"
268 .Fn sprintf "char *buf" "const char *fmt" "..."
272 .Fn vsprintf "char *buf" "const char *fmt" "va_list ap"
275 The *printf functions implement a subset of the standard
277 family functionality and some extensions.
278 The following standard conversions
279 are supported: c,d,n,o,p,s,u,x.
280 The following modifiers are supported:
281 +,-,#,*,0,field width,precision,l.
285 conversion is provided to decode error registers.
287 .Bd -ragged -offset indent
295 where <base> is the output expressed as a control character, e.g.\& \e10 gives
296 octal, \e20 gives hex.
297 Each <arg> is a sequence of characters, the first of
298 which gives the bit number to be inspected (origin 1) and the next characters
299 (up to a character less than 32) give the text to be displayed if the bit is set.
301 .Bd -ragged -offset indent
305 .Qq \e10\e2BITTWO\e1BITONE
309 would give the output
310 .Bd -ragged -offset indent
316 conversion provides a hexdump facility, e.g.
317 .Bd -ragged -offset indent
323 .Qq XX:XX:XX:XX:XX:XX
325 .Bd -ragged -offset indent
335 .Sh CHARACTER TESTS AND CONVERSIONS
394 .Fn open "const char *path" "int flags"
397 Similar to the behaviour as specified in
399 except that file creation is not supported, so the mode parameter is not
403 argument may be one of O_RDONLY, O_WRONLY and O_RDWR.
404 Only UFS currently supports writing.
414 Close all open files.
417 .Fn read "int fd" "void *buf" "size_t len"
421 .Fn write "int fd" "void *buf" "size_t len"
424 (No file systems currently support writing.)
427 .Fn lseek "int fd" "off_t offset" "int whence"
430 Files being automatically uncompressed during reading cannot seek backwards
431 from the current point.
434 .Fn stat "const char *path" "struct stat *sb"
438 .Fn fstat "int fd" "struct stat *sb"
445 functions only fill out the following fields in the
447 structure: st_mode,st_nlink,st_uid,st_gid,st_size.
450 file system cannot provide meaningful values for this call, and the
452 file system always reports files having uid/gid of zero.
457 library supplies a simple internal pager to ease reading the output of large
465 Initialises the pager and tells it that the next line output will be the top of the
467 The environment variable LINES is consulted to determine the number of
468 lines to be displayed before pausing.
477 .Fn pager_output "const char *lines"
480 Sends the lines in the
482 -terminated buffer at
485 Newline characters are counted in order to determine the number
486 of lines being output (wrapped lines are not accounted for).
489 function will return zero when all of the lines have been output, or nonzero
490 if the display was paused and the user elected to quit.
493 .Fn pager_file "const char *fname"
496 Attempts to open and display the file
498 Returns -1 on error, 0 at EOF, or 1 if the user elects to quit while reading.
501 A set of functions are provided to communicate support of features from the
502 loader binary to the interpreter.
503 These are used to do something sensible if we are still operating with a loader
504 binary that behaves differently than expected.
508 .Fn feature_enable "uint32_t mask"
511 Enable the referenced
513 feature, which should be one of the
519 .Fn feature_name_is_enabled "const char *name"
522 Check if the referenced
527 is usually the same name as the
529 macro, but with the FEATURE_ prefix stripped off.
530 The authoritative source of feature names is the mapping table near the top in
531 .Pa stand/libsa/features.c .
534 .Fn "(feature_iter_fn)" "void *cookie" "const char *name" "const char *desc" "bool enabled"
539 argument is passed as-is from the argument of the same name to
545 arguments are defined in the mapping table in
546 .Pa stand/libsa/features.c .
549 argument indicates the current status of the feature, though one could
550 theoretically turn a feature off in later execution.
551 As such, this should likely not be trusted if it is needed after the iteration
555 .Fn "feature_iter" "feature_iter_fn *iter_fn" "void *cookie"
558 Iterate over the current set of features.
564 .Fn devformat "struct devdesc *"
567 Format the specified device as a string.
570 .Fn devparse "struct devdesc **dev" "const char *devdesc" "const char **path"
576 .Sq device:[/path/to/file] .
579 table is used to match the start of the
585 is non-NULL, then it will be called to parse the rest of the string and allocate
589 If NULL, then a default routine will be called that will allocate a simple
591 parse a unit number and ensure there's no trailing characters.
594 is non-NULL, then a pointer to the remainder of the
596 string after the device specification is written.
604 array, returning the number of routines that returned an error.
610 Successive calls emit the characters in the sequence |,/,-,\\ followed by a
611 backspace in order to provide reassurance to the user.
613 .Sh REQUIRED LOW-LEVEL SUPPORT
614 The following resources are consumed by
616 - stack, heap, console and devices.
618 The stack must be established before
620 functions can be invoked.
621 Stack requirements vary depending on the functions
622 and file systems used by the consumer and the support layer functions detailed
625 The heap must be established before calling
631 Heap usage will vary depending on the number of simultaneously open files,
632 as well as client behaviour.
633 Automatic decompression will allocate more
634 than 64K of data per open file.
636 Console access is performed via the
641 functions detailed below.
643 Device access is initiated via
645 and is performed through the
650 functions in the device switch structure that
654 The consumer must provide the following support functions:
661 Return a character from the console, used by
670 Returns nonzero if a character is waiting from the console.
676 Write a character to the console, used by
683 and thus by many other functions for debugging and informational output.
686 .Fn devopen "struct open_file *of" "const char *name" "const char **file"
689 Open the appropriate device for the file named in
693 a pointer to the remaining body of
695 which does not refer to the device.
700 will be set to point to the
702 structure for the opened device if successful.
703 Device identifiers must
704 always precede the path component, but may otherwise be arbitrarily formatted.
707 and thus for all device-related I/O.
710 .Fn devclose "struct open_file *of"
713 Close the device allocated for
715 The device driver itself will already have been called for the close; this call
716 should clean up any allocation made by devopen only.
727 .Fn panic "const char *msg" "..."
730 Signal a fatal and unrecoverable error condition.
736 .Sh INTERNAL FILE SYSTEMS
737 Internal file systems are enabled by the consumer exporting the array
738 .Vt struct fs_ops *file_system[] ,
739 which should be initialised with pointers
743 The following file system handlers are supplied by
745 the consumer may supply other file systems of their own:
746 .Bl -hang -width ".Va cd9660_fsops"
752 Linux ext2fs file system.
754 File access via TFTP.
758 ISO 9660 (CD-ROM) file system.
760 Stacked file system supporting gzipped files.
761 When trying the gzipfs file system,
765 to the end of the filename, and then tries to locate the file using the other
767 Placement of this file system in the
769 array determines whether gzipped files will be opened in preference to non-gzipped
771 It is only possible to seek a gzipped file forwards, and
775 on gzipped files will report an invalid length.
780 .Xr bzip2 1 Ns -compressed
786 pointers should be terminated with a NULL.
788 Devices are exported by the supporting code via the array
789 .Vt struct devsw *devsw[]
790 which is a NULL terminated array of pointers to device switch structures.
792 The driver needs to provide a common set of entry points that are
795 to interface with the device.
798 const char dv_name[DEV_NAMLEN];
800 int (*dv_init)(void);
801 int (*dv_strategy)(void *devdata, int rw, daddr_t blk,
802 size_t size, char *buf, size_t *rsize);
803 int (*dv_open)(struct open_file *f, ...);
804 int (*dv_close)(struct open_file *f);
805 int (*dv_ioctl)(struct open_file *f, u_long cmd, void *data);
806 int (*dv_print)(int verbose);
807 void (*dv_cleanup)(void);
808 char * (*dv_fmtdev)(struct devdesc *);
809 int (*dv_parsedev)(struct devdesc **dev, const char *devpart,
811 bool (*dv_match)(struct devsw *dv, const char *devspec);
814 .Bl -tag -width ".Fn dv_strategy"
819 The supported types are:
820 .Bl -tag -width "DEVT_NONE"
828 Each type may have its own associated (struct type_devdesc),
829 which has the generic (struct devdesc) as its first member.
831 Driver initialization routine.
832 This routine should probe for available units.
833 Drivers are responsible for maintaining lists of units for later enumeration.
834 No other driver routines may be called before
838 The driver open routine.
840 The driver close routine.
842 The driver ioctl routine.
844 Prints information about the available devices.
845 Information should be presented with
848 Cleans up any memory used by the device before the next stage is run.
850 Converts the specified devdesc to the canonical string representation
853 Parses the device portion of a file path.
858 of device name, possibly followed by a colon and a path within the device.
861 is, by convention, the part of the device specification that follows the
866 is parsing the string
872 The parsing routine is expected to allocate a new
874 or subclass and return it in
877 This routine should set
879 to point to the portion of the string after device specification, or
881 in the earlier example.
882 Generally, code needing to parse a path will use
884 instead of calling this routine directly.
887 to specify that all device paths starting with
890 Otherwise, this function returns 0 for a match and a non-zero
892 to indicate why it didn't match.
893 This is helpful when you claim the device path after using it to query
894 properties on systems that have uniform naming for different types of
900 library contains contributions from many sources, including:
915 .An Matthew Dillon Aq Mt dillon@backplane.com
918 The reorganisation and port to
920 the environment functions and this manpage were written by
921 .An Mike Smith Aq Mt msmith@FreeBSD.org .
923 The lack of detailed memory usage data is unhelpful.