file-descriptor

Sending file descriptor by Linux socket

浪尽此生 提交于 2019-11-26 09:43:17
问题 I am trying to send some file descriptor by linux socket, but it does not work. What am I doing wrong? How is one supposed to debug something like this? I tried putting perror() everywhere it\'s possible, but they claimed that everything is ok. Here is what I\'ve written: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/wait.h> #include <sys/socket.h> #include <sys/types.h> #include <fcntl.h> void wyslij(int socket, int fd) // send fd by socket {

Getting the highest allocated file descriptor

怎甘沉沦 提交于 2019-11-26 09:25:40
问题 Is there a portable way (POSIX) to get the highest allocated file descriptor number for the current process? I know that there\'s a nice way to get the number on AIX, for example, but I\'m looking for a portable method. The reason I\'m asking is that I want to close all open file descriptors. My program is a server which runs as root and forks and execs child programs for non-root users. Leaving the privileged file descriptors open in the child process is a security problem. Some file

Increasing limit of FD_SETSIZE and select

為{幸葍}努か 提交于 2019-11-26 09:08:52
问题 I want to increase FD_SETSIZE macro value for my system. Is there any way to increase FD_SETSIZE so select will not fail 回答1: Per the standards, there is no way to increase FD_SETSIZE . Some programs and libraries (libevent comes to mind) try to work around this by allocating additional space for the fd_set object and passing values larger than FD_SETSIZE to the FD_* macros, but this is a very bad idea since robust implementations may perform bounds-checking on the argument and abort if it's

What&#39;s the difference between a file descriptor and file pointer?

醉酒当歌 提交于 2019-11-26 04:30:58
问题 I want to know the difference between a file descriptor and file pointer. Also, in what scenario would you use one instead of the other? 回答1: A file descriptor is a low-level integer "handle" used to identify an opened file (or socket, or whatever) at the kernel level, in Linux and other Unix-like systems. You pass "naked" file descriptors to actual Unix calls, such as read() , write() and so on. A FILE pointer is a C standard library-level construct, used to represent a file. The FILE wraps

What are file descriptors, explained in simple terms?

泪湿孤枕 提交于 2019-11-26 01:02:53
问题 What would be a more simplified description of file descriptors compared to Wikipedia\'s? Why are they required? Say, take shell processes as an example and how does it apply for it? Does a process table contain more than one file descriptor. If yes, why? 回答1: In simple words, when you open a file, the operating system creates an entry to represent that file and store the information about that opened file. So if there are 100 files opened in your OS then there will be 100 entries in OS

Retrieve filename from file descriptor in C

断了今生、忘了曾经 提交于 2019-11-26 00:19:15
问题 Is it possible to get the filename of a file descriptor (Linux) in C? 回答1: You can use readlink on /proc/self/fd/NNN where NNN is the file descriptor. This will give you the name of the file as it was when it was opened — however, if the file was moved or deleted since then, it may no longer be accurate (although Linux can track renames in some cases). To verify, stat the filename given and fstat the fd you have, and make sure st_dev and st_ino are the same. Of course, not all file

How to construct a c++ fstream from a POSIX file descriptor?

心已入冬 提交于 2019-11-25 23:46:49
问题 I\'m basically looking for a C++ version of fdopen(). I did a bit of research on this and it is one of those things that seems like it should be easy, but turns out to be very complicated. Am I missing something in this belief (i.e. it really is easy)? If not, is there a good library out there somewhere to handle this? EDIT: Moved my example solution to a separate answer. 回答1: From the answer given by Éric Malenfant: AFAIK, there is no way to do this in standard C++. Depending on your

What are file descriptors, explained in simple terms?

女生的网名这么多〃 提交于 2019-11-25 23:10:35
What would be a more simplified description of file descriptors compared to Wikipedia's? Why are they required? Say, take shell processes as an example and how does it apply for it? Does a process table contain more than one file descriptor. If yes, why? Tayyab In simple words, when you open a file, the operating system creates an entry to represent that file and store the information about that opened file. So if there are 100 files opened in your OS then there will be 100 entries in OS (somewhere in kernel). These entries are represented by integers like (...100, 101, 102....). This entry