* close-exec flag not working in 2.6.9?
@ 2005-01-31 7:56 Ben Greear
2005-01-31 21:08 ` Ulrich Drepper
0 siblings, 1 reply; 3+ messages in thread
From: Ben Greear @ 2005-01-31 7:56 UTC (permalink / raw)
To: linux-kernel
[-- Attachment #1: Type: text/plain, Size: 852 bytes --]
As far as I can tell, close-exec is not working correctly in 2.6.9
or 2.6.10-1.741_FC3smp.
The attached program generates a file /tmp/cl_foo.output
which has this data:
[greear@grok ~]$ more /tmp/cl_foo.output
total 5
lrwx------ 1 greear greear 64 Jan 30 23:49 0 -> /dev/pts/3
l-wx------ 1 greear greear 64 Jan 30 23:49 1 -> /tmp/cl_foo.output
l-wx------ 1 greear greear 64 Jan 30 23:49 2 -> /tmp/cl_foo.output
lr-x------ 1 greear greear 64 Jan 30 23:49 3 -> /etc/passwd
lr-x------ 1 greear greear 64 Jan 30 23:49 4 -> /proc/7020/fd
I would not expect to see the /etc/passwd entry since I marked
it close-exec in the parent process before forking and running
the system() command.
Am I confused about things, or is this a real bug?
Thanks,
Ben
--
Ben Greear <greearb@candelatech.com>
Candela Technologies Inc http://www.candelatech.com
[-- Attachment #2: close_exec.cc --]
[-- Type: text/plain, Size: 1254 bytes --]
#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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: close-exec flag not working in 2.6.9?
2005-01-31 7:56 close-exec flag not working in 2.6.9? Ben Greear
@ 2005-01-31 21:08 ` Ulrich Drepper
2005-01-31 21:26 ` Ben Greear
0 siblings, 1 reply; 3+ messages in thread
From: Ulrich Drepper @ 2005-01-31 21:08 UTC (permalink / raw)
To: Ben Greear; +Cc: linux-kernel
On Sun, 30 Jan 2005 23:56:07 -0800, Ben Greear <greearb@candelatech.com> 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.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: close-exec flag not working in 2.6.9?
2005-01-31 21:08 ` Ulrich Drepper
@ 2005-01-31 21:26 ` Ben Greear
0 siblings, 0 replies; 3+ messages in thread
From: Ben Greear @ 2005-01-31 21:26 UTC (permalink / raw)
To: Ulrich Drepper; +Cc: linux-kernel
Ulrich Drepper wrote:
> On Sun, 30 Jan 2005 23:56:07 -0800, Ben Greear <greearb@candelatech.com> 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 <greearb@candelatech.com>
Candela Technologies Inc http://www.candelatech.com
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2005-01-31 21:26 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-01-31 7:56 close-exec flag not working in 2.6.9? Ben Greear
2005-01-31 21:08 ` Ulrich Drepper
2005-01-31 21:26 ` Ben Greear
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox