From owner-freebsd-current@FreeBSD.ORG Sun Feb 19 18:09:42 2006 Return-Path: X-Original-To: freebsd-current@freebsd.org Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 41C0316A420 for ; Sun, 19 Feb 2006 18:09:42 +0000 (GMT) (envelope-from joseph.koshy@gmail.com) Received: from xproxy.gmail.com (xproxy.gmail.com [66.249.82.205]) by mx1.FreeBSD.org (Postfix) with ESMTP id A6E3843D49 for ; Sun, 19 Feb 2006 18:09:41 +0000 (GMT) (envelope-from joseph.koshy@gmail.com) Received: by xproxy.gmail.com with SMTP id h29so540729wxd for ; Sun, 19 Feb 2006 10:09:40 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=mg/xbDAb3J5gnk/JEhr9cC0f8OvAAyQvekeejO760D6pHeoPWXzUs/3zTH9Nwel2BOxUyJZNiQyGb4pYkAQxxiBy8z70kPF5HeXLzn7ltfGJeaE2P5u0O5KR8T3qM/HyNU3gl18gyTuealUtTSd5eg5Kir3gAbOzhhPzB+Fh5EE= Received: by 10.70.89.7 with SMTP id m7mr914674wxb; Sun, 19 Feb 2006 10:09:39 -0800 (PST) Received: by 10.70.116.16 with HTTP; Sun, 19 Feb 2006 10:09:39 -0800 (PST) Message-ID: <84dead720602191009p5fa3c317w254b13454dbc8cb8@mail.gmail.com> Date: Sun, 19 Feb 2006 23:39:39 +0530 From: "Joseph Koshy" To: "David Xu" In-Reply-To: <43F16E2D.5010109@freebsd.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_13861_23762493.1140372579894" References: <200602101246.31666.root@solink.ru> <200602141023.35850.root@solink.ru> <84dead720602132054y6d114b1eo8126fca84ac8972a@mail.gmail.com> <200602141058.24366.root@solink.ru> <43F1656A.2050100@freebsd.org> <84dead720602132140p1cc2b471h5c25c877b7311791@mail.gmail.com> <43F16E2D.5010109@freebsd.org> Cc: Bachilo Dmitry , freebsd-current@freebsd.org Subject: Re: Bad system call (core dumped) X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 19 Feb 2006 18:09:42 -0000 ------=_Part_13861_23762493.1140372579894 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline jk> Userland code should be checking for kernel support for jk> features before attempting to use them. dx> I don't think user code should check it before it can be dx> used, actually, it is being used by thread libraries to dx> implement named semaphore, it is requred by POSIX. Well, support for posix semaphores is not turned on in GENERIC, and so code that calls sem_init(3) is rudely sent a SIGSYS today. How about the attached patch to make our libraries better behaved with kernels that lack the P1003_1B_SEMAPHORES option? -- FreeBSD Volunteer, http://people.freebsd.org/~jkoshy ------=_Part_13861_23762493.1140372579894 Content-Type: text/plain; name=p.txt; charset=us-ascii Content-Transfer-Encoding: 7bit X-Attachment-Id: f_ejvo90fc Content-Disposition: attachment; filename="p.txt" Index: libc/gen/sem.c =================================================================== RCS file: /cvs/FreeBSD/src/lib/libc/gen/sem.c,v retrieving revision 1.15 diff -u -u -r1.15 sem.c --- libc/gen/sem.c 18 Oct 2005 17:24:03 -0000 1.15 +++ libc/gen/sem.c 19 Feb 2006 17:48:17 -0000 @@ -59,6 +59,7 @@ #include "namespace.h" #include #include +#include #include #include #include @@ -70,6 +71,7 @@ #include "un-namespace.h" #include "libc_private.h" +static int sem_supported = -1; /* changed by the first call to __sem_init() */ static sem_t sem_alloc(unsigned int value, semid_t semid, int system_sem); static void sem_free(sem_t sem); @@ -140,6 +142,21 @@ __sem_init(sem_t *sem, int pshared, unsigned int value) { semid_t semid; + int mib[2]; + size_t len; + + if (sem_supported < 0) { + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_SEMAPHORES; + len = sizeof(sem_supported); + if (sysctl(mib, 2, &sem_supported, &len, NULL, 0) < 0) + sem_supported = 0; + } + + if (!sem_supported) { + errno = ENOSYS; + return -1; + } /* * We always have to create the kernel semaphore if the Index: libc/gen/sem_init.3 =================================================================== RCS file: /cvs/FreeBSD/src/lib/libc/gen/sem_init.3,v retrieving revision 1.18 diff -u -u -r1.18 sem_init.3 --- libc/gen/sem_init.3 13 Jul 2005 13:15:21 -0000 1.18 +++ libc/gen/sem_init.3 19 Feb 2006 18:02:56 -0000 @@ -78,6 +78,10 @@ .Dv SEM_VALUE_MAX . .It Bq Er ENOSPC Memory allocation error. +.It Bq Er ENOSYS +The system does not support +.Tn POSIX +semaphores. .It Bq Er EPERM Unable to initialize a shared semaphore. .El @@ -94,6 +98,12 @@ function conforms to .St -p1003.1-96 . .Pp +Support for +.Tn POSIX +semaphores requires the currently running kernel to have been configured +with +.Cd "options P1003_1B_SEMAPHORES" . +.Pp This implementation does not support shared semaphores, and reports this fact by setting .Va errno Index: libthr/thread/thr_sem.c =================================================================== RCS file: /cvs/FreeBSD/src/lib/libthr/thread/thr_sem.c,v retrieving revision 1.6 diff -u -u -r1.6 thr_sem.c --- libthr/thread/thr_sem.c 1 Sep 2005 15:21:23 -0000 1.6 +++ libthr/thread/thr_sem.c 19 Feb 2006 17:34:56 -0000 @@ -32,6 +32,7 @@ #include "namespace.h" #include +#include #include #include #include @@ -44,6 +45,7 @@ #include "thr_private.h" +static int sem_supported = -1; /* changed by the first call to _sem_init() */ __weak_reference(_sem_init, sem_init); __weak_reference(_sem_destroy, sem_destroy); @@ -99,6 +101,21 @@ _sem_init(sem_t *sem, int pshared, unsigned int value) { semid_t semid; + int mib[2]; + size_t len; + + if (sem_supported < 0) { + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_SEMAPHORES; + len = sizeof(sem_supported); + if (sysctl(mib, 2, &sem_supported, &len, NULL, 0) < 0) + sem_supported = 0; + } + + if (!sem_supported) { + errno = ENOSYS; + return -1; + } semid = (semid_t)SEM_USER; if ((pshared != 0) && (ksem_init(&semid, value) != 0)) Index: libpthread/thread/thr_sem.c =================================================================== RCS file: /cvs/FreeBSD/src/lib/libpthread/thread/thr_sem.c,v retrieving revision 1.17 diff -u -u -r1.17 thr_sem.c --- libpthread/thread/thr_sem.c 1 Sep 2005 15:16:46 -0000 1.17 +++ libpthread/thread/thr_sem.c 19 Feb 2006 17:44:36 -0000 @@ -31,6 +31,7 @@ #include "namespace.h" #include +#include #include #include #include @@ -43,7 +44,6 @@ #include "libc_private.h" #include "thr_private.h" - extern int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *); extern int pthread_cond_timedwait(pthread_cond_t *, pthread_mutex_t *, struct timespec *); @@ -53,6 +53,7 @@ __weak_reference(_sem_timedwait, sem_timedwait); __weak_reference(_sem_post, sem_post); +static int sem_supported = -1; /* changed by the first call to _sem_init() */ static inline int sem_check_validity(sem_t *sem) @@ -123,6 +124,21 @@ _sem_init(sem_t *sem, int pshared, unsigned int value) { semid_t semid; + int mib[2]; + size_t len; + + if (sem_supported < 0) { + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_SEMAPHORES; + len = sizeof(sem_supported); + if (sysctl(mib, 2, &sem_supported, &len, NULL, 0) < 0) + sem_supported = 0; + } + + if (!sem_supported) { + errno = ENOSYS; + return -1; + } semid = (semid_t)SEM_USER; if ((pshared != 0) && (ksem_init(&semid, value) != 0)) ------=_Part_13861_23762493.1140372579894--