File tree Expand file tree Collapse file tree 2 files changed +28
-5
lines changed Expand file tree Collapse file tree 2 files changed +28
-5
lines changed Original file line number Diff line number Diff line change 63
63
64
64
#define __free (_name ) __cleanup(__free_##_name)
65
65
66
- #define __get_and_null_ptr (p ) \
67
- ({ __auto_type __ptr = &(p); \
68
- __auto_type __val = *__ptr; \
69
- *__ptr = NULL; __val; })
66
+ #define __get_and_null (p , nullvalue ) \
67
+ ({ \
68
+ __auto_type __ptr = &(p); \
69
+ __auto_type __val = *__ptr; \
70
+ *__ptr = nullvalue; \
71
+ __val; \
72
+ })
70
73
71
74
static inline __must_check
72
75
const volatile void * __must_check_fn (const volatile void * val )
73
76
{ return val ; }
74
77
75
78
#define no_free_ptr (p ) \
76
- ((typeof(p)) __must_check_fn(__get_and_null_ptr(p )))
79
+ ((typeof(p)) __must_check_fn(__get_and_null(p, NULL )))
77
80
78
81
#define return_ptr (p ) return no_free_ptr(p)
79
82
Original file line number Diff line number Diff line change @@ -97,6 +97,26 @@ extern void put_unused_fd(unsigned int fd);
97
97
DEFINE_CLASS (get_unused_fd , int , if (_T >= 0 ) put_unused_fd (_T ),
98
98
get_unused_fd_flags (flags ), unsigned flags )
99
99
100
+ /*
101
+ * take_fd() will take care to set @fd to -EBADF ensuring that
102
+ * CLASS(get_unused_fd) won't call put_unused_fd(). This makes it
103
+ * easier to rely on CLASS(get_unused_fd):
104
+ *
105
+ * struct file *f;
106
+ *
107
+ * CLASS(get_unused_fd, fd)(O_CLOEXEC);
108
+ * if (fd < 0)
109
+ * return fd;
110
+ *
111
+ * f = dentry_open(&path, O_RDONLY, current_cred());
112
+ * if (IS_ERR(f))
113
+ * return PTR_ERR(fd);
114
+ *
115
+ * fd_install(fd, f);
116
+ * return take_fd(fd);
117
+ */
118
+ #define take_fd (fd ) __get_and_null(fd, -EBADF)
119
+
100
120
extern void fd_install (unsigned int fd , struct file * file );
101
121
102
122
int receive_fd (struct file * file , int __user * ufd , unsigned int o_flags );
You can’t perform that action at this time.
0 commit comments