| 1 | // { dg-require-fork "" } |
| 2 | // { dg-require-mkfifo "" } |
| 3 | |
| 4 | // Copyright (C) 2001-2025 Free Software Foundation, Inc. |
| 5 | // |
| 6 | // This file is part of the GNU ISO C++ Library. This library is free |
| 7 | // software; you can redistribute it and/or modify it under the |
| 8 | // terms of the GNU General Public License as published by the |
| 9 | // Free Software Foundation; either version 3, or (at your option) |
| 10 | // any later version. |
| 11 | |
| 12 | // This library is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU General Public License for more details. |
| 16 | |
| 17 | // You should have received a copy of the GNU General Public License along |
| 18 | // with this library; see the file COPYING3. If not see |
| 19 | // <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | // C++98 27.8.1.3 filebuf member functions |
| 22 | |
| 23 | // various tests for filebuf::open() and filebuf::close() including |
| 24 | // the non-portable functionality in the libstdc++-v3 IO library |
| 25 | |
| 26 | #include <fstream> |
| 27 | #include <iostream> |
| 28 | #include <cstdlib> |
| 29 | #include <unistd.h> |
| 30 | #include <signal.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/stat.h> |
| 34 | |
| 35 | #include <testsuite_hooks.h> |
| 36 | |
| 37 | // libstdc++/2913, libstdc++/4879 |
| 38 | // John Fardo <[email protected]>, Brad Garcia <[email protected]> |
| 39 | bool |
| 40 | test_04() |
| 41 | { |
| 42 | using namespace __gnu_test; |
| 43 | |
| 44 | bool test = true; |
| 45 | const char* name = "tmp_fifo1"; |
| 46 | semaphore s1, s2; |
| 47 | |
| 48 | signal(SIGPIPE, SIG_IGN); |
| 49 | |
| 50 | unlink(name); |
| 51 | if (0 != mkfifo(name, S_IRWXU)) |
| 52 | { |
| 53 | std::cerr << "failed to create fifo" << std::endl; |
| 54 | exit(-1); |
| 55 | } |
| 56 | |
| 57 | int fval = fork(); |
| 58 | if (fval == -1) |
| 59 | { |
| 60 | std::cerr << "failed to fork" << std::endl; |
| 61 | unlink(name); |
| 62 | return false; |
| 63 | } |
| 64 | else if (fval == 0) |
| 65 | { |
| 66 | std::ifstream ifs(name); |
| 67 | s1.wait(); |
| 68 | ifs.close(); |
| 69 | s2.signal(); |
| 70 | exit(0); |
| 71 | } |
| 72 | |
| 73 | std::ofstream ofs(name); |
| 74 | s1.signal(); |
| 75 | s2.wait(); |
| 76 | ofs.put('t'); |
| 77 | |
| 78 | /* |
| 79 | * ISO/IED 14882:1998(E) 27.8.1.10.4 |
| 80 | * |
| 81 | * void close(); |
| 82 | * |
| 83 | * Effects: Calls rdbuf()->close() and, if that function fails |
| 84 | * (returns a null pointer), calls setstate(failbit)... |
| 85 | */ |
| 86 | ofs.close(); |
| 87 | if (!(ofs.rdstate() & std::ios::failbit)) |
| 88 | { |
| 89 | test = false; |
| 90 | } |
| 91 | |
| 92 | unlink(name); |
| 93 | |
| 94 | return test; |
| 95 | } |
| 96 | |
| 97 | int |
| 98 | main() |
| 99 | { |
| 100 | return !test_04(); |
| 101 | } |