sysutil.h 15 KB

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