關於fcntl(fd, F_SETFD, FD_CLOEXEC)設定exec時close的屬性
snd_ctl_hw_open
#define SNDRV_FILE_CONTROL ALSA_DEVICE_DIRECTORY "controlC%i"
sprintf(filename, SNDRV_FILE_CONTROL, card); // 路徑/dev/snd/controlC0
fd = snd_open_device(filename, fmode);
fcntl(fd,
F_SETFD, FD_CLOEXEC); //
這裡設定為FD_CLOEXEC表示當程式執行exec函數時本fd將被系統自動關閉,表示不傳遞給exec建立的新進程,
如果設定為fcntl(fd, F_SETFD, 0);那麼本fd將保持開啟狀態複製到exec建立的新進程中[luther.gliethttp].
進入核心系統調用
sys_fcntl
do_fcntl
case F_SETFD:
err = 0;
set_close_on_exec(fd, arg & FD_CLOEXEC);
void fastcall set_close_on_exec(unsigned int fd, int flag)
{
struct files_struct *files = current->files;
struct fdtable *fdt;
spin_lock(&files->file_lock);
fdt = files_fdtable(files);
if (flag)
FD_SET(fd, fdt->close_on_exec);
else
FD_CLR(fd, fdt->close_on_exec);
spin_unlock(&files->file_lock);
}
下面是man fcntl看到的對FD_CLOEXEC解釋
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 (long)
Set the file descriptor flags to the value specified by arg.