1#!/bin/sh 2 3# 4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD 5# 6# Copyright (c) 2020 Konstantin Belousov 7# 8# Redistribution and use in source and binary forms, with or without 9# modification, are permitted provided that the following conditions 10# are met: 11# 1. Redistributions of source code must retain the above copyright 12# notice, this list of conditions and the following disclaimer. 13# 2. Redistributions in binary form must reproduce the above copyright 14# notice, this list of conditions and the following disclaimer in the 15# documentation and/or other materials provided with the distribution. 16# 17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27# SUCH DAMAGE. 28# 29 30# Pipe test scenario from https://reviews.freebsd.org/D23993 31# https://gist.github.com/kostikbel/b2844258b7fba6e8ce3ccd8ef9422e5a 32 33. ../default.cfg 34 35cd /tmp 36cat > pipe_enomem.c <<EOF 37/* $Id: pipe_enomem.c,v 1.3 2020/03/07 21:02:04 kostik Exp kostik $ */ 38 39#include <errno.h> 40#include <stdio.h> 41#include <stdlib.h> 42#include <string.h> 43#include <unistd.h> 44 45struct pipepair { 46 int pp[2]; 47}; 48 49static struct pipepair *p; 50 51int 52main(void) 53{ 54 int error, pp[2]; 55 size_t i, k, nsz, sz; 56 char x; 57 58 sz = 1024; 59 p = calloc(sz, sizeof(struct pipepair)); 60 if (p == NULL) { 61 fprintf(stderr, "calloc: %s\n", strerror(errno)); 62 exit(1); 63 } 64 65 for (i = 0;; i++) { 66 if (pipe(pp) == -1) { 67 printf("created %zd pipes. syscall error %s\n", 68 i, strerror(errno)); 69 break; 70 } 71 if (i >= sz) { 72 nsz = sz * 2; 73 p = reallocf(p, nsz * sizeof(struct pipepair)); 74 if (p == NULL) { 75 fprintf(stderr, "reallocf: %s\n", 76 strerror(errno)); 77 exit(1); 78 } 79 memset(p + sz, 0, (nsz - sz) * sizeof(struct pipepair)); 80 sz = nsz; 81 } 82 p[i].pp[0]= pp[0]; 83 p[i].pp[1]= pp[1]; 84 } 85 86 x = 'a'; 87 for (k = 0; k < i; k++) { 88 error = write(p[k].pp[1], &x, 1); 89 if (error == -1) 90 printf("pipe %zd fds %d %d error %s\n", 91 k, p[k].pp[0], p[k].pp[1], strerror(errno)); 92 else if (error == 0) 93 printf("pipe %zd fds %d %d EOF\n", 94 k, p[k].pp[0], p[k].pp[1]); 95 } 96} 97EOF 98 99mycc -o pipe_enomem -Wall -Wextra -O2 pipe_enomem.c || exit 1 100./pipe_enomem 2>&1 | head -5 101rm -f pipe_enomem.c pipe_enomem 102exit 0 103