Grep Write Error Bad File Descriptor
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have cat standard output bad file descriptor Meta Discuss the workings and policies of this site About Us dup2 bad file descriptor c Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with write error: bad file descriptor git us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just stdin_fileno like you, helping each other. Join them; it only takes a minute: Sign up grep: (standard input): Bad file descriptor up vote 0 down vote favorite I am trying to change the stdin to a pipe outlet and stdout to another file and using grep. I get the following error: grep: (standard input): Bad file descriptor My
Dup2 Example
code is: #include
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn
C Pipe
more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up “Bad file descriptor” error when reading from pipe as stdin up vote 3 down vote favorite 1 I fork and set up a command like http://stackoverflow.com/questions/22139665/grep-standard-input-bad-file-descriptor this: pid_t pid; pid = fork(); if (pid == 0) { // I am the child freopen("/input_pipe","r",stdin); freopen("/output_pip","w",stdout); execl("/bin/sh", "sh", "-c", command, (char *)NULL); // using execv is probably faster // Should never get here perror("execl"); exit(1); } The /input_pipe has been created and filled with data before the processs is forked. This works in almost all cases absolutely fine. The command reads (with read()) from its stdin and gets the data that was written to http://stackoverflow.com/questions/21289077/bad-file-descriptor-error-when-reading-from-pipe-as-stdin the pipe by another process. But sometimes the command cannot read from its stdin stream and gets the "Bad file descriptor" error when trying to do so. What could cause this error? Edit: I have changed the freopen parts to this: pipe_in = open(pipename_in, O_RDONLY); pipe_out = open(pipename_out, O_WRONLY); dup2(pipe_in, 0); dup2(pipe_out, 1); I will test this for a couple of days though as the error was only appearing very seldom. c pipe share|improve this question edited Jan 23 '14 at 7:57 asked Jan 22 '14 at 16:54 Robby75 66121332 Does your "real" code check freopen() for error by testing the function return value against NULL? –alk Jan 22 '14 at 17:13 add a comment| 1 Answer 1 active oldest votes up vote 4 down vote accepted One possible source of issues is that "stdin" an "stdout" don't correspond necessarily with file descriptors 0 and 1 respectively. In many implementations of the runtime library, freopen may change the file descriptor belonging to the FILE*. The file descriptors belong to the kernel, while the FILEs belong to the runtime library, and they are not necessarily aligned. Please have a look here: http://man7.org/linux/man-pages/man3/stdout.3.html "Applying freopen(3) to one of these streams can change the file descriptor number associated with the stream" The child process, after execl, only expects that file descriptor 0 and 1 are standard
HAT RUBY SAP SEARCH SILVERLIGHT SYMBIAN VIDEO VIRTUALIZATION WEB 2.0 WEBLOGIC WEBSPHERE WIRELESS XML Home Subscribe Advertise Authors Topics Videos Events Webcasts Internet of Things | Cloud | http://linux.sys-con.com/node/1053821 Big Data | DevOps | Containers | Microservices | WebRTC | Java | Linux | Open Stack | Mobile | Security | SDDC Linux Containers Authors: Liz McMillan, Elizabeth White, Derek Weeks, Pat Romanski, Lori MacVittie Related Topics: Linux Containers Linux Containers: Article Bad File Descriptor Error in Linux Stellar Phoenix Linux Data Recovery is a comprehensive data recovery tool By Allen Sood Article Rating: Select ratingGive bad file it 1/5Give it 2/5Give it 3/5Give it 4/5Give it 5/5 July 30, 2009 04:30 AM EDT Reads: 69,498 Related Print Email Feedback Add This Blog This In a Linux system, files, blocks, directories, sockets and other items are referred by corresponding file descriptors. If your system is reporting errors that the file descriptor is bad, one of possible causes is that file system is corrupt and thus, you require bad file descriptor restoring from backup. But if backup is not valid enough to restore complete information, you need to use advanced Linux Data Recovery applications. As an instance, you might encounter the below error message with your Linux based system: “Bad file descriptor” You might receive this error while trying to access a file or while trying to boot the system. Cause Possible reasons for the ‘Bad file descriptor’ error to occur are: Linux systems use a special file, called /dev/null (also called the null device). This file removes all the data that is written to it after reporting that the write process has successfully succeeded. You might receive the above error message if /dev/null has been deleted or overwritten. You can infer this cause when file system is reported as read-only at the time of booting through error messages, such as“/dev/null: Read-only filesystem” and “dup2: bad file descriptor” If you get this error message while trying to access a file, the most prominent causes are file system corruption or damaged disk block(s). In such situations, you also get write errors. Solution You need to use these methods to troubleshoot the above behavior: For solving deleted or overwritten /dev/null file issues, you need to replace it with the suitab