aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/kmod.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/kmod.c')
-rw-r--r--kernel/kmod.c216
1 files changed, 150 insertions, 66 deletions
diff --git a/kernel/kmod.c b/kernel/kmod.c
index 4d32eb077179..78d365c524ed 100644
--- a/kernel/kmod.c
+++ b/kernel/kmod.c
@@ -119,9 +119,10 @@ struct subprocess_info {
119 char **argv; 119 char **argv;
120 char **envp; 120 char **envp;
121 struct key *ring; 121 struct key *ring;
122 int wait; 122 enum umh_wait wait;
123 int retval; 123 int retval;
124 struct file *stdin; 124 struct file *stdin;
125 void (*cleanup)(char **argv, char **envp);
125}; 126};
126 127
127/* 128/*
@@ -180,6 +181,14 @@ static int ____call_usermodehelper(void *data)
180 do_exit(0); 181 do_exit(0);
181} 182}
182 183
184void call_usermodehelper_freeinfo(struct subprocess_info *info)
185{
186 if (info->cleanup)
187 (*info->cleanup)(info->argv, info->envp);
188 kfree(info);
189}
190EXPORT_SYMBOL(call_usermodehelper_freeinfo);
191
183/* Keventd can't block, but this (a child) can. */ 192/* Keventd can't block, but this (a child) can. */
184static int wait_for_helper(void *data) 193static int wait_for_helper(void *data)
185{ 194{
@@ -216,8 +225,8 @@ static int wait_for_helper(void *data)
216 sub_info->retval = ret; 225 sub_info->retval = ret;
217 } 226 }
218 227
219 if (sub_info->wait < 0) 228 if (sub_info->wait == UMH_NO_WAIT)
220 kfree(sub_info); 229 call_usermodehelper_freeinfo(sub_info);
221 else 230 else
222 complete(sub_info->complete); 231 complete(sub_info->complete);
223 return 0; 232 return 0;
@@ -229,34 +238,122 @@ static void __call_usermodehelper(struct work_struct *work)
229 struct subprocess_info *sub_info = 238 struct subprocess_info *sub_info =
230 container_of(work, struct subprocess_info, work); 239 container_of(work, struct subprocess_info, work);
231 pid_t pid; 240 pid_t pid;
232 int wait = sub_info->wait; 241 enum umh_wait wait = sub_info->wait;
233 242
234 /* CLONE_VFORK: wait until the usermode helper has execve'd 243 /* CLONE_VFORK: wait until the usermode helper has execve'd
235 * successfully We need the data structures to stay around 244 * successfully We need the data structures to stay around
236 * until that is done. */ 245 * until that is done. */
237 if (wait) 246 if (wait == UMH_WAIT_PROC || wait == UMH_NO_WAIT)
238 pid = kernel_thread(wait_for_helper, sub_info, 247 pid = kernel_thread(wait_for_helper, sub_info,
239 CLONE_FS | CLONE_FILES | SIGCHLD); 248 CLONE_FS | CLONE_FILES | SIGCHLD);
240 else 249 else
241 pid = kernel_thread(____call_usermodehelper, sub_info, 250 pid = kernel_thread(____call_usermodehelper, sub_info,
242 CLONE_VFORK | SIGCHLD); 251 CLONE_VFORK | SIGCHLD);
243 252
244 if (wait < 0) 253 switch (wait) {
245 return; 254 case UMH_NO_WAIT:
255 break;
246 256
247 if (pid < 0) { 257 case UMH_WAIT_PROC:
258 if (pid > 0)
259 break;
248 sub_info->retval = pid; 260 sub_info->retval = pid;
261 /* FALLTHROUGH */
262
263 case UMH_WAIT_EXEC:
249 complete(sub_info->complete); 264 complete(sub_info->complete);
250 } else if (!wait) 265 }
251 complete(sub_info->complete); 266}
267
268/**
269 * call_usermodehelper_setup - prepare to call a usermode helper
270 * @path - path to usermode executable
271 * @argv - arg vector for process
272 * @envp - environment for process
273 *
274 * Returns either NULL on allocation failure, or a subprocess_info
275 * structure. This should be passed to call_usermodehelper_exec to
276 * exec the process and free the structure.
277 */
278struct subprocess_info *call_usermodehelper_setup(char *path,
279 char **argv, char **envp)
280{
281 struct subprocess_info *sub_info;
282 sub_info = kzalloc(sizeof(struct subprocess_info), GFP_ATOMIC);
283 if (!sub_info)
284 goto out;
285
286 INIT_WORK(&sub_info->work, __call_usermodehelper);
287 sub_info->path = path;
288 sub_info->argv = argv;
289 sub_info->envp = envp;
290
291 out:
292 return sub_info;
252} 293}
294EXPORT_SYMBOL(call_usermodehelper_setup);
253 295
254/** 296/**
255 * call_usermodehelper_keys - start a usermode application 297 * call_usermodehelper_setkeys - set the session keys for usermode helper
256 * @path: pathname for the application 298 * @info: a subprocess_info returned by call_usermodehelper_setup
257 * @argv: null-terminated argument list 299 * @session_keyring: the session keyring for the process
258 * @envp: null-terminated environment list 300 */
259 * @session_keyring: session keyring for process (NULL for an empty keyring) 301void call_usermodehelper_setkeys(struct subprocess_info *info,
302 struct key *session_keyring)
303{
304 info->ring = session_keyring;
305}
306EXPORT_SYMBOL(call_usermodehelper_setkeys);
307
308/**
309 * call_usermodehelper_setcleanup - set a cleanup function
310 * @info: a subprocess_info returned by call_usermodehelper_setup
311 * @cleanup: a cleanup function
312 *
313 * The cleanup function is just befor ethe subprocess_info is about to
314 * be freed. This can be used for freeing the argv and envp. The
315 * Function must be runnable in either a process context or the
316 * context in which call_usermodehelper_exec is called.
317 */
318void call_usermodehelper_setcleanup(struct subprocess_info *info,
319 void (*cleanup)(char **argv, char **envp))
320{
321 info->cleanup = cleanup;
322}
323EXPORT_SYMBOL(call_usermodehelper_setcleanup);
324
325/**
326 * call_usermodehelper_stdinpipe - set up a pipe to be used for stdin
327 * @sub_info: a subprocess_info returned by call_usermodehelper_setup
328 * @filp: set to the write-end of a pipe
329 *
330 * This constructs a pipe, and sets the read end to be the stdin of the
331 * subprocess, and returns the write-end in *@filp.
332 */
333int call_usermodehelper_stdinpipe(struct subprocess_info *sub_info,
334 struct file **filp)
335{
336 struct file *f;
337
338 f = create_write_pipe();
339 if (IS_ERR(f))
340 return PTR_ERR(f);
341 *filp = f;
342
343 f = create_read_pipe(f);
344 if (IS_ERR(f)) {
345 free_write_pipe(*filp);
346 return PTR_ERR(f);
347 }
348 sub_info->stdin = f;
349
350 return 0;
351}
352EXPORT_SYMBOL(call_usermodehelper_stdinpipe);
353
354/**
355 * call_usermodehelper_exec - start a usermode application
356 * @sub_info: information about the subprocessa
260 * @wait: wait for the application to finish and return status. 357 * @wait: wait for the application to finish and return status.
261 * when -1 don't wait at all, but you get no useful error back when 358 * when -1 don't wait at all, but you get no useful error back when
262 * the program couldn't be exec'ed. This makes it safe to call 359 * the program couldn't be exec'ed. This makes it safe to call
@@ -265,81 +362,68 @@ static void __call_usermodehelper(struct work_struct *work)
265 * Runs a user-space application. The application is started 362 * Runs a user-space application. The application is started
266 * asynchronously if wait is not set, and runs as a child of keventd. 363 * asynchronously if wait is not set, and runs as a child of keventd.
267 * (ie. it runs with full root capabilities). 364 * (ie. it runs with full root capabilities).
268 *
269 * Must be called from process context. Returns a negative error code
270 * if program was not execed successfully, or 0.
271 */ 365 */
272int call_usermodehelper_keys(char *path, char **argv, char **envp, 366int call_usermodehelper_exec(struct subprocess_info *sub_info,
273 struct key *session_keyring, int wait) 367 enum umh_wait wait)
274{ 368{
275 DECLARE_COMPLETION_ONSTACK(done); 369 DECLARE_COMPLETION_ONSTACK(done);
276 struct subprocess_info *sub_info;
277 int retval; 370 int retval;
278 371
279 if (!khelper_wq) 372 if (sub_info->path[0] == '\0') {
280 return -EBUSY; 373 retval = 0;
281 374 goto out;
282 if (path[0] == '\0') 375 }
283 return 0;
284 376
285 sub_info = kzalloc(sizeof(struct subprocess_info), GFP_ATOMIC); 377 if (!khelper_wq) {
286 if (!sub_info) 378 retval = -EBUSY;
287 return -ENOMEM; 379 goto out;
380 }
288 381
289 INIT_WORK(&sub_info->work, __call_usermodehelper);
290 sub_info->complete = &done; 382 sub_info->complete = &done;
291 sub_info->path = path;
292 sub_info->argv = argv;
293 sub_info->envp = envp;
294 sub_info->ring = session_keyring;
295 sub_info->wait = wait; 383 sub_info->wait = wait;
296 384
297 queue_work(khelper_wq, &sub_info->work); 385 queue_work(khelper_wq, &sub_info->work);
298 if (wait < 0) /* task has freed sub_info */ 386 if (wait == UMH_NO_WAIT) /* task has freed sub_info */
299 return 0; 387 return 0;
300 wait_for_completion(&done); 388 wait_for_completion(&done);
301 retval = sub_info->retval; 389 retval = sub_info->retval;
302 kfree(sub_info); 390
391 out:
392 call_usermodehelper_freeinfo(sub_info);
303 return retval; 393 return retval;
304} 394}
305EXPORT_SYMBOL(call_usermodehelper_keys); 395EXPORT_SYMBOL(call_usermodehelper_exec);
306 396
397/**
398 * call_usermodehelper_pipe - call a usermode helper process with a pipe stdin
399 * @path: path to usermode executable
400 * @argv: arg vector for process
401 * @envp: environment for process
402 * @filp: set to the write-end of a pipe
403 *
404 * This is a simple wrapper which executes a usermode-helper function
405 * with a pipe as stdin. It is implemented entirely in terms of
406 * lower-level call_usermodehelper_* functions.
407 */
307int call_usermodehelper_pipe(char *path, char **argv, char **envp, 408int call_usermodehelper_pipe(char *path, char **argv, char **envp,
308 struct file **filp) 409 struct file **filp)
309{ 410{
310 DECLARE_COMPLETION(done); 411 struct subprocess_info *sub_info;
311 struct subprocess_info sub_info = { 412 int ret;
312 .work = __WORK_INITIALIZER(sub_info.work,
313 __call_usermodehelper),
314 .complete = &done,
315 .path = path,
316 .argv = argv,
317 .envp = envp,
318 .retval = 0,
319 };
320 struct file *f;
321 413
322 if (!khelper_wq) 414 sub_info = call_usermodehelper_setup(path, argv, envp);
323 return -EBUSY; 415 if (sub_info == NULL)
416 return -ENOMEM;
324 417
325 if (path[0] == '\0') 418 ret = call_usermodehelper_stdinpipe(sub_info, filp);
326 return 0; 419 if (ret < 0)
420 goto out;
327 421
328 f = create_write_pipe(); 422 return call_usermodehelper_exec(sub_info, 1);
329 if (IS_ERR(f))
330 return PTR_ERR(f);
331 *filp = f;
332
333 f = create_read_pipe(f);
334 if (IS_ERR(f)) {
335 free_write_pipe(*filp);
336 return PTR_ERR(f);
337 }
338 sub_info.stdin = f;
339 423
340 queue_work(khelper_wq, &sub_info.work); 424 out:
341 wait_for_completion(&done); 425 call_usermodehelper_freeinfo(sub_info);
342 return sub_info.retval; 426 return ret;
343} 427}
344EXPORT_SYMBOL(call_usermodehelper_pipe); 428EXPORT_SYMBOL(call_usermodehelper_pipe);
345 429