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.

dup2(stdout_pipe[1], 1);<br />

close(stdout_pipe[1]);<br />

}<br />

execve(path, argv, envp);<br />

exit(127);<br />

}<br />

close(stdout_pipe[1]);<br />

close(stdin_pipe[0]);<br />

return p;<br />

}<br />

int spc_pclose(SPC_PIPE *p) {<br />

int status;<br />

pid_t pid;<br />

if (p->child_pid != -1) {<br />

do {<br />

pid = waitpid(p->child_pid, &status, 0);<br />

} while (pid = = -1 && errno = = EINTR);<br />

}<br />

if (p->read_fd) fclose(p->read_fd);<br />

if (p->write_fd) fclose(p->write_fd);<br />

free(p);<br />

if (pid != -1 && WIFEXITED(status)) return WEXITSTATUS(status);<br />

else return (pid = = -1 ? -1 : 0);<br />

}<br />

See Also<br />

Recipes 1.1, 1.5, 1.6<br />

1.8 Executing External Programs Securely<br />

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

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

Solution<br />

On Windows, use the CreateProcess( ) API function to load and execute a new program.<br />

Alternatively, use the CreateProcessAsUser( ) API function to load and execute<br />

a new program with a primary access token other than the one in use by the<br />

current program.<br />

Executing External Programs Securely | 33<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!