21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

process is forked, the original process is used to execute another program or the new<br />

process is used to continue primary execution of the program. In other words, the<br />

new process is most often the worker process.<br />

#include <br />

#include <br />

pid_t spc_fork(void) {<br />

pid_t childpid;<br />

if ((childpid = fork( )) = = -1) return -1;<br />

/* Reseed PRNGs in both the parent and the child */<br />

/* See Chapter 11 for examples */<br />

/* If this is the parent process, there's nothing more to do */<br />

if (childpid != 0) return childpid;<br />

/* This is the child process */<br />

spc_sanitize_files( ); /* Close all open files. See Recipe 1.1 */<br />

spc_drop_privileges(1); /* Permanently drop privileges. See Recipe 1.3 */<br />

return 0;<br />

}<br />

See Also<br />

Recipes 1.3, 1.5, 1.7<br />

1.7 Executing External Programs Securely<br />

<strong>Problem</strong><br />

Your Unix program needs to execute another program.<br />

Solution<br />

On Unix, one of the exec*( ) family of functions is used to replace the current program<br />

within a process with another program. Typically, when you’re executing<br />

another program, the original program continues to run while the new program is<br />

executed, thus requiring two processes to achieve the desired effect. The exec*( )<br />

functions do not create a new process. Instead, you must first use fork( ) to create a<br />

new process, and then use one of the exec*( ) functions in the new process to run the<br />

new program. See Recipe 1.6 for a discussion of using fork( ) securely.<br />

28 | Chapter 1: Safe Initialization<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!