From 1474b80f017c2fcc18b1de5bbc51884650d30582 Mon Sep 17 00:00:00 2001 From: Roland McGrath Date: Mon, 27 Mar 1995 10:00:09 +0000 Subject: Mon Mar 27 02:23:15 1995 Roland McGrath * posix/unistd.h [__USE_BSD] (ttyslot): Declare it. * posix/unistd.h [__USE_BSD] (L_SET, L_INCR, L_XTND): Define unless L_SET already defined. * misc/sys/file.h (L_SET, L_INCR, L_XTND): Don't define if L_SET already defined. Incorporated -lutil library from 4.4-Lite. * misc/Makefile (extra-libs, libutil-routines): New variables. * misc/login.c, misc/login_tty.c, misc/logout.c, misc/logwtmp.c, sysdeps/generic/pty.c: New files for -lutil incorporated from 4.4-Lite. Support simple, light-weight unwind-protect mechanism for longjmp. * setjmp/longjmp.c: Call _longjmp_unwind first thing. * sysdeps/mach/hurd/jmp-unwind.c: New file. * sysdeps/stub/jmp-unwind.c: New file. * sysdeps/i386/jmp_buf.h (_JMPBUF_UNWINDS): New macro. * sysdeps/mips/jmp_buf.h (_JMPBUF_UNWINDS): New macro. * setjmp/Makefile (routines): Add jmp-unwind. * hurd/hurd/userlink.h (struct hurd_userlink): Move `next' and `prevp' members into new substructure `resource'; add another such substructure `thread' and members `cleanup' (function ptr) and `cleanup_data' (generic ptr). (_hurd_userlink_link, _hurd_userlink_unlink): Insert/remove LINK into the `_hurd_self_sigstate ()->active_resources' list via the `thread' substructure. * hurd/hurd/port.h (_hurd_port_cleanup): Declare it. (_hurd_port_locked_get): Set LINK->cleanup to _hurd_port_cleanup and LINK->cleanup_data' to the port extracted. * hurd/hurd/signal.h (struct hurd_sigstate): New member `active_resources'. * hurd/port-cleanup.c: New file. * hurd/Makefile (routines): Add port-cleanup. * malloc/malloc.c: Include errno.h. Sat Mar 25 18:24:21 1995 Roland McGrath * locale/loadlocale.c (_nl_load_locale): Avoid */ inside comment. --- ChangeLog | 42 ++++++++++++++ hurd/Makefile | 2 +- hurd/hurd/port.h | 11 +++- hurd/hurd/signal.h | 5 ++ hurd/hurd/userlink.h | 99 +++++++++++++++++++++++--------- hurd/port-cleanup.c | 31 ++++++++++ locale/loadlocale.c | 3 +- misc/Makefile | 9 ++- misc/login.c | 63 ++++++++++++++++++++ misc/login_tty.c | 55 ++++++++++++++++++ misc/logout.c | 72 +++++++++++++++++++++++ misc/logwtmp.c | 67 ++++++++++++++++++++++ misc/sys/file.h | 2 + posix/getopt.c | 7 ++- posix/unistd.h | 14 +++++ setjmp/Makefile | 2 +- setjmp/longjmp.c | 5 ++ sysdeps/generic/pty.c | 127 +++++++++++++++++++++++++++++++++++++++++ sysdeps/i386/jmp_buf.h | 5 ++ sysdeps/mach/hurd/jmp-unwind.c | 62 ++++++++++++++++++++ sysdeps/mips/jmp_buf.h | 10 +++- sysdeps/stub/jmp-unwind.c | 29 ++++++++++ 22 files changed, 682 insertions(+), 40 deletions(-) create mode 100644 hurd/port-cleanup.c create mode 100644 misc/login.c create mode 100644 misc/login_tty.c create mode 100644 misc/logout.c create mode 100644 misc/logwtmp.c create mode 100644 sysdeps/generic/pty.c create mode 100644 sysdeps/mach/hurd/jmp-unwind.c create mode 100644 sysdeps/stub/jmp-unwind.c diff --git a/ChangeLog b/ChangeLog index fa446ed509..6006bcb991 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,45 @@ +Mon Mar 27 02:23:15 1995 Roland McGrath + + * posix/unistd.h [__USE_BSD] (ttyslot): Declare it. + + * posix/unistd.h [__USE_BSD] (L_SET, L_INCR, L_XTND): Define + unless L_SET already defined. + * misc/sys/file.h (L_SET, L_INCR, L_XTND): Don't define if L_SET + already defined. + + Incorporated -lutil library from 4.4-Lite. + * misc/Makefile (extra-libs, libutil-routines): New variables. + * misc/login.c, misc/login_tty.c, misc/logout.c, misc/logwtmp.c, + sysdeps/generic/pty.c: New files for -lutil incorporated from 4.4-Lite. + + Support simple, light-weight unwind-protect mechanism for longjmp. + * setjmp/longjmp.c: Call _longjmp_unwind first thing. + * sysdeps/mach/hurd/jmp-unwind.c: New file. + * sysdeps/stub/jmp-unwind.c: New file. + * sysdeps/i386/jmp_buf.h (_JMPBUF_UNWINDS): New macro. + * sysdeps/mips/jmp_buf.h (_JMPBUF_UNWINDS): New macro. + * setjmp/Makefile (routines): Add jmp-unwind. + * hurd/hurd/userlink.h (struct hurd_userlink): Move `next' and + `prevp' members into new substructure `resource'; add another such + substructure `thread' and members `cleanup' (function ptr) and + `cleanup_data' (generic ptr). + (_hurd_userlink_link, _hurd_userlink_unlink): Insert/remove LINK + into the `_hurd_self_sigstate ()->active_resources' list via the + `thread' substructure. + * hurd/hurd/port.h (_hurd_port_cleanup): Declare it. + (_hurd_port_locked_get): Set LINK->cleanup to _hurd_port_cleanup and + LINK->cleanup_data' to the port extracted. + * hurd/hurd/signal.h (struct hurd_sigstate): New member + `active_resources'. + * hurd/port-cleanup.c: New file. + * hurd/Makefile (routines): Add port-cleanup. + + * malloc/malloc.c: Include errno.h. + +Sat Mar 25 18:24:21 1995 Roland McGrath + + * locale/loadlocale.c (_nl_load_locale): Avoid */ inside comment. + Fri Mar 24 02:35:37 1995 Roland McGrath * misc/Makefile (headers): Add utmp.h. diff --git a/hurd/Makefile b/hurd/Makefile index 386267c649..278d8ec315 100644 --- a/hurd/Makefile +++ b/hurd/Makefile @@ -49,7 +49,7 @@ routines = hurdinit hurdid hurdlookup hurdpid hurdrlimit hurdprio hurdexec \ fopenport \ vpprintf \ ports-get ports-set hurdports hurdmsg \ - $(sig) $(dtable) hurdinline + $(sig) $(dtable) hurdinline port-cleanup sig = hurdsig hurdfault faultexc siginfo hurd-raise preempt-sig \ trampoline longjmp-ts catch-exc exc2signal hurdkill dtable = dtable port2fd new-fd alloc-fd intern-fd \ diff --git a/hurd/hurd/port.h b/hurd/hurd/port.h index a057503d4a..9de021c45a 100644 --- a/hurd/hurd/port.h +++ b/hurd/hurd/port.h @@ -1,5 +1,5 @@ /* Lightweight user references for ports. -Copyright (C) 1993, 1994 Free Software Foundation, Inc. +Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -70,6 +70,9 @@ _hurd_port_init (struct hurd_port *port, mach_port_t init) } +/* Cleanup function for non-local exits. */ +extern void _hurd_port_cleanup (void *, jmp_buf, int); + /* Get a reference to *PORT, which is locked. Pass return value and LINK to _hurd_port_free when done. */ @@ -80,7 +83,11 @@ _hurd_port_locked_get (struct hurd_port *port, mach_port_t result; result = port->port; if (result != MACH_PORT_NULL) - _hurd_userlink_link (&port->users, link); + { + link->cleanup = &_hurd_port_cleanup; + link->cleanup_data = (void *) result; + _hurd_userlink_link (&port->users, link); + } __spin_unlock (&port->lock); return result; } diff --git a/hurd/hurd/signal.h b/hurd/hurd/signal.h index 76007d5037..d88d287de3 100644 --- a/hurd/hurd/signal.h +++ b/hurd/hurd/signal.h @@ -79,6 +79,11 @@ struct hurd_sigstate will be passed to sigreturn after running the handler for a pending signal, instead of examining the thread state. */ struct sigcontext *context; + + /* This is the head of the thread's list of active resources; see + for details. This member is only used by the + thread itself, and always inside a critical section. */ + struct hurd_userlink *active_resources; }; /* Linked list of states of all threads whose state has been asked for. */ diff --git a/hurd/hurd/userlink.h b/hurd/hurd/userlink.h index 337d46aef6..d61e2e2a7e 100644 --- a/hurd/hurd/userlink.h +++ b/hurd/hurd/userlink.h @@ -1,5 +1,5 @@ /* Support for chains recording users of a resource; `struct hurd_userlink'. -Copyright (C) 1994 Free Software Foundation, Inc. +Copyright (C) 1994, 1995 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -25,19 +25,48 @@ Cambridge, MA 02139, USA. */ #define __need_NULL #include - -/* This structure is simply a doubly-linked list. Users of a given - resource are recorded by their presence in a list associated with that - resource. A user attaches his own link (in local storage) to a shared - chain at the time he begins using some resource. When finished with - that resource, the user removes his link from the chain. If his link is - the last (there are no other users of the resource), and his chain has - been detached from the shared cell (the resource in the cell has been - replaced), then the user deallocates the resource that he used. */ +#include +#include + + +/* This structure records a link in two doubly-linked lists. + We call these the per-resource user list and the per-thread + active-resource list. + + Users of a given resource are recorded by their presence in a list + associated with that resource. A user attaches his own link (in local + storage on his stack) to a shared chain at the time he begins using some + resource. When finished with that resource, the user removes his link + from the chain. If his link is the last (there are no other users of + the resource), and his chain has been detached from the shared cell (the + resource in the cell has been replaced), then the user deallocates the + resource that he used. + + All uses of shared resources by a single thread are linked together by + its `active-resource' list; the head of this list is stored in the + per-thread sigstate structure. When the thread makes a non-local exit + (i.e. longjmp), it will examine its active-resource list, and each link + residing in a stack frame being jumped out of will be unlinked from both + the resource's user list and the thread's active-resource list, and + deallocate the resource if that was the last user link for that resource. + + NOTE: Access to a thread's active-resource list must always be done + inside a signal-proof critical section; the functions in this file + assume they are called inside a critical section, and do no locking of + their own. Also important: the longjmp cleanup relies on all userlink + structures residing on the stack of the using thread. */ struct hurd_userlink { - struct hurd_userlink *next, **prevp; + struct + { + struct hurd_userlink *next, **prevp; + } resource, thread; + + /* This function is called when a non-local exit + unwinds the frame containing this link. */ + void (*cleanup) (void *cleanup_data, jmp_buf env, int val); + void *cleanup_data; }; @@ -52,32 +81,46 @@ _EXTERN_INLINE void _hurd_userlink_link (struct hurd_userlink **chainp, struct hurd_userlink *link) { - link->next = *chainp; - if (link->next) - link->next->prevp = &link->next; - link->prevp = chainp; + struct hurd_userlink **thread_chainp; + + link->resource.next = *chainp; + if (link->resource.next) + link->resource.next->thread.prevp = &link->resource.next; + link->resource.prevp = chainp; *chainp = link; + + /* Also chain it on the current thread's list of active resources. */ + thread_chainp = &_hurd_self_sigstate ()->active_resources; + link->thread.next = *thread_chainp; + if (link->thread.next) + link->thread.next->thread.prevp = &link->thread.next; + link->thread.prevp = thread_chainp; + *thread_chainp = link; } -/* Detach LINK from its chain. If the return value is nonzero, the caller - should deallocate the resource he started using after attaching LINK to - the chain it's on. If the return value is zero, then someone else is - still using the resource. */ +/* Detach LINK from its chain. Returns nonzero iff this was the + last user of the resource and it should be deallocated. */ _EXTERN_INLINE int _hurd_userlink_unlink (struct hurd_userlink *link) { - /* The caller should deallocate the resource he used if his chain has - been detached from the cell (and thus has a nil `prevp'), and there is - no next link representing another user reference to the same resource. */ - int dealloc = ! link->next && ! link->prevp; + /* We should deallocate the resource used if this chain has been detached + from the cell (and thus has a nil `prevp'), and there is no next link + representing another user reference to the same resource. */ + int dealloc = ! link->resource.next && ! link->resource.prevp; /* Remove our link from the chain of current users. */ - if (link->prevp) - *link->prevp = link->next; - if (link->next) - link->next->prevp = link->prevp; + if (link->resource.prevp) + *link->resource.prevp = link->resource.next; + if (link->resource.next) + link->resource.next->resource.prevp = link->resource.prevp; + + /* Remove our link from the chain of currently active resources + for this thread. */ + *link->thread.prevp = link->thread.next; + if (link->thread.next) + link->thread.next->thread.prevp = link->thread.prevp; return dealloc; } @@ -97,7 +140,7 @@ _hurd_userlink_clear (struct hurd_userlink **chainp) /* Detach the chain of current users from the cell. The last user to remove his link from that chain will deallocate the old resource. */ - (*chainp)->prevp = NULL; + (*chainp)->resource.prevp = NULL; *chainp = NULL; return 0; } diff --git a/hurd/port-cleanup.c b/hurd/port-cleanup.c new file mode 100644 index 0000000000..556baaace0 --- /dev/null +++ b/hurd/port-cleanup.c @@ -0,0 +1,31 @@ +/* Cleanup function for `struct hurd_port' users who longjmp. +Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#include +#include + +/* The last user of the send right CLEANUP_DATA is now doing + `longjmp (ENV, VAL)', and this will unwind the frame of + that last user. Deallocate the right he will never get back to using. */ + +void +_hurd_port_cleanup (void *cleanup_data, jmp_buf env, int val) +{ + __mach_port_deallocate (__mach_task_self (), (mach_port_t) cleanup_data); +} diff --git a/locale/loadlocale.c b/locale/loadlocale.c index a8cf7d5448..8066fbbaa3 100644 --- a/locale/loadlocale.c +++ b/locale/loadlocale.c @@ -83,7 +83,8 @@ _nl_load_locale (int category, char **name) goto puntfd; if (S_ISDIR (st.st_mode)) { - /* LOCALE/LC_* is a directory; open LOCALE/LC_*/SYS_LC_* instead. */ + /* LOCALE/LC_foo is a directory; open LOCALE/LC_foo/SYS_LC_foo + instead. */ __close (fd); memcpy (stpcpy (strchr (file, '\0'), "SYS_"), catname, catlen); fd = __open (file, O_RDONLY); diff --git a/misc/Makefile b/misc/Makefile index e4f9786cf9..3c87abf9ec 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -56,13 +56,16 @@ extra-objs := bsd-compat.o install-lib := libbsd-compat.a libg.a non-lib.a := libbsd-compat.a +# Build the -lutil library with these extra functions. +extra-libs := libutil +libutil-routines:= login login_tty logout logwtmp pty + + include ../Rules + $(objpfx)libbsd-compat.a: $(objpfx)bsd-compat.o rm -f $@ ln $< $@ -lib: $(objpfx)libbsd-compat.a - $(objpfx)libg.a: $(dep-dummy-lib); $(make-dummy-lib) -lib: $(objpfx)libg.a diff --git a/misc/login.c b/misc/login.c new file mode 100644 index 0000000000..911c2892d2 --- /dev/null +++ b/misc/login.c @@ -0,0 +1,63 @@ +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)login.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include + +#include +#include +#include +#include +#include + +void +login(ut) + struct utmp *ut; +{ + register int fd; + int tty; + + tty = ttyslot(); + if (tty > 0 && (fd = open(_PATH_UTMP, O_WRONLY|O_CREAT, 0644)) >= 0) { + (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), L_SET); + (void)write(fd, ut, sizeof(struct utmp)); + (void)close(fd); + } + if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) >= 0) { + (void)write(fd, ut, sizeof(struct utmp)); + (void)close(fd); + } +} diff --git a/misc/login_tty.c b/misc/login_tty.c new file mode 100644 index 0000000000..6ea2b9b07f --- /dev/null +++ b/misc/login_tty.c @@ -0,0 +1,55 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)login_tty.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +int +login_tty(fd) + int fd; +{ + (void) setsid(); + if (ioctl(fd, TIOCSCTTY, (char *)NULL) == -1) + return (-1); + (void) dup2(fd, 0); + (void) dup2(fd, 1); + (void) dup2(fd, 2); + if (fd > 2) + (void) close(fd); + return (0); +} diff --git a/misc/logout.c b/misc/logout.c new file mode 100644 index 0000000000..11da24f1c8 --- /dev/null +++ b/misc/logout.c @@ -0,0 +1,72 @@ +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)logout.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#include +#include +#include +#include +#include + +typedef struct utmp UTMP; + +int +logout(line) + register char *line; +{ + register int fd; + UTMP ut; + int rval; + + if ((fd = open(_PATH_UTMP, O_RDWR, 0)) < 0) + return(0); + rval = 0; + while (read(fd, &ut, sizeof(UTMP)) == sizeof(UTMP)) { + if (!ut.ut_name[0] || strncmp(ut.ut_line, line, UT_LINESIZE)) + continue; + bzero(ut.ut_name, UT_NAMESIZE); + bzero(ut.ut_host, UT_HOSTSIZE); + (void)time(&ut.ut_time); + (void)lseek(fd, -(off_t)sizeof(UTMP), L_INCR); + (void)write(fd, &ut, sizeof(UTMP)); + rval = 1; + } + (void)close(fd); + return(rval); +} diff --git a/misc/logwtmp.c b/misc/logwtmp.c new file mode 100644 index 0000000000..d55ac9e941 --- /dev/null +++ b/misc/logwtmp.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)logwtmp.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +#include +#include + +logwtmp(line, name, host) + char *line, *name, *host; +{ + struct utmp ut; + struct stat buf; + int fd; + time_t time(); + char *strncpy(); + + if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) < 0) + return; + if (fstat(fd, &buf) == 0) { + (void) strncpy(ut.ut_line, line, sizeof(ut.ut_line)); + (void) strncpy(ut.ut_name, name, sizeof(ut.ut_name)); + (void) strncpy(ut.ut_host, host, sizeof(ut.ut_host)); + (void) time(&ut.ut_time); + if (write(fd, (char *)&ut, sizeof(struct utmp)) != + sizeof(struct utmp)) + (void) ftruncate(fd, buf.st_size); + } + (void) close(fd); +} diff --git a/misc/sys/file.h b/misc/sys/file.h index 51d07bc160..fa4cb26239 100644 --- a/misc/sys/file.h +++ b/misc/sys/file.h @@ -30,9 +30,11 @@ __BEGIN_DECLS /* Alternate names for values for the WHENCE argument to `lseek'. These are the same as SEEK_SET, SEEK_CUR, and SEEK_END, respectively. */ +#ifndef L_SET #define L_SET 0 /* Seek from beginning of file. */ #define L_INCR 1 /* Seek from current position. */ #define L_XTND 2 /* Seek from end of file. */ +#endif /* Operations for the `flock' call. */ diff --git a/posix/getopt.c b/posix/getopt.c index 7fef53a0b3..85647e2d36 100644 --- a/posix/getopt.c +++ b/posix/getopt.c @@ -3,7 +3,7 @@ "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu before changing it! - Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94 + Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95 Free Software Foundation, Inc. This file is part of the GNU C Library. Its master source is NOT part of @@ -387,7 +387,10 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) optarg = NULL; if (optind == 0) - optstring = _getopt_initialize (optstring); + { + optstring = _getopt_initialize (optstring); + optind = 1; /* Don't scan ARGV[0], the program name. */ + } if (nextchar == NULL || *nextchar == '\0') { diff --git a/posix/unistd.h b/posix/unistd.h index f0254b44bc..4468541e3a 100644 --- a/posix/unistd.h +++ b/posix/unistd.h @@ -125,6 +125,14 @@ extern int euidaccess __P ((__const char *__name, int __type)); #define SEEK_END 2 /* Seek from end of file. */ #endif +#if defined (__USE_BSD) && !defined (L_SET) +/* Old BSD names for the same constants; just for compatibility. */ +#define L_SET SEEK_SET +#define L_INCR SEEK_CUR +#define L_XTND SEEK_END +#endif + + /* Move FD's file position to OFFSET bytes from the beginning of the file (if WHENCE is SEEK_SET), the current position (if WHENCE is SEEK_CUR), @@ -417,6 +425,12 @@ extern char *ttyname __P ((int __fd)); extern int __isatty __P ((int __fd)); extern int isatty __P ((int __fd)); +#ifdef __USE_BSD +/* Return the index into the active-logins file (utmp) for + the terminal FD is open on. */ +extern int ttyslot ((int __fd)); +#endif + /* Make a link to FROM named TO. */ extern int __link __P ((__const char *__from, __const char *__to)); diff --git a/setjmp/Makefile b/setjmp/Makefile index 4a7e4aa85a..4773b617e1 100644 --- a/setjmp/Makefile +++ b/setjmp/Makefile @@ -24,7 +24,7 @@ subdir := setjmp headers := setjmp.h jmp_buf.h routines := setjmp sigjmp bsd-setjmp bsd-_setjmp \ - longjmp __longjmp + longjmp __longjmp jmp-unwind tests := tst-setjmp diff --git a/setjmp/longjmp.c b/setjmp/longjmp.c index d4e4ef7740..c70be7b886 100644 --- a/setjmp/longjmp.c +++ b/setjmp/longjmp.c @@ -21,12 +21,17 @@ Cambridge, MA 02139, USA. */ #include +extern void _longjmp_unwind (jmp_buf env, int val); + /* Set the signal mask to the one specified in ENV, and jump to the position specified in ENV, causing the setjmp call there to return VAL, or 1 if VAL is 0. */ void longjmp (sigjmp_buf env, int val) { + /* Perform any cleanups needed by the frames being unwound. */ + _longjmp_unwind (env, val); + if (env[0].__mask_was_saved) /* Restore the saved signal mask. */ (void) __sigprocmask (SIG_SETMASK, &env[0].__saved_mask, diff --git a/sysdeps/generic/pty.c b/sysdeps/generic/pty.c new file mode 100644 index 0000000000..1644aee39b --- /dev/null +++ b/sysdeps/generic/pty.c @@ -0,0 +1,127 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)pty.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +openpty(amaster, aslave, name, termp, winp) + int *amaster, *aslave; + char *name; + struct termios *termp; + struct winsize *winp; +{ + static char line[] = "/dev/ptyXX"; + register const char *cp1, *cp2; + register int master, slave, ttygid; + struct group *gr; + + if ((gr = getgrnam("tty")) != NULL) + ttygid = gr->gr_gid; + else + ttygid = -1; + + for (cp1 = "pqrs"; *cp1; cp1++) { + line[8] = *cp1; + for (cp2 = "0123456789abcdef"; *cp2; cp2++) { + line[9] = *cp2; + if ((master = open(line, O_RDWR, 0)) == -1) { + if (errno == ENOENT) + return (-1); /* out of ptys */ + } else { + line[5] = 't'; + (void) chown(line, getuid(), ttygid); + (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP); + (void) revoke(line); + if ((slave = open(line, O_RDWR, 0)) != -1) { + *amaster = master; + *aslave = slave; + if (name) + strcpy(name, line); + if (termp) + (void) tcsetattr(slave, + TCSAFLUSH, termp); + if (winp) + (void) ioctl(slave, TIOCSWINSZ, + (char *)winp); + return (0); + } + (void) close(master); + line[5] = 'p'; + } + } + } + errno = ENOENT; /* out of ptys */ + return (-1); +} + +forkpty(amaster, name, termp, winp) + int *amaster; + char *name; + struct termios *termp; + struct winsize *winp; +{ + int master, slave, pid; + + if (openpty(&master, &slave, name, termp, winp) == -1) + return (-1); + switch (pid = fork()) { + case -1: + return (-1); + case 0: + /* + * child + */ + (void) close(master); + login_tty(slave); + return (0); + } + /* + * parent + */ + *amaster = master; + (void) close(slave); + return (pid); +} diff --git a/sysdeps/i386/jmp_buf.h b/sysdeps/i386/jmp_buf.h index 7686c4d278..7949883b60 100644 --- a/sysdeps/i386/jmp_buf.h +++ b/sysdeps/i386/jmp_buf.h @@ -7,3 +7,8 @@ typedef struct __ptr_t __sp; __ptr_t __pc; } __jmp_buf[1]; + +/* Test if longjmp to JMPBUF would unwind the frame + containing a local variable at ADDRESS. */ +#define _JMPBUF_UNWINDS(jmpbuf, address) \ + ((__ptr_t) (address) < (jmpbuf)[0].__sp) diff --git a/sysdeps/mach/hurd/jmp-unwind.c b/sysdeps/mach/hurd/jmp-unwind.c new file mode 100644 index 0000000000..f7540f0dde --- /dev/null +++ b/sysdeps/mach/hurd/jmp-unwind.c @@ -0,0 +1,62 @@ +/* _longjmp_unwind -- Clean up stack frames unwound by longjmp. Hurd version. +Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#include +#include +#include +#include + +/* This function is called by `longjmp' (with its arguments) to restore + active resources to a sane state before the frames code using them are + jumped out of. */ + +void +_longjmp_unwind (jmp_buf env, int val) +{ + struct hurd_sigstate *ss = _hurd_self_sigstate (); + struct hurd_userlink *link; + + /* All access to SS->active_resources must take place inside a critical + section where signal handlers cannot run. */ + __spin_lock (&ss->lock); + assert (! ss->critical_section); + ss->critical_section = 1; + __spin_unlock (&ss->lock); + +#ifndef _JMPBUF_UNWINDS + #error "sysdeps/MACHINE/jmp_buf.h fails to define _JMPBUF_UNWINDS" +#endif + + /* Iterate over the current thread's list of active resources. + Process the head portion of the list whose links reside + in stack frames being unwound by this jump. */ + + for (link = ss->active_resources; + link && _JMPBUF_UNWINDS (env[0].__jmpbuf, link); + link = link->thread.next) + /* Remove this link from the resource's users list, + since the frame using the resource is being unwound. + This call returns nonzero if that was the last user. */ + if (_hurd_userlink_unlink (link)) + /* One of the frames being unwound by the longjmp was the last user + of its resource. Call the cleanup function to deallocate it. */ + (*link->cleanup) (link->cleanup_data, env, val); + + _hurd_critical_section_unlock (ss); +} diff --git a/sysdeps/mips/jmp_buf.h b/sysdeps/mips/jmp_buf.h index eed47dce7f..102a0193d8 100644 --- a/sysdeps/mips/jmp_buf.h +++ b/sysdeps/mips/jmp_buf.h @@ -1,5 +1,5 @@ -/* Define the machine-dependent type `jmp_buf'. Mips version. - Copyright (C) 1992, 1993 Free Software Foundation, Inc. +/* Define the machine-dependent type `jmp_buf'. MIPS version. + Copyright (C) 1992, 1993, 1995 Free Software Foundation, Inc. Contributed by Brendan Kehoe (brendan@zen.org). The GNU C Library is free software; you can redistribute it and/or @@ -45,3 +45,9 @@ typedef struct /* Offset to the program counter in `jmp_buf'. */ #define JB_PC 0 #endif + + +/* Test if longjmp to JMPBUF would unwind the frame + containing a local variable at ADDRESS. */ +#define _JMPBUF_UNWINDS(jmpbuf, address) \ + ((__ptr_t) (address) < (jmpbuf)[0].__sp) diff --git a/sysdeps/stub/jmp-unwind.c b/sysdeps/stub/jmp-unwind.c new file mode 100644 index 0000000000..937000fd70 --- /dev/null +++ b/sysdeps/stub/jmp-unwind.c @@ -0,0 +1,29 @@ +/* _longjmp_unwind -- Clean up stack frames unwound by longjmp. Stub version. +Copyright (C) 1995 Free Software Foundation, Inc. +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#include + +void +_longjmp_unwind (jmp_buf env, int val) +{ + + /* This function can perform any cleanups necessary to safely unwind the + stack frames around the current context which ENV unwinds past. */ + +} -- cgit 1.4.1