4 /* TODO: these functions need proper documenting! */
10 /* Return value queries */
11 int vsf_sysutil_retval_is_error(int retval
);
14 kVSFSysUtilErrUnknown
= 1,
15 kVSFSysUtilErrADDRINUSE
,
19 kVSFSysUtilErrOPNOTSUPP
,
23 enum EVSFSysUtilError
vsf_sysutil_get_error(void);
25 /* Signal handling utility functions */
26 enum EVSFSysUtilSignal
28 kVSFSysUtilSigALRM
= 1,
35 enum EVSFSysUtilInterruptContext
40 typedef void (*vsf_sighandle_t
)(void*);
41 typedef void (*vsf_async_sighandle_t
)(int);
42 typedef void (*vsf_context_io_t
)(int, int, void*);
44 void vsf_sysutil_install_null_sighandler(const enum EVSFSysUtilSignal sig
);
45 void vsf_sysutil_install_sighandler(const enum EVSFSysUtilSignal
,
46 vsf_sighandle_t handler
,
49 void vsf_sysutil_install_async_sighandler(const enum EVSFSysUtilSignal sig
,
50 vsf_async_sighandle_t handler
);
51 void vsf_sysutil_default_sig(const enum EVSFSysUtilSignal sig
);
52 void vsf_sysutil_install_io_handler(vsf_context_io_t handler
, void* p_private
);
53 void vsf_sysutil_uninstall_io_handler(void);
54 void vsf_sysutil_check_pending_actions(
55 const enum EVSFSysUtilInterruptContext context
, int retval
, int fd
);
56 void vsf_sysutil_block_sig(const enum EVSFSysUtilSignal sig
);
57 void vsf_sysutil_unblock_sig(const enum EVSFSysUtilSignal sig
);
59 /* Alarm setting/clearing utility functions */
60 void vsf_sysutil_set_alarm(const unsigned int trigger_seconds
);
61 void vsf_sysutil_clear_alarm(void);
63 /* Directory related things */
64 char* vsf_sysutil_getcwd(char* p_dest
, const unsigned int buf_size
);
65 int vsf_sysutil_mkdir(const char* p_dirname
, const unsigned int mode
);
66 int vsf_sysutil_rmdir(const char* p_dirname
);
67 int vsf_sysutil_chdir(const char* p_dirname
);
68 int vsf_sysutil_rename(const char* p_from
, const char* p_to
);
70 struct vsf_sysutil_dir
;
71 struct vsf_sysutil_dir
* vsf_sysutil_opendir(const char* p_dirname
);
72 void vsf_sysutil_closedir(struct vsf_sysutil_dir
* p_dir
);
73 const char* vsf_sysutil_next_dirent(struct vsf_sysutil_dir
* p_dir
);
75 /* File create/open/close etc. */
76 enum EVSFSysUtilOpenMode
78 kVSFSysUtilOpenReadOnly
= 1,
79 kVSFSysUtilOpenWriteOnly
,
80 kVSFSysUtilOpenReadWrite
82 int vsf_sysutil_open_file(const char* p_filename
,
83 const enum EVSFSysUtilOpenMode
);
84 /* Fails if file already exists */
85 int vsf_sysutil_create_file_exclusive(const char* p_filename
);
86 /* Creates file or appends if already exists */
87 int vsf_sysutil_create_or_open_file_append(const char* p_filename
,
89 /* Creates or appends */
90 int vsf_sysutil_create_or_open_file(const char* p_filename
, unsigned int mode
);
91 void vsf_sysutil_dupfd2(int old_fd
, int new_fd
);
92 void vsf_sysutil_close(int fd
);
93 int vsf_sysutil_close_failok(int fd
);
94 int vsf_sysutil_unlink(const char* p_dead
);
95 int vsf_sysutil_write_access(const char* p_filename
);
96 void vsf_sysutil_ftruncate(int fd
);
98 /* Reading and writing */
99 void vsf_sysutil_lseek_to(const int fd
, filesize_t seek_pos
);
100 void vsf_sysutil_lseek_end(const int fd
);
101 filesize_t
vsf_sysutil_get_file_offset(const int file_fd
);
102 int vsf_sysutil_read(const int fd
, void* p_buf
, const unsigned int size
);
103 int vsf_sysutil_write(const int fd
, const void* p_buf
,
104 const unsigned int size
);
105 /* Reading and writing, with handling of interrupted system calls and partial
106 * reads/writes. Slightly more usable than the standard UNIX API!
108 int vsf_sysutil_read_loop(const int fd
, void* p_buf
, unsigned int size
);
109 int vsf_sysutil_write_loop(const int fd
, const void* p_buf
, unsigned int size
);
111 struct vsf_sysutil_statbuf
;
112 int vsf_sysutil_stat(const char* p_name
, struct vsf_sysutil_statbuf
** p_ptr
);
113 int vsf_sysutil_lstat(const char* p_name
, struct vsf_sysutil_statbuf
** p_ptr
);
114 void vsf_sysutil_fstat(int fd
, struct vsf_sysutil_statbuf
** p_ptr
);
115 void vsf_sysutil_dir_stat(const struct vsf_sysutil_dir
* p_dir
,
116 struct vsf_sysutil_statbuf
** p_ptr
);
117 int vsf_sysutil_statbuf_is_regfile(const struct vsf_sysutil_statbuf
* p_stat
);
118 int vsf_sysutil_statbuf_is_symlink(const struct vsf_sysutil_statbuf
* p_stat
);
119 int vsf_sysutil_statbuf_is_socket(const struct vsf_sysutil_statbuf
* p_stat
);
120 int vsf_sysutil_statbuf_is_dir(const struct vsf_sysutil_statbuf
* p_stat
);
121 filesize_t
vsf_sysutil_statbuf_get_size(
122 const struct vsf_sysutil_statbuf
* p_stat
);
123 const char* vsf_sysutil_statbuf_get_perms(
124 const struct vsf_sysutil_statbuf
* p_stat
);
125 const char* vsf_sysutil_statbuf_get_date(
126 const struct vsf_sysutil_statbuf
* p_stat
, int use_localtime
, long curr_time
);
127 const char* vsf_sysutil_statbuf_get_numeric_date(
128 const struct vsf_sysutil_statbuf
* p_stat
, int use_localtime
);
129 unsigned int vsf_sysutil_statbuf_get_links(
130 const struct vsf_sysutil_statbuf
* p_stat
);
131 int vsf_sysutil_statbuf_get_uid(const struct vsf_sysutil_statbuf
* p_stat
);
132 int vsf_sysutil_statbuf_get_gid(const struct vsf_sysutil_statbuf
* p_stat
);
133 int vsf_sysutil_statbuf_is_readable_other(
134 const struct vsf_sysutil_statbuf
* p_stat
);
135 const char* vsf_sysutil_statbuf_get_sortkey_mtime(
136 const struct vsf_sysutil_statbuf
* p_stat
);
138 int vsf_sysutil_chmod(const char* p_filename
, unsigned int mode
);
139 void vsf_sysutil_fchown(const int fd
, const int uid
, const int gid
);
140 void vsf_sysutil_fchmod(const int fd
, unsigned int mode
);
141 int vsf_sysutil_readlink(const char* p_filename
, char* p_dest
,
142 unsigned int bufsiz
);
144 /* Get / unget various locks. Lock gets are blocking. Write locks are
145 * exclusive; read locks are shared.
147 int vsf_sysutil_lock_file_write(int fd
);
148 int vsf_sysutil_lock_file_read(int fd
);
149 void vsf_sysutil_unlock_file(int fd
);
151 /* Mapping/unmapping */
152 enum EVSFSysUtilMapPermission
154 kVSFSysUtilMapProtReadOnly
= 1,
155 kVSFSysUtilMapProtNone
157 void vsf_sysutil_memprotect(void* p_addr
, unsigned int len
,
158 const enum EVSFSysUtilMapPermission perm
);
159 void vsf_sysutil_memunmap(void* p_start
, unsigned int length
);
161 /* Memory allocating/freeing */
162 void* vsf_sysutil_malloc(unsigned int size
);
163 void* vsf_sysutil_realloc(void* p_ptr
, unsigned int size
);
164 void vsf_sysutil_free(void* p_ptr
);
166 /* Process creation/exit/process handling */
167 unsigned int vsf_sysutil_getpid(void);
168 void vsf_sysutil_post_fork(void);
169 int vsf_sysutil_fork(void);
170 int vsf_sysutil_fork_failok(void);
171 void vsf_sysutil_exit(int exit_code
);
172 struct vsf_sysutil_wait_retval
174 int PRIVATE_HANDS_OFF_syscall_retval
;
175 int PRIVATE_HANDS_OFF_exit_status
;
177 struct vsf_sysutil_wait_retval
vsf_sysutil_wait(void);
178 int vsf_sysutil_wait_reap_one(void);
179 int vsf_sysutil_wait_get_retval(
180 const struct vsf_sysutil_wait_retval
* p_waitret
);
181 int vsf_sysutil_wait_exited_normally(
182 const struct vsf_sysutil_wait_retval
* p_waitret
);
183 int vsf_sysutil_wait_get_exitcode(
184 const struct vsf_sysutil_wait_retval
* p_waitret
);
186 /* Various string functions */
187 unsigned int vsf_sysutil_strlen(const char* p_text
);
188 char* vsf_sysutil_strdup(const char* p_str
);
189 void vsf_sysutil_memclr(void* p_dest
, unsigned int size
);
190 void vsf_sysutil_memcpy(void* p_dest
, const void* p_src
,
191 const unsigned int size
);
192 void vsf_sysutil_strcpy(char* p_dest
, const char* p_src
, unsigned int maxsize
);
193 int vsf_sysutil_memcmp(const void* p_src1
, const void* p_src2
,
195 int vsf_sysutil_strcmp(const char* p_src1
, const char* p_src2
);
196 int vsf_sysutil_atoi(const char* p_str
);
197 filesize_t
vsf_sysutil_a_to_filesize_t(const char* p_str
);
198 const char* vsf_sysutil_ulong_to_str(unsigned long the_ulong
);
199 const char* vsf_sysutil_filesize_t_to_str(filesize_t the_filesize
);
200 const char* vsf_sysutil_double_to_str(double the_double
);
201 const char* vsf_sysutil_uint_to_octal(unsigned int the_uint
);
202 unsigned int vsf_sysutil_octal_to_uint(const char* p_str
);
203 int vsf_sysutil_toupper(int the_char
);
204 int vsf_sysutil_isspace(int the_char
);
205 int vsf_sysutil_isprint(int the_char
);
206 int vsf_sysutil_isalnum(int the_char
);
207 int vsf_sysutil_isdigit(int the_char
);
209 /* Socket handling */
210 struct vsf_sysutil_sockaddr
;
211 struct vsf_sysutil_socketpair_retval
216 void vsf_sysutil_sockaddr_alloc(struct vsf_sysutil_sockaddr
** p_sockptr
);
217 void vsf_sysutil_sockaddr_clear(struct vsf_sysutil_sockaddr
** p_sockptr
);
218 void vsf_sysutil_sockaddr_alloc_ipv4(struct vsf_sysutil_sockaddr
** p_sockptr
);
219 void vsf_sysutil_sockaddr_alloc_ipv6(struct vsf_sysutil_sockaddr
** p_sockptr
);
220 void vsf_sysutil_sockaddr_clone(
221 struct vsf_sysutil_sockaddr
** p_sockptr
,
222 const struct vsf_sysutil_sockaddr
* p_src
);
223 int vsf_sysutil_sockaddr_addr_equal(const struct vsf_sysutil_sockaddr
* p1
,
224 const struct vsf_sysutil_sockaddr
* p2
);
225 int vsf_sysutil_sockaddr_is_ipv6(
226 const struct vsf_sysutil_sockaddr
* p_sockaddr
);
227 void vsf_sysutil_sockaddr_set_ipv4addr(struct vsf_sysutil_sockaddr
* p_sockptr
,
228 const unsigned char* p_raw
);
229 void vsf_sysutil_sockaddr_set_ipv6addr(struct vsf_sysutil_sockaddr
* p_sockptr
,
230 const unsigned char* p_raw
);
231 void vsf_sysutil_sockaddr_set_any(struct vsf_sysutil_sockaddr
* p_sockaddr
);
232 unsigned short vsf_sysutil_sockaddr_get_port(
233 const struct vsf_sysutil_sockaddr
* p_sockptr
);
234 void vsf_sysutil_sockaddr_set_port(struct vsf_sysutil_sockaddr
* p_sockptr
,
235 unsigned short the_port
);
236 int vsf_sysutil_is_port_reserved(unsigned short port
);
237 int vsf_sysutil_get_ipsock(const struct vsf_sysutil_sockaddr
* p_sockaddr
);
238 unsigned int vsf_sysutil_get_ipaddr_size(void);
239 void* vsf_sysutil_sockaddr_get_raw_addr(
240 struct vsf_sysutil_sockaddr
* p_sockaddr
);
241 const void* vsf_sysutil_sockaddr_ipv6_v4(
242 const struct vsf_sysutil_sockaddr
* p_sockaddr
);
243 const void* vsf_sysutil_sockaddr_ipv4_v6(
244 const struct vsf_sysutil_sockaddr
* p_sockaddr
);
245 int vsf_sysutil_get_ipv4_sock(void);
246 int vsf_sysutil_get_ipv6_sock(void);
247 struct vsf_sysutil_socketpair_retval
248 vsf_sysutil_unix_stream_socketpair(void);
249 int vsf_sysutil_bind(int fd
, const struct vsf_sysutil_sockaddr
* p_sockptr
);
250 int vsf_sysutil_listen(int fd
, const unsigned int backlog
);
251 void vsf_sysutil_getsockname(int fd
, struct vsf_sysutil_sockaddr
** p_sockptr
);
252 void vsf_sysutil_getpeername(int fd
, struct vsf_sysutil_sockaddr
** p_sockptr
);
253 int vsf_sysutil_accept_timeout(int fd
, struct vsf_sysutil_sockaddr
* p_sockaddr
,
254 unsigned int wait_seconds
);
255 int vsf_sysutil_connect_timeout(int fd
,
256 const struct vsf_sysutil_sockaddr
* p_sockaddr
,
257 unsigned int wait_seconds
);
258 void vsf_sysutil_dns_resolve(struct vsf_sysutil_sockaddr
** p_sockptr
,
260 /* Option setting on sockets */
261 void vsf_sysutil_activate_keepalive(int fd
);
262 void vsf_sysutil_set_iptos_throughput(int fd
);
263 void vsf_sysutil_activate_reuseaddr(int fd
);
264 void vsf_sysutil_set_nodelay(int fd
);
265 void vsf_sysutil_activate_sigurg(int fd
);
266 void vsf_sysutil_activate_oobinline(int fd
);
267 void vsf_sysutil_activate_linger(int fd
);
268 void vsf_sysutil_deactivate_linger_failok(int fd
);
269 void vsf_sysutil_activate_noblock(int fd
);
270 void vsf_sysutil_deactivate_noblock(int fd
);
271 /* This does SHUT_RDWR */
272 void vsf_sysutil_shutdown_failok(int fd
);
273 /* And this does SHUT_RD */
274 void vsf_sysutil_shutdown_read_failok(int fd
);
275 int vsf_sysutil_recv_peek(const int fd
, void* p_buf
, unsigned int len
);
277 const char* vsf_sysutil_inet_ntop(
278 const struct vsf_sysutil_sockaddr
* p_sockptr
);
279 const char* vsf_sysutil_inet_ntoa(const void* p_raw_addr
);
280 int vsf_sysutil_inet_aton(
281 const char* p_text
, struct vsf_sysutil_sockaddr
* p_addr
);
283 /* User database queries etc. */
284 struct vsf_sysutil_user
;
285 struct vsf_sysutil_group
;
287 struct vsf_sysutil_user
* vsf_sysutil_getpwuid(const int uid
);
288 struct vsf_sysutil_user
* vsf_sysutil_getpwnam(const char* p_user
);
289 const char* vsf_sysutil_user_getname(const struct vsf_sysutil_user
* p_user
);
290 const char* vsf_sysutil_user_get_homedir(
291 const struct vsf_sysutil_user
* p_user
);
292 int vsf_sysutil_user_getuid(const struct vsf_sysutil_user
* p_user
);
293 int vsf_sysutil_user_getgid(const struct vsf_sysutil_user
* p_user
);
295 struct vsf_sysutil_group
* vsf_sysutil_getgrgid(const int gid
);
296 const char* vsf_sysutil_group_getname(const struct vsf_sysutil_group
* p_group
);
298 /* More random things */
299 unsigned int vsf_sysutil_getpagesize(void);
300 unsigned char vsf_sysutil_get_random_byte(void);
301 unsigned int vsf_sysutil_get_umask(void);
302 void vsf_sysutil_set_umask(unsigned int umask
);
303 void vsf_sysutil_make_session_leader(void);
304 void vsf_sysutil_reopen_standard_fds(void);
305 void vsf_sysutil_tzset(void);
306 const char* vsf_sysutil_get_current_date(void);
307 void vsf_sysutil_qsort(void* p_base
, unsigned int num_elem
,
308 unsigned int elem_size
,
309 int (*p_compar
)(const void *, const void *));
310 char* vsf_sysutil_getenv(const char* p_var
);
311 typedef void (*exitfunc_t
)(void);
312 void vsf_sysutil_set_exit_func(exitfunc_t exitfunc
);
313 int vsf_sysutil_getuid(void);
315 /* Syslogging (bah) */
316 void vsf_sysutil_openlog(int force
);
317 void vsf_sysutil_syslog(const char* p_text
, int severe
);
318 void vsf_sysutil_closelog(void);
320 /* Credentials handling */
321 int vsf_sysutil_running_as_root(void);
322 void vsf_sysutil_setuid(const struct vsf_sysutil_user
* p_user
);
323 void vsf_sysutil_setgid(const struct vsf_sysutil_user
* p_user
);
324 void vsf_sysutil_setuid_numeric(int uid
);
325 void vsf_sysutil_setgid_numeric(int gid
);
326 int vsf_sysutil_geteuid(void);
327 int vsf_sysutil_getegid(void);
328 void vsf_sysutil_seteuid(const struct vsf_sysutil_user
* p_user
);
329 void vsf_sysutil_setegid(const struct vsf_sysutil_user
* p_user
);
330 void vsf_sysutil_seteuid_numeric(int uid
);
331 void vsf_sysutil_setegid_numeric(int gid
);
332 void vsf_sysutil_clear_supp_groups(void);
333 void vsf_sysutil_initgroups(const struct vsf_sysutil_user
* p_user
);
334 void vsf_sysutil_chroot(const char* p_root_path
);
337 /* Do not call get_time_usec() without calling get_time_sec()
338 * first otherwise you will get stale data.
340 long vsf_sysutil_get_time_sec(void);
341 long vsf_sysutil_get_time_usec(void);
342 long vsf_sysutil_parse_time(const char* p_text
);
343 void vsf_sysutil_sleep(double seconds
);
344 int vsf_sysutil_setmodtime(const char* p_file
, long the_time
, int is_localtime
);
347 void vsf_sysutil_set_address_space_limit(long bytes
);
348 void vsf_sysutil_set_no_fds(void);
349 void vsf_sysutil_set_no_procs(void);
351 #endif /* VSF_SYSUTIL_H */