Back to index

nagios-plugins  1.4.16
pipe-safer.c
Go to the documentation of this file.
00001 /* Invoke pipe, but avoid some glitches.
00002    Copyright (C) 2005-2006, 2009-2010 Free Software Foundation, Inc.
00003 
00004    This program is free software: you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 3 of the License, or
00007    (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00016 
00017 /* Written by Jim Meyering.  */
00018 
00019 #include <config.h>
00020 
00021 #include "unistd-safer.h"
00022 
00023 #include <unistd.h>
00024 #include <errno.h>
00025 
00026 /* Like pipe, but ensure that neither of the file descriptors is
00027    STDIN_FILENO, STDOUT_FILENO, or STDERR_FILENO.  Fail with ENOSYS on
00028    platforms that lack pipe.  */
00029 
00030 int
00031 pipe_safer (int fd[2])
00032 {
00033 #if HAVE_PIPE
00034   if (pipe (fd) == 0)
00035     {
00036       int i;
00037       for (i = 0; i < 2; i++)
00038         {
00039           fd[i] = fd_safer (fd[i]);
00040           if (fd[i] < 0)
00041             {
00042               int e = errno;
00043               close (fd[1 - i]);
00044               errno = e;
00045               return -1;
00046             }
00047         }
00048 
00049       return 0;
00050     }
00051 #else
00052   errno = ENOSYS;
00053 #endif
00054 
00055   return -1;
00056 }