#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <errno.h>
using namespace std;
void close_exec(int s) {
int flags;
flags = fcntl(s, F_GETFL);
flags |= (FD_CLOEXEC);
if (fcntl(s, F_SETFL, flags) < 0) {
cerr << "ERROR: fcntl, executing close_exec: " << strerror(errno)
<< endl;
}
}//close_exec
int main() {
ofstream script("/tmp/cl_foo.bash");
script << "#!/bin/bash\nls -l /proc/self/fd > /tmp/cl_foo.output 2>&1\n";
script.close();
if (chmod("/tmp/cl_foo.bash",
S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) < 0) {
cerr << "ERROR: Failed to chmod /tmp/cl_foo.bash, error: "
<< strerror(errno) << endl;
exit(7);
}
int fd = open("/etc/passwd", O_RDONLY);
if (fd < 0) {
cerr << "ERROR: Failed to open /etc/passwd: " << strerror(errno) << endl;
}
else {
close_exec(fd);
}
int rv = fork();
if (rv < 0) {
cerr << "ERROR from fork: " << strerror(errno) << endl;
}
else if (rv == 0) {
// Child
system("/tmp/cl_foo.bash");
exit(7);
}
else {
// parent, done
sleep(5);
}
return 0;
}//main
On Sun, 30 Jan 2005 23:56:07 -0800, Ben Greear <[email protected]> wrote:
> flags = fcntl(s, F_GETFL);
> flags |= (FD_CLOEXEC);
> if (fcntl(s, F_SETFL, flags) < 0) {
These have to be F_GETFD and F_SETFD respectively. Note L -> D.
Ulrich Drepper wrote:
> On Sun, 30 Jan 2005 23:56:07 -0800, Ben Greear <[email protected]> wrote:
>
>> flags = fcntl(s, F_GETFL);
>> flags |= (FD_CLOEXEC);
>> if (fcntl(s, F_SETFL, flags) < 0) {
>
>
> These have to be F_GETFD and F_SETFD respectively. Note L -> D.
Yes, that does seem to work much better. It would have been
a while before I ever figured that out on my own.
Thanks to you and the other people who pointed that out
off the list!
Ben
--
Ben Greear <[email protected]>
Candela Technologies Inc http://www.candelatech.com