From owner-freebsd-stable Fri May 10 5:13:27 2002 Delivered-To: freebsd-stable@freebsd.org Received: from mail.pcnet.com (pcnet1.pcnet.com [204.213.232.3]) by hub.freebsd.org (Postfix) with ESMTP id C9C7237B403 for ; Fri, 10 May 2002 05:13:22 -0700 (PDT) Received: from localhost (eischen@localhost) by mail.pcnet.com (8.12.3/8.12.1) with ESMTP id g4ACDKwb023664; Fri, 10 May 2002 08:13:21 -0400 (EDT) Date: Fri, 10 May 2002 08:13:20 -0400 (EDT) From: Daniel Eischen To: Victor Ivanov Cc: freebsd-stable@FreeBSD.ORG Subject: Re: pthread app from init (/etc/ttys)? In-Reply-To: <20020510103805.A99419@icon.icon.bg> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-stable@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Fri, 10 May 2002, Victor Ivanov wrote: > Hi :) > > I'm developing an pthread app which is run from init on a virtual tty (it's > not run from a user, it must be run automatically). Yesterday I upgraded to > 4.6-PRERELEASE and the app started acting strangely. > The app has a screen-oriented UI, and it needs threads to do data > collecting and client/server communication "in the background" while > showing some nice window oriented interface. > > To show the problem I've made a small app which opens the tty the same way > as getty does it. First it chmod/chown/revokes it, the int opens it O_RDWR, > and then it calls login_tty to make it the controlling terminal, stdin, > stderr, stdout. > > All works fine as long as the app is not compiled/linked -pthread. In the > latter case none of the initialization calls return error, but after that > input/output to the console returns 'Resource temporary unavailable'. > > I've tried dup2-ing and TIOCSCTTY-ing and etc instead of using login_tty, > but the result is just the same. > > I guess I should make some kind of wrapper app or start getty using > autologin and specifiying the app as the user's shell (which user must > be uid 0) ... but I really prefer the simple way. > > Any hints/ideas/workarounds? You can try this patch (to -current, massage it a bit for -stable). -- Dan Eischen RCS file: /opt/d/CVS/src/lib/libc_r/uthread/uthread_init.c,v retrieving revision 1.38 diff -u -r1.38 uthread_init.c --- uthread_init.c 19 Mar 2002 22:58:56 -0000 1.38 +++ uthread_init.c 10 May 2002 11:15:09 -0000 @@ -173,37 +173,24 @@ if ((references[0] == NULL) || (libgcc_references[0] == NULL)) PANIC("Failed loading mandatory references in _thread_init"); - /* - * Check for the special case of this process running as - * or in place of init as pid = 1: - */ - if (getpid() == 1) { + /* Handle standard I/O file descriptors. */ + for (i = 0; i < 3; i++) { /* - * Setup a new session for this process which is - * assumed to be running as root. + * Make sure the pipe does not get in the way of stdio: */ - if (setsid() == -1) - PANIC("Can't set session ID"); - if (revoke(_PATH_CONSOLE) != 0) - PANIC("Can't revoke console"); - if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0) - PANIC("Can't open console"); - if (setlogin("root") == -1) - PANIC("Can't set login to root"); - if (__sys_ioctl(fd,TIOCSCTTY, (char *) NULL) == -1) - PANIC("Can't set controlling terminal"); - if (__sys_dup2(fd,0) == -1 || - __sys_dup2(fd,1) == -1 || - __sys_dup2(fd,2) == -1) - PANIC("Can't dup2"); - } - - /* Get the standard I/O flags before messing with them : */ - for (i = 0; i < 3; i++) + if (_thread_kern_pipe[i] < 3) { + fd = __sys_fcntl(_thread_kern_pipe[i], F_DUPFD, 3); + if (fd == -1) + PANIC("Cannot create kernel pipe"); + __sys_close(_thread_kern_pipe[i]); + _thread_kern_pipe[i] = fd; + } + /* Get the standard I/O flags before messing with them : */ if (((_pthread_stdio_flags[i] = __sys_fcntl(i,F_GETFL, NULL)) == -1) && (errno != EBADF)) PANIC("Cannot get stdio flags"); + } /* * Create a pipe that is written to by the signal handler to prevent To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-stable" in the body of the message