13

So I'm building a Unix minishell in C, and am implementing input, output, and err redirection, and have come across a problem with files. I open my files in a loop where I find redirection operators, and use open(), which returns an fd. I then assign the child's fd accordingly, and call an execute function.

When my shell is just going out and finding programs, and executing them with execvp(), I don't have much of a problem. The only problem is knowing whether or not I need to call close() on the file descriptors before prompting for the next command line. I'm worried about having an fd leak, but don't exactly understand how it works.

My real problem arises when using builtin commands. I have a builtin command called "read", that takes one argument, an environmental variable name(could be one that doesn't yet exist). Read then prompts for a value, and assigns that value to the variable. Here's an example:

% read TESTVAR
test value test value test value
% echo  ${TESTVAR}
test value test value test value

Well lets say that I try something like this:

% echo here's another test value > f1
% read TESTVAR < f1
% echo  ${TESTVAR}
here's another test value

This works great, keep in mind that read executes inside the parent process, I don't call read with execvp since it's builtin. Read uses gets, which requires a stream variable, not an fd. So after poking around on the irc forums a bit I was told to use fdopen, to get the stream from the file descriptor. So before calling gets, I call:

rdStream = fdopen(inFD, "r");

then call

if(fgets(buffer, envValLen, rdStream) != buffer) 
{
    if(inFD) fclose(rdStream);
    return -1;
}
if(inFD) fclose(rdStream);

As you can see, at the moment I'm closing the stream with fclose(), unless it is equal to stdin(which is 0). Is this necessary? Do I need to close the stream? Or just the file descriptor? Or both? I'm quite confused on which I should close, since they both refer to the same file, in a different manner. At the moment I'm not closing the fd, however I think that I definitely should. I would just like somebody to help make sure my shell isn't leaking any files, as I want it to be able to execute several thousand commands in a single session without leaking memory.

Thanks, if you guys want me to post anymore code just ask.

2 Answers 2

14

The standard says:

The fclose() function shall perform the equivalent of a close() on the file descriptor that is associated with the stream pointed to by stream.

So calling fclose is enough; it will also close the descriptor.

7
  • Will calling close in addition be bad? There are many cases in which I don't think the file descriptor will close inside the command, for example if I call "echo abc > f1", echo won't close f1 will it? So I need to call close inside my shell each time redirection is used. Knowing that this is happening, do I still need to close the stream? I'm guessing I do since calling fclose will need to flush the buffers and whatnot.
    – robins35
    Commented Dec 3, 2012 at 20:48
  • @Scriptonaut The standard doesn't mention it. My guess is that close will return -1 but technically the behavior is undefined. So you could devise a scheme where after a fclose you also set the associated descriptor to -1. Then a close will fail cleanly with EBADF. Even better: don't mix standard I/O with low-level I/O.
    – cnicutar
    Commented Dec 3, 2012 at 20:53
  • Ya, I would rather get away from high level IO since my shell mostly relies on low level stuff. Are there functions like gets that take fd's instead of streams? Anyways, thanks for the help, I think I got it mostly figured out.
    – robins35
    Commented Dec 3, 2012 at 21:47
  • @Scriptonaut No, there's no function like fgets unfortunately
    – cnicutar
    Commented Dec 4, 2012 at 3:07
  • 5
    Calling close in addition is very bad. It's a type of double-free bug. In single-threaded programs without signal handlers, you probably won't run into trouble, but if there's any chance of another file getting opened between the fclose and the close, you'll accidentally close an unrelated file and badly mess up the state of your program. This can lead to dangerous file-corruption bugs. Commented Dec 4, 2012 at 3:35
2

FILE is a buffering object from standard C library. When you do fclose (standard C function) it will eventually call close (Unix system function) but only after making sure C library buffers are flushed. So, I would say, if you use fopen andfwrite then you should use fclose, and not just close, otherwise you risk loosing some data.

4
  • Ok. I think I will need to call close() in the main loop in the shell, because there are many cases where the cmd's themselves won't look after closing the files, such as "echo abc > f1". Knowing that I will need to call close() in the main loop, I'm guessing then that I will still need to call fclose() inside read because fclose does other higher level things like you said. This would all be quite a bit easier if I didn't need a file stream to get a line of input. Thanks.
    – robins35
    Commented Dec 3, 2012 at 20:51
  • I think calling fclose once is enough. Also, notice that open files are closed (call to close) automatically when process dies.
    – piokuc
    Commented Dec 3, 2012 at 20:55
  • Won't I need to call close in the case that I'm not using fdopen()? This will happen often in this shell. My shell doesn't call a process for every command, sometimes it executes it right in the parent process.
    – robins35
    Commented Dec 3, 2012 at 21:25
  • 1
    Ok, in this case I think you have two options: either use FILE objects only, or use both file descriptors and FILE but keep track what is what and use appropriate function to close them.
    – piokuc
    Commented Dec 3, 2012 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.