c++ - what is the purpose to set SOCK_CLOEXEC flag with accept4() same as O_CLOEXEC -


basically, need know purpose of setting sock_cloexec when sitting accept4(). how can check functionality of flag returned file descriptor accept.

accepted_fd =   accept4(sd, (struct sockaddr *)& tcp_remote, &size,  sock_cloexec); 

the reason sock_cloexec exist avoid race condition between getting new socket accept , setting fd_cloexec flag afterwards.

normally if want file descriptor close-on-exec you'd first obtain file descriptor in way, call fcntl(fd, f_setfd, fd_cloexec). in threaded program there possibility race condition between getting file descriptor (in case accept) , setting cloexec flag. therefore linux has changed (if not all) system calls return new file descriptors accept flags tell kernel atomically set close-on-exec flag before making file descriptor valid. way race condition closed.

if wonder why close on exec exists, it's because in cases, when you're executing non-privileged programs privileged one, don't want file descriptors leak program.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -