From owner-svn-src-user@freebsd.org Tue May 29 21:37:04 2018 Return-Path: Delivered-To: svn-src-user@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id BB42AF77E52 for ; Tue, 29 May 2018 21:37:03 +0000 (UTC) (envelope-from asomers@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 5BF0A80410; Tue, 29 May 2018 21:37:03 +0000 (UTC) (envelope-from asomers@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 16AB921939; Tue, 29 May 2018 21:37:03 +0000 (UTC) (envelope-from asomers@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w4TLb2Zo080172; Tue, 29 May 2018 21:37:02 GMT (envelope-from asomers@FreeBSD.org) Received: (from asomers@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w4TLb2PF080170; Tue, 29 May 2018 21:37:02 GMT (envelope-from asomers@FreeBSD.org) Message-Id: <201805292137.w4TLb2PF080170@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: asomers set sender to asomers@FreeBSD.org using -f From: Alan Somers Date: Tue, 29 May 2018 21:37:02 +0000 (UTC) To: src-committers@freebsd.org, svn-src-user@freebsd.org Subject: svn commit: r334355 - in user/asomers: . style9 X-SVN-Group: user X-SVN-Commit-Author: asomers X-SVN-Commit-Paths: in user/asomers: . style9 X-SVN-Commit-Revision: 334355 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-user@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "SVN commit messages for the experimental " user" src tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 29 May 2018 21:37:04 -0000 Author: asomers Date: Tue May 29 21:37:02 2018 New Revision: 334355 URL: https://svnweb.freebsd.org/changeset/base/334355 Log: user/asomers/style9: automatic style verifier This python script can automatically detect many common style(9) violations. Sponsored by: Spectra Logic Corp Added: user/asomers/ user/asomers/style9/ user/asomers/style9/badstyle.c (contents, props changed) user/asomers/style9/style9.py (contents, props changed) Added: user/asomers/style9/badstyle.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ user/asomers/style9/badstyle.c Tue May 29 21:37:02 2018 (r334355) @@ -0,0 +1,73 @@ +/* + * this is a comment + * with text on the last line */ + +/* func on same line as type; OK in a prototype */ +int foo(); + + +/* func on same line as type */ +int foo(){ +} +static const struct blargle *myfunc(int x, char *y, struct foo *f){ +} +/* this one is really long */ +static int zfs_close(vnode_t *vp, int flag, int count, offset_t offset, + cred_t *cr, caller_context_t *ct) + +/* Omit the space after some keywords */ +int +foo(){ + if(1) { + while(7){ + if (foo){ + } + } + } +} + +// A C++ style comment +int foo; //C++ style inline comment + + +/* Solo brace after control block */ +long +bar(){ + if (x) + { + 1; + } +} + +/* Empty loop with space before the semicolon */ +float +baz() +{ + for (i=0; i<10; i++) ; +} + + +/* bad inline function */ +inline static int zero(void); + +/* long control block without braces around single statement body */ +if (this + && that + && the other) + do_stuff(); + + +/* Return statement without parens around the argument */ +int +foo() +{ + return 0; +} + +/* Void return statement without parens. This is ok */ +void +voidfoo() +{ + return ; +} + Added: user/asomers/style9/style9.py ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ user/asomers/style9/style9.py Tue May 29 21:37:02 2018 (r334355) @@ -0,0 +1,352 @@ +#! /usr/bin/env python +import optparse +import re +import string +import sys + +class Line(str): + """String that carries a line number with it""" + @property + def lineNumber(self): + "Line number where this line first occurred in the file" + try: + return self._lineNumber + except AttributeError: + return -1 + + @lineNumber.setter + def lineNumber(self, value): + self._lineNumber = value + + +class FileScanner(object): + """Scans a file for a regex""" + antipattern = '' + strip_comments = False + flags = 0 + def __init__(self): + self._reobj = re.compile(self.pattern, self.flags) + if self.antipattern: + self.__antipattern_obj = re.compile(self.antipattern, self.flags) + self.__comment_reobj = re.compile( + r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"', + re.DOTALL | re.MULTILINE + ) + + def _readlines(self, filename): + """Return an iterable that will yield each line in file, stripping + comments if self.strip_comments is set""" + f = open(filename, 'r') + if self.strip_comments: + reader = self._strip_comments(f.read()).split('\n') + else: + reader = f + n = 1 + for l in reader: + line = Line(l) + line.lineNumber = n + yield line + n += 1 + + def __replacer(self, match): + s = match.group(0) + if s.startswith('/'): + return "" + "\n" * s.count('\n') + else: + return s + + def _strip_comments(self, text): + #Thanks to MizardX on stackoverflow.com + return re.sub(self.__comment_reobj, self.__replacer, text) + + def finditer(self, filename): + """Returns a generator yielding every matching line in the file""" + f = self._readlines(filename) + for line in f: + mo = self._reobj.search(line) + if mo: + if not self.antipattern or not self.__antipattern_obj.search(line): + yield line + + def has_match(self, filename): + """Returns True iff at least one line in the file matches the pattern""" + f = self._readlines(filename) + for line in f: + mo = self._reobj.search(line) + if mo: + if not self.antipattern or not self.__antipattern_obj.search(line): + return True + break + return False + + +class Attribute(FileScanner): + """Use of __attribute__ outside of compiler-specific macro""" + pattern = '__attribute__' + + +class BraceSolo(FileScanner): + """Puts the opening brace of a control statement on its own line""" + strip_comments = True + pattern = r'^\s+{\s*$' + + +class BraceWithoutSpace(FileScanner): + """Omits a space before the { of a control block""" + strip_comments = True + pattern = r'[^a-zA-Z0-9_](if|while|for|switch)(\(|[^a-zA-Z0-9_;(]\S*\s*\().*\){' + + +class CppComment(FileScanner): + """C++ style comments""" + # Assume that if the first non-whitespace character of a line is '*', then + # the line is probably a continuation of a C-style comment + pattern = r'//' + antipattern = r'^\s*/?\*' + + +class EmptyLoopSemicolon(FileScanner): + """Puts space before the ; of a loop with no body""" + strip_comments = True + pattern = r'[^a-zA-Z0-9_](while|for)\s*\(.*\)\s+;' + + +class FuncInDeclaration(FileScanner): + """Calls a function in a variable declaration""" + strip_comments = True + pattern = r'^(\s+[a-zA-Z0-9_]+){2,}\s*=.*[a-zA-Z0-9_]\(' + + +class FuncOnSameLine(FileScanner): + """Function names on the same line as their return types in a definition""" + strip_comments = True + flags = re.MULTILINE | re.DOTALL + pattern = r'^[a-zA-Z0-9_][a-zA-Z0-9_ \t\r\f\v*]+[ \t\r\f\v]+\*?[a-zA-Z0-9_]+\([^)]*\)[{\t\r\f\v]*$' + + def finditer(self, filename): + """Returns a generator yielding every matching line in the file""" + file_as_str = self._strip_comments(open(filename, 'r').read()) + for mo in re.finditer(self._reobj, file_as_str): + line = Line(mo.group(0)) + line.lineNumber = file_as_str[0:mo.start(0)].count('\n') + 1 + yield line + + def has_match(self, filename): + """Returns True iff at least one line in the file matches the pattern""" + file_as_str = self._strip_comments(open(filename, 'r').read()) + mo = self._reobj.search(file_as_str) + if mo: + return True + else: + return False + + +class InlineStatic(FileScanner): + """Uses "inline static" instead of "static inline" """ + strip_comments = True + pattern = r'inline static' + + +class LongLines(FileScanner): + """Lines over 80 characters""" + def __init__(self): + pass + + def has_match(self, filename): + """Returns True iff at least one line in the file matches the pattern""" + f = open(filename, 'r') + for line in f: + expanded_line = string.expandtabs(line, 8) #re.sub('\t', ' ', line) + if len(expanded_line) > 81: #allow one character for the newline + return True + break + return False + + def finditer(self, filename): + """Returns a generator yielding every matching line in the file""" + for line in self._readlines(filename): + expanded_line = string.expandtabs(line, 8) #re.sub('\t', ' ', line) + if len(expanded_line) > 81: #allow one character for the newline + yield line + + +class MultilineFirst(FileScanner): + """Multiline comments with text on the first line""" + pattern = r'/\*.*\S+.*[^/\n]$' + + +class MultilineLast(FileScanner): + """Multiline comments with text on the last line""" + pattern = r'[a-zA-Z0-9_]+.*\*/' + antipattern = r'/\*' + + +class ReturnParens(FileScanner): + """Return statements must enclose their values in parenthesis""" + strip_comments = True + pattern = r'[^a-zA-Z0-9_]return\s*[^(; \t\n]' + +class RequireBraces(FileScanner): + """Always uses braces after a control statement""" + strip_comments = True + pattern = '^((.*\s+((if|else|elseif|for|do|switch))|([^}]*\s+while))\s*\(.*)$' + + def finditer(self, filename): + f = self._readlines(filename) + for line in f: + ctlStart = re.search(self.pattern, line) + if ctlStart: + curLine = ctlStart.group(0) + lParen = curLine.count('(') + rParen = curLine.count(')') + while lParen != rParen: + curLine = f.next() + lParen += curLine.count('(') + rParen += curLine.count(')') + if not '{' in curLine: + yield line + + def has_match(self, filename): + while self.finditer(filename): + return True + return False + + +class SpaceAfterKeyword(FileScanner): + """Omit a space after flow control keywords""" + strip_comments = True + pattern = r'[^a-zA-Z0-9_](if|while|for|return|switch|case)[^a-zA-Z0-9_ \t\r\f\v;]' + + +class TrailingWhitespace(FileScanner): + """Trailing whitespace""" + pattern = r'[ \t\r\f\v]$' + + +class UnnecessaryBraces(FileScanner): + """ Braces around a single line statement following a control block """ + pattern = '\s+(if|else|elseif|for|while|do|switch)\s*\(.*{' + ENDPATTERN = '^\s+}' + + def __init__(self): + self.strip_comments = True + self.ctlBlockLinesStack = [] + self.ctlBlockLines = 0 + super(UnnecessaryBraces, self).__init__() + + def finditer(self, filename): + f = self._readlines(filename) + for line in f: + if re.search(self.pattern, line): + # Started a new level of control block + self.ctlBlockLinesStack.append(self.ctlBlockLines) + self.ctlBlockLines = 0 + + elif re.search(self.ENDPATTERN, line): + # Ended a control block + if self.ctlBlockLines == 1: + yield line + if self.ctlBlockLinesStack: + self.ctlBlockLines += self.ctlBlockLinesStack.pop() + else: + # Just a regular line + self.ctlBlockLines += 1 + + def has_match(self, filename): + while self.finditer(filename): + return True + return False + +def main(): + usage = "usage: %prog [OPTIONS] FILE [FILE ...]" + parser = optparse.OptionParser(usage) + parser.add_option('-a', '--all', action="store_true", default=False, + help="Run all checks"); + parser.add_option('-v', '--verbose', action="store_true", default=False, + help="Print matching lines as well as filenames"); + + parser.add_option('--attribute', action="store_true", default=False, + help="Check for " + Attribute.__doc__); + parser.add_option('--brace-solo', action="store_true", default=False, + help="Check for " + BraceSolo.__doc__); + parser.add_option('--brace-without-space', action="store_true", + default=False, help="Check for " + BraceWithoutSpace.__doc__); + parser.add_option('--cpp-comments', action="store_true", default=False, + help="Check for " + CppComment.__doc__); + parser.add_option('--empty-loop-semicolon', action="store_true", + default=False, help="Check for " + EmptyLoopSemicolon.__doc__); + parser.add_option('--func-in-declaration', action="store_true", + default=False, help="Check for " + FuncInDeclaration.__doc__); + parser.add_option('--func-on-same-line', action="store_true", + default=False, help="Check for " + FuncOnSameLine.__doc__); + parser.add_option('--inline-static', action="store_true", default=False, + help="Check for " + InlineStatic.__doc__); + parser.add_option('--long-lines', action="store_true", default=False, + help="Check for " + LongLines.__doc__); + parser.add_option('--multiline-first', action="store_true", default=False, + help="Check for " + MultilineFirst.__doc__); + parser.add_option('--multiline-last', action="store_true", default=False, + help="Check for " + MultilineLast.__doc__); + parser.add_option('--return-parens', action="store_true", + default=False, help="Check for " + ReturnParens.__doc__); + parser.add_option('--space-after-keyword', action="store_true", + default=False, help="Check for " + SpaceAfterKeyword.__doc__); + parser.add_option('--trailing-whitespace', action="store_true", + default=False, help="Check for " + TrailingWhitespace.__doc__); + parser.add_option('--unnecessary-braces', action="store_true", + default=False, help="Check for " + UnnecessaryBraces.__doc__); + parser.add_option('--require-braces', action="store_true", + default=False, help="Check for " + RequireBraces.__doc__); + + (options, args) = parser.parse_args() + if len(args) < 1: + parser.error("Incorrect number of arguments") + + checks = [] + if options.all or options.attribute: + checks.append(Attribute()) + if options.all or options.brace_solo: + checks.append(BraceSolo()) + if options.all or options.brace_without_space: + checks.append(BraceWithoutSpace()) + if options.all or options.cpp_comments: + checks.append(CppComment()) + if options.all or options.empty_loop_semicolon: + checks.append(EmptyLoopSemicolon()) + if options.all or options.func_in_declaration: + checks.append(FuncInDeclaration()) + if options.all or options.inline_static: + checks.append(FuncOnSameLine()) + if options.all or options.inline_static: + checks.append(InlineStatic()) + if options.all or options.long_lines: + checks.append(LongLines()) + if options.all or options.multiline_first: + checks.append(MultilineFirst()) + if options.all or options.multiline_last: + checks.append(MultilineLast()) + if options.all or options.return_parens: + checks.append(ReturnParens()) + if options.all or options.space_after_keyword: + checks.append(SpaceAfterKeyword()) + if options.all or options.trailing_whitespace: + checks.append(TrailingWhitespace()) + if (options.all or options.unnecessary_braces) and not options.require_braces: + checks.append(UnnecessaryBraces()) + if options.require_braces: + checks.append(RequireBraces()) + + for check in checks: + #TODO: don't print check.desc if it has no matches + print "*** " + check.__doc__ + " ***" + for filename in args: + if options.verbose: + for line in check.finditer(filename): + print "%s:%d:%s" % (filename, line.lineNumber, line) + else: + if check.has_match(filename): + print filename + + +if __name__ == '__main__': + main() From owner-svn-src-user@freebsd.org Wed May 30 08:34:20 2018 Return-Path: Delivered-To: svn-src-user@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id A8A7FF77FE3 for ; Wed, 30 May 2018 08:34:20 +0000 (UTC) (envelope-from pho@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 5A88276E00; Wed, 30 May 2018 08:34:20 +0000 (UTC) (envelope-from pho@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 3BA2F516; Wed, 30 May 2018 08:34:20 +0000 (UTC) (envelope-from pho@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w4U8YKxZ012361; Wed, 30 May 2018 08:34:20 GMT (envelope-from pho@FreeBSD.org) Received: (from pho@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w4U8YKuh012356; Wed, 30 May 2018 08:34:20 GMT (envelope-from pho@FreeBSD.org) Message-Id: <201805300834.w4U8YKuh012356@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: pho set sender to pho@FreeBSD.org using -f From: Peter Holm Date: Wed, 30 May 2018 08:34:20 +0000 (UTC) To: src-committers@freebsd.org, svn-src-user@freebsd.org Subject: svn commit: r334377 - user/pho/stress2/misc X-SVN-Group: user X-SVN-Commit-Author: pho X-SVN-Commit-Paths: user/pho/stress2/misc X-SVN-Commit-Revision: 334377 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-user@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "SVN commit messages for the experimental " user" src tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 30 May 2018 08:34:21 -0000 Author: pho Date: Wed May 30 08:34:19 2018 New Revision: 334377 URL: https://svnweb.freebsd.org/changeset/base/334377 Log: Added a "sendfile(2) over socket pairs" test. Sponsored by: Dell EMC Isilon Added: user/pho/stress2/misc/sendfile15.sh (contents, props changed) Added: user/pho/stress2/misc/sendfile15.sh ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ user/pho/stress2/misc/sendfile15.sh Wed May 30 08:34:19 2018 (r334377) @@ -0,0 +1,219 @@ +#!/bin/sh + +# +# SPDX-License-Identifier: BSD-2-Clause-FreeBSD +# +# Copyright (c) 2018 Dell EMC Isilon +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $FreeBSD$ +# + +# sendfile() over socket pairs test. + +# Page fault in unp_dispose+0x90 seen: +# https://people.freebsd.org/~pho/stress/log/sendfile15.txt + +# "panic: bad pte va 800657000 pte 0" seen: +# https://people.freebsd.org/~pho/stress/log/sendfile15-2.txt + +# "panic: vm_page_free_prep 0xfffff817e0efac10 PG_ZERO 87 ...": +# https://people.freebsd.org/~pho/stress/log/sendfile15-2.txt + +. ../default.cfg +[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1 + +dir=/tmp +odir=`pwd` +cd $dir +sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile15.c +mycc -o sendfile15 -Wall -Wextra -O0 -g sendfile15.c || exit 1 +rm -f sendfile15.c +cd $odir + +set -e +mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint +[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart +mdconfig -a -t swap -s 2g -u $mdstart +bsdlabel -w md$mdstart auto +newfs $newfs_flags md${mdstart}$part > /dev/null +mount /dev/md${mdstart}$part $mntpoint +set +e + +cd $mntpoint +dd if=/dev/random of=input bs=4k count=1 status=none +(cd $odir/../testcases/swap; ./swap -t 5m -i 20 > /dev/null) & +$dir/sendfile15 +s=$? +while pgrep -q swap; do + pkill swap +done +wait +[ -f sendfile15.core -a $s -eq 0 ] && + { ls -l sendfile15.core; mv sendfile15.core $dir; s=1; } +cd $odir + +for i in `jot 6`; do + mount | grep -q "on $mntpoint " || break + umount $mntpoint && break || sleep 10 + [ $i -eq 6 ] && + { echo FATAL; fstat -mf $mntpoint; exit 1; } +done +mdconfig -d -u $mdstart +rm -rf $dir/sendfile15 +exit $s + +EOF +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +static volatile u_int *share; + +#define PARALLEL 128 +#define RUNTIME (5 * 60) +#define SYNC 0 + +static void +test(void) +{ + struct stat st; + off_t written, pos; + int child, error, from, n, status, sv[2]; + char *buf; + const char *from_name; + + atomic_add_int(&share[SYNC], 1); + while (share[SYNC] != PARALLEL) + ; + + from_name = "input"; + + if ((from = open(from_name, O_RDONLY)) == -1) + err(1, "open read %s", from_name); + + if ((error = fstat(from, &st)) == -1) + err(1, "stat %s", from_name); + + if ((error = socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) == -1) + err(1, "socketpair"); + + child = fork(); + if (child == -1) + err(1, "fork"); + else if (child != 0) { + setproctitle("parent"); + close(sv[1]); + pos = 0; + for (;;) { + error = sendfile(from, sv[0], pos, st.st_size - pos, + NULL, &written, 0); + if (error == -1) { + if (errno != EAGAIN) + err(1, "sendfile"); + } + pos += written; + if (pos == st.st_size) + break; + } + close(sv[0]); + if (waitpid(child, &status, 0) != child) + err(1, "waitpid(%d)", child); + } else { + setproctitle("child"); + close(sv[0]); + buf = malloc(st.st_size); + if (buf == NULL) + err(1, "malloc %jd", st.st_size); + pos = 0; + for (;;) { + written = st.st_size - pos; + n = read(sv[1], buf + pos, written); + if (n == -1) + err(1, "read"); + else if (n == 0) + errx(1, "Short read"); + pos += n; + if (pos == st.st_size) + break; + } + close(sv[1]); + _exit(0); + } + + _exit(0); +} + +int +main(void) +{ + pid_t pids[PARALLEL]; + size_t len; + time_t start; + int e, i, status; + + e = 0; + len = PAGE_SIZE; + if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED) + err(1, "mmap"); + + start = time(NULL); + while ((time(NULL) - start) < RUNTIME && e == 0) { + share[SYNC] = 0; + for (i = 0; i < PARALLEL; i++) { + if ((pids[i] = fork()) == 0) + test(); + if (pids[i] == -1) + err(1, "fork()"); + } + for (i = 0; i < PARALLEL; i++) { + if (waitpid(pids[i], &status, 0) == -1) + err(1, "waitpid(%d)", pids[i]); + if (status != 0) { + if (WIFSIGNALED(status)) + fprintf(stderr, + "pid %d exit signal %d\n", + pids[i], WTERMSIG(status)); + } + e += status == 0 ? 0 : 1; + } + } + + return (e); +} From owner-svn-src-user@freebsd.org Wed May 30 10:00:55 2018 Return-Path: Delivered-To: svn-src-user@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 34090F7D154 for ; Wed, 30 May 2018 10:00:55 +0000 (UTC) (envelope-from brde@optusnet.com.au) Received: from mail109.syd.optusnet.com.au (mail109.syd.optusnet.com.au [211.29.132.80]) by mx1.freebsd.org (Postfix) with ESMTP id 7859779D21; Wed, 30 May 2018 10:00:54 +0000 (UTC) (envelope-from brde@optusnet.com.au) Received: from [192.168.0.102] (c110-21-101-228.carlnfd1.nsw.optusnet.com.au [110.21.101.228]) by mail109.syd.optusnet.com.au (Postfix) with ESMTPS id 18AFCD6436D; Wed, 30 May 2018 20:00:46 +1000 (AEST) Date: Wed, 30 May 2018 20:00:45 +1000 (EST) From: Bruce Evans X-X-Sender: bde@besplex.bde.org To: Alan Somers cc: src-committers@freebsd.org, svn-src-user@freebsd.org Subject: Re: svn commit: r334355 - in user/asomers: . style9 In-Reply-To: <201805292137.w4TLb2PF080170@repo.freebsd.org> Message-ID: <20180530193603.S2597@besplex.bde.org> References: <201805292137.w4TLb2PF080170@repo.freebsd.org> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.2 cv=cIaQihWN c=1 sm=1 tr=0 a=PalzARQSbocsUSjMRkwAPg==:117 a=PalzARQSbocsUSjMRkwAPg==:17 a=kj9zAlcOel0A:10 a=iOz-SEPdZ0gwQ49tp2YA:9 a=CjuIK1q_8ugA:10 X-BeenThere: svn-src-user@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "SVN commit messages for the experimental " user" src tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 30 May 2018 10:00:55 -0000 On Tue, 29 May 2018, Alan Somers wrote: > Log: > user/asomers/style9: automatic style verifier > > This python script can automatically detect many common style(9) violations. > > Sponsored by: Spectra Logic Corp > > Added: > user/asomers/ > user/asomers/style9/ > user/asomers/style9/badstyle.c (contents, props changed) > user/asomers/style9/style9.py (contents, props changed) > > Added: user/asomers/style9/badstyle.c > ============================================================================== > --- /dev/null 00:00:00 1970 (empty, because file is newly added) > +++ user/asomers/style9/badstyle.c Tue May 29 21:37:02 2018 (r334355) > @@ -0,0 +1,73 @@ > +/* > + * this is a comment > + * with text on the last line */ > + > +/* func on same line as type; OK in a prototype */ > +int foo(); I wonder if the script detects all the style bugs or if so many are intentional. I doubt that the following in the above are intentional or can be detected without full English and C parsers: - sentences without capitalizaton or punctuation - indentaation affer type (before function name). indent(1) will fix this and about half of the comment misformatting, but needs a delicate choice of options. I usually tell indent to not reformat comments since comments tend to have too many misformattings and rewrapping comments gives enormous changes for small bugs. Declarations are also much less structured than statements so I would like to often ignore misformattings in them, but indent has no options for that. - K&R declaration - comment saying that a K&R declaration is a prototype - comment saying that something is OK when it is required. > + > + - double vertical spacing. Extra blank lines are poorly handled by indent(1), since some are not extra. But double blank lines usually have at least 1 extra. > +/* func on same line as type */ > +int foo(){ > +} - error list incomplete. This also has the left brace on the same line and no space before it. > +static const struct blargle *myfunc(int x, char *y, struct foo *f){ > +} > +/* this one is really long */ > +static int zfs_close(vnode_t *vp, int flag, int count, offset_t offset, > + cred_t *cr, caller_context_t *ct) - but better formatted than most long declarations. > + > +/* Omit the space after some keywords */ - the rules are not consistenly backwards, so it is not clear which ones are parodies > +int > +foo(){ > + if(1) { > + while(7){ > + if (foo){ > + } > + } > + } > +} > + > +// A C++ style comment > +int foo; //C++ style inline comment Some rules are not stated as [anti-]rules. > + > + > +/* Solo brace after control block */ > +long > +bar(){ > + if (x) > + { > + 1; > + } > +} > + > +/* Empty loop with space before the semicolon */ > +float > +baz() > +{ > + for (i=0; i<10; i++) ; > +} > + > + > +/* bad inline function */ > +inline static int zero(void); > + > +/* long control block without braces around single statement body */ > +if (this > + && that > + && the other) > + do_stuff(); - also excessive line splitting - also gnuish-style formatting of placement of operators after line splitting (start new lines with an operator, the opposite to KNF. Full gnu style also requires a different indentation than the above for the operators). > + > + > +/* Return statement without parens around the argument */ > +int > +foo() > +{ > + return 0; > +} > + > +/* Void return statement without parens. This is ok */ > +void > +voidfoo() > +{ > + return ; > +} > + - also spelling of OK as ok - also redundant return statement. Bruce From owner-svn-src-user@freebsd.org Fri Jun 1 05:48:30 2018 Return-Path: Delivered-To: svn-src-user@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 30AD5FE3F99 for ; Fri, 1 Jun 2018 05:48:30 +0000 (UTC) (envelope-from pho@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id D68997C969; Fri, 1 Jun 2018 05:48:29 +0000 (UTC) (envelope-from pho@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id B79D524BFE; Fri, 1 Jun 2018 05:48:29 +0000 (UTC) (envelope-from pho@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w515mTgo086653; Fri, 1 Jun 2018 05:48:29 GMT (envelope-from pho@FreeBSD.org) Received: (from pho@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w515mScj086649; Fri, 1 Jun 2018 05:48:28 GMT (envelope-from pho@FreeBSD.org) Message-Id: <201806010548.w515mScj086649@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: pho set sender to pho@FreeBSD.org using -f From: Peter Holm Date: Fri, 1 Jun 2018 05:48:28 +0000 (UTC) To: src-committers@freebsd.org, svn-src-user@freebsd.org Subject: svn commit: r334473 - user/pho/stress2/misc X-SVN-Group: user X-SVN-Commit-Author: pho X-SVN-Commit-Paths: user/pho/stress2/misc X-SVN-Commit-Revision: 334473 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-user@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "SVN commit messages for the experimental " user" src tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 01 Jun 2018 05:48:30 -0000 Author: pho Date: Fri Jun 1 05:48:28 2018 New Revision: 334473 URL: https://svnweb.freebsd.org/changeset/base/334473 Log: Added UNIX datagram socket tests. Sponsored by: Dell EMC Isilon Added: user/pho/stress2/misc/datagram.sh (contents, props changed) user/pho/stress2/misc/datagram2.sh (contents, props changed) user/pho/stress2/misc/datagram3.sh (contents, props changed) user/pho/stress2/misc/unix_socket.sh (contents, props changed) Added: user/pho/stress2/misc/datagram.sh ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ user/pho/stress2/misc/datagram.sh Fri Jun 1 05:48:28 2018 (r334473) @@ -0,0 +1,97 @@ +#!/bin/sh + +# +# SPDX-License-Identifier: BSD-2-Clause-FreeBSD +# +# Copyright (c) 2018 Dell EMC Isilon +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $FreeBSD$ +# + +# UNIX datagram socket test. + +# "panic: mutex unp not owned at ../../../kern/uipc_usrreq.c:879" seen: +# https://people.freebsd.org/~pho/stress/log/datagram.txt + +. ../default.cfg + +cd /tmp +cat > datagram.c < +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +char *filename = "/tmp/datagram.socket"; + +int +main(void) { + + struct sockaddr_un addr; + int bytes, sockfd; + char buf[1024]; + + unlink(filename); + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, filename, 104); + + if ((sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) + err(1, "socket"); + + if (bind(sockfd, (struct sockaddr *) &addr, + sizeof(addr)) == -1) + err(1, "bind"); + + if (connect(sockfd, (struct sockaddr *) &addr, + sizeof(addr)) == -1) + err(1, "connect"); + + bytes = read(sockfd, buf, sizeof(buf)); + + return (0); +} +EOF + +mycc -o datagram -Wall -Wextra -O2 -g datagram.c || exit 1 +rm datagram.c + +./datagram & +sleep 1 +kill $! +wait + +rm -f datagram /tmp/datagram.socket +exit 0 Added: user/pho/stress2/misc/datagram2.sh ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ user/pho/stress2/misc/datagram2.sh Fri Jun 1 05:48:28 2018 (r334473) @@ -0,0 +1,105 @@ +#!/bin/sh + +# +# SPDX-License-Identifier: BSD-2-Clause-FreeBSD +# +# Copyright (c) 2018 Dell EMC Isilon +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $FreeBSD$ +# + +# UNIX datagram socket test. + +# "panic: mutex unp not owned at ../../../kern/uipc_usrreq.c:879" seen. + +. ../default.cfg + +cd /tmp +cat > datagram2.c < +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +char *filename = "/tmp/datagram2.socket"; + +int +main(void) { + + struct message { struct cmsghdr msg_hdr; int fd; } m; + struct msghdr mh; + struct sockaddr_un addr; + ssize_t len; + int sockfd; + + unlink(filename); + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, filename, 104); + + if ((sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) + err(1, "socket"); + + if (bind(sockfd, (struct sockaddr *) &addr, + sizeof(addr)) == -1) + err(1, "bind"); + + if (connect(sockfd, (struct sockaddr *) &addr, + sizeof(addr)) == -1) + err(1, "connect"); + + bzero(&mh, sizeof(mh)); + bzero(&m, sizeof(m)); + mh.msg_control = &m; + mh.msg_controllen = sizeof(m); + m.msg_hdr.cmsg_len = sizeof(m); + m.msg_hdr.cmsg_level = SOL_SOCKET; + m.msg_hdr.cmsg_type = SCM_RIGHTS; + m.fd = sockfd; + len = sendmsg(sockfd, &mh, 0); + if (len < 0) + err(1, "sendmsg"); + + return (0); +} +EOF + +cc -o datagram2 -Wall -Wextra -O2 -g datagram2.c || exit 1 +rm -f datagram2.c datagram2.socket + +./datagram2 + +rm datagram2 datagram2.socket +exit 0 Added: user/pho/stress2/misc/datagram3.sh ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ user/pho/stress2/misc/datagram3.sh Fri Jun 1 05:48:28 2018 (r334473) @@ -0,0 +1,107 @@ +#!/bin/sh + +# +# SPDX-License-Identifier: BSD-2-Clause-FreeBSD +# +# Copyright (c) 2018 Dell EMC Isilon +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $FreeBSD$ +# + +# UNIX datagram socket test. + +# "panic: mutex unp not owned at ../../../kern/uipc_usrreq.c:879" seen. + +. ../default.cfg + +cd /tmp +cat > datagram3.c < +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +char *filename = "/tmp/datagram3.socket"; + +int +main(void) { + + struct message { struct cmsghdr msg_hdr; int fd; } m; + struct msghdr mh; + struct sockaddr_un addr; + ssize_t len; + int i, sockfd; + + unlink(filename); + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, filename, 104); + + if ((sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) + err(1, "socket"); + + if (bind(sockfd, (struct sockaddr *) &addr, + sizeof(addr)) == -1) + err(1, "bind"); + + if (connect(sockfd, (struct sockaddr *) &addr, + sizeof(addr)) == -1) + err(1, "connect"); + + for (i = 0; i < 32; i++) { + bzero(&mh, sizeof(mh)); + bzero(&m, sizeof(m)); + mh.msg_control = &m; + mh.msg_controllen = sizeof(m); + m.msg_hdr.cmsg_len = sizeof(m); + m.msg_hdr.cmsg_level = SOL_SOCKET; + m.msg_hdr.cmsg_type = SCM_RIGHTS; + m.fd = sockfd; + len = sendmsg(sockfd, &mh, 0); + if (len < 0) + err(1, "sendmsg"); + } + + return (0); +} +EOF + +mycc -o datagram3 -Wall -Wextra -O2 -g datagram3.c || exit 1 +rm -f datagram3.c datagram3.socket + +./datagram3 + +rm datagram3 datagram3.socket +exit 0 Added: user/pho/stress2/misc/unix_socket.sh ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ user/pho/stress2/misc/unix_socket.sh Fri Jun 1 05:48:28 2018 (r334473) @@ -0,0 +1,162 @@ +#!/bin/sh + +# +# SPDX-License-Identifier: BSD-2-Clause-FreeBSD +# +# Copyright (c) 2018 Dell EMC Isilon +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $FreeBSD$ +# + +# "panic: mutex unp not owned at ../../../kern/uipc_usrreq.c:879" seen: +# https://people.freebsd.org/~pho/stress/log/mmacy018.txt + +. ../default.cfg + +cd /tmp +cat > unix_socket.c < +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#define SOCK_FILE "/tmp/unix_socket.socket" + +static int +client(void) { + struct sockaddr_un addr; + int fd, len; + char buff[8192]; + + if ((fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) + err(1, "socket"); + + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + strcpy(addr.sun_path, SOCK_FILE); + unlink(SOCK_FILE); + if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) + err(1, "bind"); + + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + strcpy(addr.sun_path, SOCK_FILE); + if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) + err(1, "connect"); + + strcpy (buff, "1234567890"); + if (send(fd, buff, strlen(buff)+1, 0) == -1) + err(1, "send"); + printf ("sent i1234567890\n"); + + if ((len = recv(fd, buff, 8192, 0)) < 0) + err(1, "recv"); + printf ("receive %d %s\n", len, buff); + + if (fd >= 0) { + close(fd); + } + unlink (SOCK_FILE); + + return(0); +} + +static int +server() { + struct sockaddr_un addr; + struct sockaddr_un from; + socklen_t fromlen = sizeof(from); + int fd, len, ret; + char buff[8192]; + + unlink(SOCK_FILE); + if ((fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) + err(1, "socket"); + + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + strcpy(addr.sun_path, SOCK_FILE); + unlink(SOCK_FILE); + if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) + err(1, "bind"); + + alarm(2); + while ((len = recvfrom(fd, buff, 8192, 0, (struct sockaddr *)&from, + &fromlen)) > 0) { + printf ("recvfrom: %s\n", buff); + strcpy (buff, "transmit good!"); + ret = sendto(fd, buff, strlen(buff)+1, 0, + (struct sockaddr *)&from, fromlen); + if (ret < 0) { + perror("sendto"); + break; + } + } + + if (fd != -1) + close(fd); + + return (0); +} + +int +main(void) +{ + pid_t pid; + + if ((pid = fork()) == -1) + err(1, "fork"); + + if (pid == 0) { + server(); + _exit(0); + } + sleep(2); + client(); + + if (waitpid(pid, NULL, 0) != pid) + err(1, "waitpid"); + + return (0); +} +EOF + +cc -o unix_socket -Wall -Wextra -O2 -g unix_socket.c || exit +rm unix_socket.c + +./unix_socket > /dev/null + +rm unix_socket +exit 0