xref: /f-stack/tools/libutil/libutil.h (revision d4a07e70)
122ce4affSfengbojiang /*-
222ce4affSfengbojiang  * SPDX-License-Identifier: BSD-3-Clause
322ce4affSfengbojiang  *
41eaf0ac3Slogwang  * Copyright (c) 1996  Peter Wemm <[email protected]>.
51eaf0ac3Slogwang  * All rights reserved.
61eaf0ac3Slogwang  * Copyright (c) 2002 Networks Associates Technology, Inc.
71eaf0ac3Slogwang  * All rights reserved.
81eaf0ac3Slogwang  *
91eaf0ac3Slogwang  * Portions of this software were developed for the FreeBSD Project by
101eaf0ac3Slogwang  * ThinkSec AS and NAI Labs, the Security Research Division of Network
111eaf0ac3Slogwang  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
121eaf0ac3Slogwang  * ("CBOSS"), as part of the DARPA CHATS research program.
131eaf0ac3Slogwang  *
141eaf0ac3Slogwang  * Redistribution and use in source and binary forms, with or without
151eaf0ac3Slogwang  * modification, is permitted provided that the following conditions
161eaf0ac3Slogwang  * are met:
171eaf0ac3Slogwang  * 1. Redistributions of source code must retain the above copyright
181eaf0ac3Slogwang  *    notice, this list of conditions and the following disclaimer.
191eaf0ac3Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
201eaf0ac3Slogwang  *    notice, this list of conditions and the following disclaimer in the
211eaf0ac3Slogwang  *    documentation and/or other materials provided with the distribution.
221eaf0ac3Slogwang  * 3. The name of the author may not be used to endorse or promote
231eaf0ac3Slogwang  *    products derived from this software without specific prior written
241eaf0ac3Slogwang  *    permission.
251eaf0ac3Slogwang  *
261eaf0ac3Slogwang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
271eaf0ac3Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281eaf0ac3Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291eaf0ac3Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
301eaf0ac3Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311eaf0ac3Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321eaf0ac3Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331eaf0ac3Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341eaf0ac3Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
351eaf0ac3Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361eaf0ac3Slogwang  * SUCH DAMAGE.
371eaf0ac3Slogwang  *
381eaf0ac3Slogwang  * $FreeBSD$
391eaf0ac3Slogwang  */
401eaf0ac3Slogwang 
411eaf0ac3Slogwang #ifndef _LIBUTIL_H_
421eaf0ac3Slogwang #define	_LIBUTIL_H_
431eaf0ac3Slogwang 
441eaf0ac3Slogwang #include <sys/cdefs.h>
451eaf0ac3Slogwang #include <sys/_types.h>
46*d4a07e70Sfengbojiang #ifndef FSTACK
471eaf0ac3Slogwang #include <sys/_stdint.h>
48*d4a07e70Sfengbojiang #else
49*d4a07e70Sfengbojiang #include <stdint.h>
50*d4a07e70Sfengbojiang #endif
511eaf0ac3Slogwang 
521eaf0ac3Slogwang #ifndef _GID_T_DECLARED
531eaf0ac3Slogwang typedef	__gid_t		gid_t;
541eaf0ac3Slogwang #define	_GID_T_DECLARED
551eaf0ac3Slogwang #endif
561eaf0ac3Slogwang 
571eaf0ac3Slogwang #ifndef _MODE_T_DECLARED
581eaf0ac3Slogwang typedef	__mode_t	mode_t;
591eaf0ac3Slogwang #define	_MODE_T_DECLARED
601eaf0ac3Slogwang #endif
611eaf0ac3Slogwang 
621eaf0ac3Slogwang #ifndef _PID_T_DECLARED
631eaf0ac3Slogwang typedef	__pid_t		pid_t;
641eaf0ac3Slogwang #define	_PID_T_DECLARED
651eaf0ac3Slogwang #endif
661eaf0ac3Slogwang 
671eaf0ac3Slogwang #ifndef _SIZE_T_DECLARED
681eaf0ac3Slogwang typedef	__size_t	size_t;
691eaf0ac3Slogwang #define	_SIZE_T_DECLARED
701eaf0ac3Slogwang #endif
711eaf0ac3Slogwang 
721eaf0ac3Slogwang #ifndef _UID_T_DECLARED
731eaf0ac3Slogwang typedef	__uid_t		uid_t;
741eaf0ac3Slogwang #define	_UID_T_DECLARED
751eaf0ac3Slogwang #endif
761eaf0ac3Slogwang 
771eaf0ac3Slogwang #define	PROPERTY_MAX_NAME	64
781eaf0ac3Slogwang #define	PROPERTY_MAX_VALUE	512
791eaf0ac3Slogwang 
801eaf0ac3Slogwang /* For properties.c. */
811eaf0ac3Slogwang typedef struct _property {
821eaf0ac3Slogwang 	struct _property *next;
831eaf0ac3Slogwang 	char	*name;
841eaf0ac3Slogwang 	char	*value;
851eaf0ac3Slogwang } *properties;
861eaf0ac3Slogwang 
871eaf0ac3Slogwang /* Avoid pulling in all the include files for no need. */
881eaf0ac3Slogwang struct in_addr;
891eaf0ac3Slogwang struct pidfh;
901eaf0ac3Slogwang struct sockaddr;
911eaf0ac3Slogwang struct termios;
921eaf0ac3Slogwang struct winsize;
931eaf0ac3Slogwang 
941eaf0ac3Slogwang __BEGIN_DECLS
951eaf0ac3Slogwang char	*auth_getval(const char *_name);
961eaf0ac3Slogwang void	clean_environment(const char * const *_white,
971eaf0ac3Slogwang 	    const char * const *_more_white);
981eaf0ac3Slogwang int	expand_number(const char *_buf, uint64_t *_num);
991eaf0ac3Slogwang int	extattr_namespace_to_string(int _attrnamespace, char **_string);
1001eaf0ac3Slogwang int	extattr_string_to_namespace(const char *_string, int *_attrnamespace);
1011eaf0ac3Slogwang int	flopen(const char *_path, int _flags, ...);
10222ce4affSfengbojiang int	flopenat(int _dirfd, const char *_path, int _flags, ...);
1031eaf0ac3Slogwang int	forkpty(int *_amaster, char *_name,
1041eaf0ac3Slogwang 	    struct termios *_termp, struct winsize *_winp);
10522ce4affSfengbojiang const char *
10622ce4affSfengbojiang 	getlocalbase(void);
1071eaf0ac3Slogwang void	hexdump(const void *_ptr, int _length, const char *_hdr, int _flags);
1081eaf0ac3Slogwang int	humanize_number(char *_buf, size_t _len, int64_t _number,
1091eaf0ac3Slogwang 	    const char *_suffix, int _scale, int _flags);
1101eaf0ac3Slogwang struct kinfo_file *
1111eaf0ac3Slogwang 	kinfo_getfile(pid_t _pid, int *_cntp);
1121eaf0ac3Slogwang struct kinfo_vmentry *
1131eaf0ac3Slogwang 	kinfo_getvmmap(pid_t _pid, int *_cntp);
1141eaf0ac3Slogwang struct kinfo_vmobject *
1151eaf0ac3Slogwang 	kinfo_getvmobject(int *_cntp);
1161eaf0ac3Slogwang struct kinfo_proc *
1171eaf0ac3Slogwang 	kinfo_getallproc(int *_cntp);
1181eaf0ac3Slogwang struct kinfo_proc *
1191eaf0ac3Slogwang 	kinfo_getproc(pid_t _pid);
1201eaf0ac3Slogwang int	kld_isloaded(const char *_name);
1211eaf0ac3Slogwang int	kld_load(const char *_name);
1221eaf0ac3Slogwang int	login_tty(int _fd);
1231eaf0ac3Slogwang int	openpty(int *_amaster, int *_aslave, char *_name,
1241eaf0ac3Slogwang 	    struct termios *_termp, struct winsize *_winp);
1251eaf0ac3Slogwang int	pidfile_close(struct pidfh *_pfh);
1261eaf0ac3Slogwang int	pidfile_fileno(const struct pidfh *_pfh);
1271eaf0ac3Slogwang struct pidfh *
1281eaf0ac3Slogwang 	pidfile_open(const char *_path, mode_t _mode, pid_t *_pidptr);
1291eaf0ac3Slogwang int	pidfile_remove(struct pidfh *_pfh);
1301eaf0ac3Slogwang int	pidfile_write(struct pidfh *_pfh);
1311eaf0ac3Slogwang void	properties_free(properties _list);
1321eaf0ac3Slogwang char	*property_find(properties _list, const char *_name);
1331eaf0ac3Slogwang properties
1341eaf0ac3Slogwang 	properties_read(int _fd);
1351eaf0ac3Slogwang int	realhostname(char *_host, size_t _hsize, const struct in_addr *_ip);
1361eaf0ac3Slogwang int	realhostname_sa(char *_host, size_t _hsize, struct sockaddr *_addr,
1371eaf0ac3Slogwang 	    int _addrlen);
1381eaf0ac3Slogwang int	_secure_path(const char *_path, uid_t _uid, gid_t _gid);
1391eaf0ac3Slogwang void	trimdomain(char *_fullhost, int _hostsize);
1401eaf0ac3Slogwang const char *
1411eaf0ac3Slogwang 	uu_lockerr(int _uu_lockresult);
1421eaf0ac3Slogwang int	uu_lock(const char *_ttyname);
1431eaf0ac3Slogwang int	uu_unlock(const char *_ttyname);
1441eaf0ac3Slogwang int	uu_lock_txfr(const char *_ttyname, pid_t _pid);
1451eaf0ac3Slogwang 
1461eaf0ac3Slogwang /*
1471eaf0ac3Slogwang  * Conditionally prototype the following functions if the include
1481eaf0ac3Slogwang  * files upon which they depend have been included.
1491eaf0ac3Slogwang  */
1501eaf0ac3Slogwang #ifdef _STDIO_H_
1511eaf0ac3Slogwang char	*fparseln(FILE *_fp, size_t *_len, size_t *_lineno,
1521eaf0ac3Slogwang 	    const char _delim[3], int _flags);
1531eaf0ac3Slogwang #endif
1541eaf0ac3Slogwang 
1551eaf0ac3Slogwang #ifdef _PWD_H_
1561eaf0ac3Slogwang int	pw_copy(int _ffd, int _tfd, const struct passwd *_pw,
1571eaf0ac3Slogwang 	    struct passwd *_old_pw);
1581eaf0ac3Slogwang struct passwd
1591eaf0ac3Slogwang 	*pw_dup(const struct passwd *_pw);
1601eaf0ac3Slogwang int	pw_edit(int _notsetuid);
1611eaf0ac3Slogwang int	pw_equal(const struct passwd *_pw1, const struct passwd *_pw2);
1621eaf0ac3Slogwang void	pw_fini(void);
1631eaf0ac3Slogwang int	pw_init(const char *_dir, const char *_master);
16422ce4affSfengbojiang void	pw_initpwd(struct passwd *_pw);
1651eaf0ac3Slogwang char	*pw_make(const struct passwd *_pw);
1661eaf0ac3Slogwang char	*pw_make_v7(const struct passwd *_pw);
1671eaf0ac3Slogwang int	pw_mkdb(const char *_user);
1681eaf0ac3Slogwang int	pw_lock(void);
1691eaf0ac3Slogwang struct passwd *
1701eaf0ac3Slogwang 	pw_scan(const char *_line, int _flags);
1711eaf0ac3Slogwang const char *
1721eaf0ac3Slogwang 	pw_tempname(void);
1731eaf0ac3Slogwang int	pw_tmp(int _mfd);
1741eaf0ac3Slogwang #endif
1751eaf0ac3Slogwang 
1761eaf0ac3Slogwang #ifdef _GRP_H_
1771eaf0ac3Slogwang int 	gr_copy(int __ffd, int _tfd, const struct group *_gr,
1781eaf0ac3Slogwang 	    struct group *_old_gr);
1791eaf0ac3Slogwang struct group *
1801eaf0ac3Slogwang 	gr_dup(const struct group *_gr);
1811eaf0ac3Slogwang struct group *
1821eaf0ac3Slogwang 	gr_add(const struct group *_gr, const char *_newmember);
1831eaf0ac3Slogwang int	gr_equal(const struct group *_gr1, const struct group *_gr2);
1841eaf0ac3Slogwang void	gr_fini(void);
1851eaf0ac3Slogwang int	gr_init(const char *_dir, const char *_master);
1861eaf0ac3Slogwang int	gr_lock(void);
1871eaf0ac3Slogwang char	*gr_make(const struct group *_gr);
1881eaf0ac3Slogwang int	gr_mkdb(void);
1891eaf0ac3Slogwang struct group *
1901eaf0ac3Slogwang 	gr_scan(const char *_line);
1911eaf0ac3Slogwang int	gr_tmp(int _mdf);
1921eaf0ac3Slogwang #endif
1931eaf0ac3Slogwang 
1941eaf0ac3Slogwang #ifdef _UFS_UFS_QUOTA_H_
1951eaf0ac3Slogwang struct fstab;
1961eaf0ac3Slogwang struct quotafile;
1971eaf0ac3Slogwang int	quota_check_path(const struct quotafile *_qf, const char *_path);
1981eaf0ac3Slogwang void	quota_close(struct quotafile *_qf);
1991eaf0ac3Slogwang int	quota_convert(struct quotafile *_qf, int _wordsize);
2001eaf0ac3Slogwang const char *
2011eaf0ac3Slogwang 	quota_fsname(const struct quotafile *_qf);
2021eaf0ac3Slogwang int	quota_maxid(struct quotafile *_qf);
2031eaf0ac3Slogwang int	quota_off(struct quotafile *_qf);
2041eaf0ac3Slogwang int	quota_on(struct quotafile *_qf);
2051eaf0ac3Slogwang struct quotafile *
2061eaf0ac3Slogwang 	quota_open(struct fstab *_fs, int _quotatype, int _openflags);
2071eaf0ac3Slogwang const char *
2081eaf0ac3Slogwang 	quota_qfname(const struct quotafile *_qf);
2091eaf0ac3Slogwang int	quota_read(struct quotafile *_qf, struct dqblk *_dqb, int _id);
2101eaf0ac3Slogwang int	quota_write_limits(struct quotafile *_qf, struct dqblk *_dqb, int _id);
2111eaf0ac3Slogwang int	quota_write_usage(struct quotafile *_qf, struct dqblk *_dqb, int _id);
2121eaf0ac3Slogwang #endif
2131eaf0ac3Slogwang 
2141eaf0ac3Slogwang __END_DECLS
2151eaf0ac3Slogwang 
2161eaf0ac3Slogwang /* fparseln(3) */
2171eaf0ac3Slogwang #define	FPARSELN_UNESCESC	0x01
2181eaf0ac3Slogwang #define	FPARSELN_UNESCCONT	0x02
2191eaf0ac3Slogwang #define	FPARSELN_UNESCCOMM	0x04
2201eaf0ac3Slogwang #define	FPARSELN_UNESCREST	0x08
2211eaf0ac3Slogwang #define	FPARSELN_UNESCALL	0x0f
2221eaf0ac3Slogwang 
2231eaf0ac3Slogwang /* Flags for hexdump(3). */
2241eaf0ac3Slogwang #define	HD_COLUMN_MASK		0xff
2251eaf0ac3Slogwang #define	HD_DELIM_MASK		0xff00
2261eaf0ac3Slogwang #define	HD_OMIT_COUNT		(1 << 16)
2271eaf0ac3Slogwang #define	HD_OMIT_HEX		(1 << 17)
2281eaf0ac3Slogwang #define	HD_OMIT_CHARS		(1 << 18)
2291eaf0ac3Slogwang 
2301eaf0ac3Slogwang /* Values for humanize_number(3)'s flags parameter. */
2311eaf0ac3Slogwang #define	HN_DECIMAL		0x01
2321eaf0ac3Slogwang #define	HN_NOSPACE		0x02
2331eaf0ac3Slogwang #define	HN_B			0x04
2341eaf0ac3Slogwang #define	HN_DIVISOR_1000		0x08
2351eaf0ac3Slogwang #define	HN_IEC_PREFIXES		0x10
2361eaf0ac3Slogwang 
2371eaf0ac3Slogwang /* Values for humanize_number(3)'s scale parameter. */
2381eaf0ac3Slogwang #define	HN_GETSCALE		0x10
2391eaf0ac3Slogwang #define	HN_AUTOSCALE		0x20
2401eaf0ac3Slogwang 
2411eaf0ac3Slogwang /* Return values from realhostname(). */
2421eaf0ac3Slogwang #define	HOSTNAME_FOUND		0
2431eaf0ac3Slogwang #define	HOSTNAME_INCORRECTNAME	1
2441eaf0ac3Slogwang #define	HOSTNAME_INVALIDADDR	2
2451eaf0ac3Slogwang #define	HOSTNAME_INVALIDNAME	3
2461eaf0ac3Slogwang 
2471eaf0ac3Slogwang /* Flags for pw_scan(). */
2481eaf0ac3Slogwang #define	PWSCAN_MASTER		0x01
2491eaf0ac3Slogwang #define	PWSCAN_WARN		0x02
2501eaf0ac3Slogwang 
2511eaf0ac3Slogwang /* Return values from uu_lock(). */
2521eaf0ac3Slogwang #define	UU_LOCK_INUSE		1
2531eaf0ac3Slogwang #define	UU_LOCK_OK		0
2541eaf0ac3Slogwang #define	UU_LOCK_OPEN_ERR	(-1)
2551eaf0ac3Slogwang #define	UU_LOCK_READ_ERR	(-2)
2561eaf0ac3Slogwang #define	UU_LOCK_CREAT_ERR	(-3)
2571eaf0ac3Slogwang #define	UU_LOCK_WRITE_ERR	(-4)
2581eaf0ac3Slogwang #define	UU_LOCK_LINK_ERR	(-5)
2591eaf0ac3Slogwang #define	UU_LOCK_TRY_ERR		(-6)
2601eaf0ac3Slogwang #define	UU_LOCK_OWNER_ERR	(-7)
2611eaf0ac3Slogwang 
2621eaf0ac3Slogwang #endif /* !_LIBUTIL_H_ */
263