xref: /linux-6.15/include/linux/kmod.h (revision ae3cef73)
1 #ifndef __LINUX_KMOD_H__
2 #define __LINUX_KMOD_H__
3 
4 /*
5  *	include/linux/kmod.h
6  *
7  *      This program is free software; you can redistribute it and/or modify
8  *      it under the terms of the GNU General Public License as published by
9  *      the Free Software Foundation; either version 2 of the License, or
10  *      (at your option) any later version.
11  *
12  *      This program is distributed in the hope that it will be useful,
13  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *      GNU General Public License for more details.
16  *
17  *      You should have received a copy of the GNU General Public License
18  *      along with this program; if not, write to the Free Software
19  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include <linux/gfp.h>
23 #include <linux/stddef.h>
24 #include <linux/errno.h>
25 #include <linux/compiler.h>
26 #include <linux/workqueue.h>
27 #include <linux/sysctl.h>
28 
29 #define KMOD_PATH_LEN 256
30 
31 #ifdef CONFIG_MODULES
32 extern char modprobe_path[]; /* for sysctl */
33 /* modprobe exit status on success, -ve on error.  Return value
34  * usually useless though. */
35 extern __printf(2, 3)
36 int __request_module(bool wait, const char *name, ...);
37 #define request_module(mod...) __request_module(true, mod)
38 #define request_module_nowait(mod...) __request_module(false, mod)
39 #define try_then_request_module(x, mod...) \
40 	((x) ?: (__request_module(true, mod), (x)))
41 #else
42 static inline int request_module(const char *name, ...) { return -ENOSYS; }
43 static inline int request_module_nowait(const char *name, ...) { return -ENOSYS; }
44 #define try_then_request_module(x, mod...) (x)
45 #endif
46 
47 
48 struct cred;
49 struct file;
50 
51 #define UMH_NO_WAIT	0	/* don't wait at all */
52 #define UMH_WAIT_EXEC	1	/* wait for the exec, but not the process */
53 #define UMH_WAIT_PROC	2	/* wait for the process to complete */
54 #define UMH_KILLABLE	4	/* wait for EXEC/PROC killable */
55 
56 struct subprocess_info {
57 	struct work_struct work;
58 	struct completion *complete;
59 	char *path;
60 	char **argv;
61 	char **envp;
62 	int wait;
63 	int retval;
64 	int (*init)(struct subprocess_info *info, struct cred *new);
65 	void (*cleanup)(struct subprocess_info *info);
66 	void *data;
67 };
68 
69 /* Allocate a subprocess_info structure */
70 struct subprocess_info *call_usermodehelper_setup(char *path, char **argv,
71 						  char **envp, gfp_t gfp_mask);
72 
73 /* Set various pieces of state into the subprocess_info structure */
74 void call_usermodehelper_setfns(struct subprocess_info *info,
75 		    int (*init)(struct subprocess_info *info, struct cred *new),
76 		    void (*cleanup)(struct subprocess_info *info),
77 		    void *data);
78 
79 /* Actually execute the sub-process */
80 int call_usermodehelper_exec(struct subprocess_info *info, int wait);
81 
82 static inline int
83 call_usermodehelper_fns(char *path, char **argv, char **envp, int wait,
84 			int (*init)(struct subprocess_info *info, struct cred *new),
85 			void (*cleanup)(struct subprocess_info *), void *data)
86 {
87 	struct subprocess_info *info;
88 	gfp_t gfp_mask = (wait == UMH_NO_WAIT) ? GFP_ATOMIC : GFP_KERNEL;
89 
90 	info = call_usermodehelper_setup(path, argv, envp, gfp_mask);
91 
92 	if (info == NULL)
93 		return -ENOMEM;
94 
95 	call_usermodehelper_setfns(info, init, cleanup, data);
96 
97 	return call_usermodehelper_exec(info, wait);
98 }
99 
100 static inline int
101 call_usermodehelper(char *path, char **argv, char **envp, int wait)
102 {
103 	return call_usermodehelper_fns(path, argv, envp, wait,
104 				       NULL, NULL, NULL);
105 }
106 
107 extern struct ctl_table usermodehelper_table[];
108 
109 enum umh_disable_depth {
110 	UMH_ENABLED = 0,
111 	UMH_FREEZING,
112 	UMH_DISABLED,
113 };
114 
115 extern void usermodehelper_init(void);
116 
117 extern int __usermodehelper_disable(enum umh_disable_depth depth);
118 extern void __usermodehelper_set_disable_depth(enum umh_disable_depth depth);
119 
120 static inline int usermodehelper_disable(void)
121 {
122 	return __usermodehelper_disable(UMH_DISABLED);
123 }
124 
125 static inline void usermodehelper_enable(void)
126 {
127 	__usermodehelper_set_disable_depth(UMH_ENABLED);
128 }
129 
130 extern int usermodehelper_read_trylock(void);
131 extern long usermodehelper_read_lock_wait(long timeout);
132 extern void usermodehelper_read_unlock(void);
133 
134 #endif /* __LINUX_KMOD_H__ */
135