X-Recipient: archive-cygwin AT delorie DOT com DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:to:from:subject:date:message-id:references :mime-version:content-type:content-transfer-encoding; q=dns; s= default; b=tx+ql7VHjp3Y+Z23OGt9E/TZwn0pp6pEmJ31+QU1zRnApB29nSQ61 LzuZahQ6kJ5re+jDq28zKkITdOSZnEcVs7rV8vwTbvpabIZIIEBch2qPGT2Ygzn2 lW/t73uy7Ewm7mnXYpb55MdKLf0TFP5SOEwMiGSsOfS462xBfZGtWY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:to:from:subject:date:message-id:references :mime-version:content-type:content-transfer-encoding; s=default; bh=Ne6zJj7I/FsAy+YnGwMLuVrKfXg=; b=BlMx2H21m37tlJq4l5nNsWYpusWV UQqnQyzm8LoQSULNSCsqZQu4JYWSkZfkGCsR1lrz0r9fda1Fd9w2g0ag2C+CwBpz t8uMJpygpLbiNQcAY/jvoacEKeEoSX9XIUMJvATbQzW1Myg5hhLBTsU4k1rVbWHm Hy5fBMTxriQfAh4= Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_LOW,RP_MATCHES_RCVD,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2 spammy=gmane.org, gmaneorg, errno, UNKNOWN X-HELO: plane.gmane.org To: cygwin AT cygwin DOT com From: Henri Subject: Re: Pipes Again -- a simple test case Date: Mon, 11 Jan 2016 16:13:33 +0000 (UTC) Lines: 262 Message-ID: References: <0aebd09993901f3ef3ff728d162952cd AT xs4all DOT nl> <568A32BE DOT 4080101 AT gmail DOT com> <20160108151933 DOT GI20447 AT calimero DOT vinschen DOT de> <83235106e7ed2fcfde26293d156be51d AT xs4all DOT nl> <20160108163055 DOT GK20447 AT calimero DOT vinschen DOT de> <20160111115523 DOT GD32610 AT calimero DOT vinschen DOT de> <20160111154737 DOT GA30417 AT calimero DOT vinschen DOT de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit User-Agent: Loom/3.14 (http://gmane.org/) X-IsSubscribed: yes Corinna Vinschen cygwin.com> writes: > Can you please provide the socket testcase? Trying to send you what you ask for (using gmane.org, I have to copy/paste) ----- t_stat3.c: /*************************************************************************\ * Copyright (C) Michael Kerrisk, 2014. * * * * This program is free software. You may use, modify, and redistribute it * * under the terms of the GNU Affero General Public License as published * * by the Free Software Foundation, either version 3 or (at your option) * * any later version. This program is distributed without any warranty. * * See the file COPYING.agpl-v3 for details. * \*************************************************************************/ /* Listing 15-1 */ // t_stat.c -- heavily modified #define _BSD_SOURCE /* Get major() and minor() from */ #include #include #include //#include "file_perms.h" //#include "tlpi_hdr.h" #include /* Standard I/O functions */ #include /* Prototypes of commonly used library functions, plus EXIT_SUCCESS and EXIT_FAILURE constants */ #include /* Prototypes for many system calls */ #include /* Commonly used string-handling functions */ static void displayStatInfo(const struct stat *sb) { printf("File type: "); switch (sb->st_mode & S_IFMT) { case S_IFREG: printf("regular file\n"); break; case S_IFDIR: printf("directory\n"); break; case S_IFCHR: printf("character device\n"); break; case S_IFBLK: printf("block device\n"); break; case S_IFLNK: printf("symbolic (soft) link\n"); break; case S_IFIFO: printf("FIFO or pipe\n"); break; case S_IFSOCK: printf("socket\n"); break; default: printf("unknown file type?\n"); break; } printf("Device containing i-node: (%lx) major=%lu minor=%lu\n", (long) sb->st_dev, (long) major(sb->st_dev), (long) minor(sb->st_dev)); printf("I-node number: %llx - decimal: %llu\n", sb->st_ino, sb->st_ino); if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode)) printf(" **** Device number (st_rdev): major=%ld; minor=%ld\n", (long) major(sb->st_rdev), (long) minor(sb->st_rdev)); printf("File size: %lld bytes\n", (long long) sb->st_size); } void errExit(const char *format, ...); #include #include int main() { char symlnk[64]; pid_t pid = getpid(); int sfd[2]; for (int f = 0; f < 2; f++) // step over the 1st and 2nd socket { if (!f) { sfd[f] = socket(AF_UNIX, SOCK_STREAM, 0); //sfd[f] = socket(AF_INET, SOCK_STREAM, 0); if (sfd[f] == -1) errExit("socket"); } else { sfd[f] = socket(AF_UNIX, SOCK_STREAM, 0); //sfd[f] = socket(AF_INET, SOCK_STREAM, 0); if (sfd[f] == -1) errExit("socket"); } } struct stat sb[2]; // one buffer for the 1st socket, one for the 2nd int f; char target[128]; ssize_t num; printf("1st socket\n"); f = 0; if (sprintf(symlnk, "/proc/%d/fd/%d", pid, sfd[f]) <= 0) errExit("sprintf"); printf("... %s (symbolic link to the socket)\n", symlnk); if ( (num = readlink( (const char *)symlnk, target, 128) ) == -1) errExit("readlink"); if (num < 128) { target[num] = '\0'; printf("... %s (target of symbolic link to the socket)\n", target); printf("... using stat() on symbolic link: %s\n", symlnk); //if (stat(target, sb + f) != 0) // not possible: target is NOT a filename ... if (stat(symlnk, sb + f) != 0) errExit("stat"); displayStatInfo(sb + f); printf("... using fstat()\n"); memset(sb + f, 0, sizeof(struct stat) ); // CLEAR if (fstat(sfd[f], sb + f) != 0) errExit("fstat"); displayStatInfo(sb + f); } #if 1 printf("\n2nd socket\n"); f = 1; if (sprintf(symlnk, "/proc/%d/fd/%d", pid, sfd[f]) <= 0) errExit("sprintf"); printf("... %s (symbolic link to the socket)\n", symlnk); if ( (num = readlink( (const char *)symlnk, target, 128) ) == -1) errExit("readlink"); if (num < 128) { target[num] = '\0'; printf("... %s (target of symbolic link to the socket)\n", target); printf("... using stat() on symbolic link: %s\n", symlnk); //if (stat(target, sb + f) != 0) // not possible: target is NOT a filename ... if (stat(symlnk, sb + f) != 0) errExit("stat"); displayStatInfo(sb + f); printf("... using fstat()\n"); memset(sb + f, 0, sizeof(struct stat) ); // CLEAR if (fstat(sfd[f], sb + f) != 0) errExit("fstat"); displayStatInfo(sb + f); } #endif close(sfd[0]); close(sfd[1]); exit(EXIT_SUCCESS); } #include /* Declares errno and defines error constants */ #include #ifdef TRUE #undef TRUE #endif #ifdef FALSE #undef FALSE #endif typedef enum { FALSE, TRUE } Boolean; static char *ename[] = { /* 0 */ "", /* 1 */ "EPERM", "ENOENT", "ESRCH", "EINTR", "EIO", "ENXIO", /* 7 */ "E2BIG", "ENOEXEC", "EBADF", "ECHILD", /* 11 */ "EAGAIN/EWOULDBLOCK", "ENOMEM", "EACCES", "EFAULT", /* 15 */ "ENOTBLK", "EBUSY", "EEXIST", "EXDEV", "ENODEV", /* 20 */ "ENOTDIR", "EISDIR", "EINVAL", "ENFILE", "EMFILE", /* 25 */ "ENOTTY", "ETXTBSY", "EFBIG", "ENOSPC", "ESPIPE", /* 30 */ "EROFS", "EMLINK", "EPIPE", "EDOM", "ERANGE", "ENOMSG", /* 36 */ "EIDRM", "ECHRNG", "EL2NSYNC", "EL3HLT", "EL3RST", /* 41 */ "ELNRNG", "EUNATCH", "ENOCSI", "EL2HLT", "EDEADLK", /* 46 */ "ENOLCK", "", "", "", "EBADE", "EBADR", "EXFULL", /* 53 */ "ENOANO", "EBADRQC", "EBADSLT", "EDEADLOCK", "EBFONT", /* 58 */ "", "", "ENOSTR", "ENODATA", "ETIME", "ENOSR", "ENONET", /* 65 */ "ENOPKG", "EREMOTE", "ENOLINK", "EADV", "ESRMNT", /* 70 */ "ECOMM", "EPROTO", "", "", "EMULTIHOP", "ELBIN", /* 76 */ "EDOTDOT", "EBADMSG", "", "EFTYPE", "ENOTUNIQ", "EBADFD", /* 82 */ "EREMCHG", "ELIBACC", "ELIBBAD", "ELIBSCN", "ELIBMAX", /* 87 */ "ELIBEXEC", "ENOSYS", "ENMFILE", "ENOTEMPTY", /* 91 */ "ENAMETOOLONG", "ELOOP", "", "", "EOPNOTSUPP", /* 96 */ "EPFNOSUPPORT", "", "", "", "", "", "", "", "ECONNRESET", /* 105 */ "ENOBUFS", "EAFNOSUPPORT", "EPROTOTYPE", "ENOTSOCK", /* 109 */ "ENOPROTOOPT", "ESHUTDOWN", "ECONNREFUSED", "EADDRINUSE", /* 113 */ "ECONNABORTED", "ENETUNREACH", "ENETDOWN", "ETIMEDOUT", /* 117 */ "EHOSTDOWN", "EHOSTUNREACH", "EINPROGRESS", "EALREADY", /* 121 */ "EDESTADDRREQ", "EMSGSIZE", "EPROTONOSUPPORT", /* 124 */ "ESOCKTNOSUPPORT", "EADDRNOTAVAIL", "ENETRESET", /* 127 */ "EISCONN", "ENOTCONN", "ETOOMANYREFS", "EPROCLIM", /* 131 */ "EUSERS", "EDQUOT", "ESTALE", "ENOTSUP", "ENOMEDIUM", /* 136 */ "ENOSHARE", "ECASECLASH", "EILSEQ", "EOVERFLOW", /* 140 */ "ECANCELED", "ENOTRECOVERABLE", "EOWNERDEAD", "ESTRPIPE" }; #define MAX_ENAME 143 static void terminate(Boolean useExit3) { char *s; /* Dump core if EF_DUMPCORE environment variable is defined and is a nonempty string; otherwise call exit(3) or _exit(2), depending on the value of 'useExit3'. */ s = getenv("EF_DUMPCORE"); if (s != NULL && *s != '\0') abort(); else if (useExit3) exit(EXIT_FAILURE); else _exit(EXIT_FAILURE); } static void outputError(Boolean useErr, int err, Boolean flushStdout, const char *format, va_list ap) { #define BUF_SIZE 500 char buf[BUF_SIZE], userMsg[BUF_SIZE], errText[BUF_SIZE]; vsnprintf(userMsg, BUF_SIZE, format, ap); if (useErr) snprintf(errText, BUF_SIZE, " [%s %s]", (err > 0 && err <= MAX_ENAME) ? ename[err] : "?UNKNOWN?", strerror(err)); else snprintf(errText, BUF_SIZE, ":"); snprintf(buf, BUF_SIZE, "ERROR%s %s\n", errText, userMsg); if (flushStdout) fflush(stdout); /* Flush any pending stdout */ fputs(buf, stderr); fflush(stderr); /* In case stderr is not line-buffered */ } void errExit(const char *format, ...) { va_list argList; va_start(argList, format); outputError(TRUE, errno, TRUE, format, argList); va_end(argList); terminate(TRUE); } //===== -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple