Open() не устанавливает флаг O_CLOEXEC
Я пытаюсь установить флаг O_CLOEXEC, используя open(), и не имеет sucess.
Рассмотрим следующий микротест:
#include <stdio.h>
#include <fcntl.h>
int main() {
int fd = open("test.c", O_RDONLY | O_CLOEXEC);
int ret = fcntl(fd, F_GETFL);
if(ret & O_CLOEXEC) {
printf("OK!\n");
} else {
printf("FAIL!\n");
}
printf("fd = %d\n", fd);
printf("ret = %x, O_CLOEXEC = %x\n", ret, O_CLOEXEC);
return 0;
}
При работе в Linux с ядром версии 2.6 тест завершается успешно и печатает "OK!", но не работает с ядрами 3.8 или 3.9.
Что случилось?
Спасибо!
Ответы
Ответ 1
Было решено, что выставлять флаг O_CLOEXEC
на fcntl(fd, F_GETFL)
является утечкой безопасности. Изменения были сделаны this commit в ядре 3.6-rc7:
commit c6f3d81115989e274c42a852222b80d2e14ced6f
Author: Al Viro <[email protected]>
Date: Sun Aug 26 11:01:04 2012 -0400
don't leak O_CLOEXEC into ->f_flags
Signed-off-by: Al Viro <[email protected]>
Другими словами, вы не должны были полагаться на O_CLOEXEC
на первый взгляд.
Ответ 2
Параметр вызова fcntl F_GETFD
, флаг FD_CLOEXEC
и поддержка O_CLOEXEC
появилась в 2.6. 23
. Прочтите руководства:
File descriptor flags
The following commands manipulate the flags associated with a file descriptor.
Currently, only one such flag is defined: FD_CLOEXEC, the close-on-exec flag.
If the FD_CLOEXEC bit is 0, the file descriptor will remain open across an
execve(2), otherwise it will be closed.
F_GETFD (void)
Read the file descriptor flags; arg is ignored.
F_SETFD (int)
Set the file descriptor flags to the value specified by arg.
Ответ 3
Вы делаете это неправильно. Вы должны сделать так:
int ret = fcntl(fd, F_GETFD);
if (ret & FD_CLOEXEC) {
...
}