]> git.proxmox.com Git - mirror_lxcfs.git/blob - src/utils.h
utils: add safe_uint32() helper
[mirror_lxcfs.git] / src / utils.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef __LXCFS_UTILS_H
4 #define __LXCFS_UTILS_H
5
6 #include "config.h"
7
8 #include <signal.h>
9 #include <stdbool.h>
10 #include <sys/socket.h>
11 #include <sys/types.h>
12 #include <sys/un.h>
13 #include <sys/syscall.h>
14 #include <unistd.h>
15
16 #include "lxcfs_fuse.h"
17
18 #include "macro.h"
19 #include "syscall_numbers.h"
20
21 /* Reserve buffer size to account for file size changes. */
22 #define BUF_RESERVE_SIZE 512
23
24 #define SEND_CREDS_OK 0
25 #define SEND_CREDS_NOTSK 1
26 #define SEND_CREDS_FAIL 2
27
28 struct file_info;
29
30 __attribute__((__format__(__printf__, 4, 5))) extern char *must_strcat(char **src, size_t *sz, size_t *asz, const char *format, ...);
31 extern bool is_shared_pidns(pid_t pid);
32 extern int preserve_ns(const int pid, const char *ns);
33 extern void do_release_file_info(struct fuse_file_info *fi);
34 extern bool recv_creds(int sock, struct ucred *cred, char *v);
35 extern int send_creds(int sock, struct ucred *cred, char v, bool pingfirst);
36 extern bool wait_for_sock(int sock, int timeout);
37 extern int read_file_fuse(const char *path, char *buf, size_t size,
38 struct file_info *d);
39 extern int read_file_fuse_with_offset(const char *path, char *buf, size_t size,
40 off_t offset, struct file_info *d);
41 extern void prune_init_slice(char *cg);
42 extern int wait_for_pid(pid_t pid);
43
44 #if !HAVE_PIDFD_OPEN
45 static inline int pidfd_open(pid_t pid, unsigned int flags)
46 {
47 return syscall(__NR_pidfd_open, pid, flags);
48 }
49 #endif
50
51 #if !HAVE_PIDFD_SEND_SIGNAL
52 static inline int pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
53 unsigned int flags)
54 {
55 return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
56 }
57 #endif
58
59 extern FILE *fopen_cached(const char *path, const char *mode,
60 void **caller_freed_buffer);
61 extern FILE *fdopen_cached(int fd, const char *mode, void **caller_freed_buffer);
62 extern DIR *opendir_flags(const char *path, int oflags);
63 extern ssize_t write_nointr(int fd, const void *buf, size_t count);
64 extern int safe_uint64(const char *numstr, uint64_t *converted, int base);
65 extern int safe_uint32(const char *numstr, uint32_t *converted, int base);
66 extern char *trim_whitespace_in_place(char *buffer);
67
68 static inline bool file_exists(const char *f)
69 {
70 struct stat statbuf;
71
72 return stat(f, &statbuf) == 0;
73 }
74
75 #define PROTECT_OPEN_WITH_TRAILING_SYMLINKS (O_CLOEXEC | O_NOCTTY | O_RDONLY)
76 #define PROTECT_OPEN (PROTECT_OPEN_WITH_TRAILING_SYMLINKS | O_NOFOLLOW)
77 extern char *read_file_at(int dfd, const char *fnam, unsigned int o_flags);
78
79 #endif /* __LXCFS_UTILS_H */