-/* $NetBSD: util.h,v 1.55 2010/02/25 18:37:12 joerg Exp $ */
+/* $NetBSD: util.h,v 1.63 2012/04/07 16:44:59 christos Exp $ */
/*-
* Copyright (c) 1995
#include <sys/cdefs.h>
#include <sys/ttycom.h>
#include <sys/types.h>
-#include <stdio.h>
#include <pwd.h>
+#include <stdarg.h>
+#include <stdio.h>
#include <termios.h>
#include <utmp.h>
#include <utmpx.h>
+#include <stdint.h>
#include <machine/ansi.h>
#ifdef _BSD_TIME_T_
const char *getbootfile(void);
off_t getlabeloffset(void);
int getlabelsector(void);
+int getlabelusesmbr(void);
int getmaxpartitions(void);
int getrawpartition(void);
+const char *getdiskrawname(char *, size_t, const char *);
+const char *getdiskcookedname(char *, size_t, const char *);
+const char *getfstypename(int);
+const char *getfsspecname(char *, size_t, const char *);
#ifndef __LIBC12_SOURCE__
void login(const struct utmp *) __RENAME(__login50);
void loginx(const struct utmpx *) __RENAME(__loginx50);
char *, size_t) __RENAME(__pw_copyx50);
#endif
void pw_edit(int, const char *);
-void pw_error(const char *, int, int);
+__dead void pw_error(const char *, int, int);
void pw_getconf(char *, size_t, const char *, const char *);
#ifndef __LIBC12_SOURCE__
void pw_getpwconf(char *, size_t, const struct passwd *,
int snprintb(char *, size_t, const char *, uint64_t);
int sockaddr_snprintf(char *, size_t, const char *,
const struct sockaddr *);
+char *strpct(char *, size_t, uintmax_t, uintmax_t, size_t);
+char *strspct(char *, size_t, intmax_t, intmax_t, size_t);
int string_to_flags(char **, unsigned long *, unsigned long *);
int ttyaction(const char *, const char *, const char *);
int ttylock(const char *, int, pid_t *);
struct __sFILE *efopen(const char *, const char *);
int easprintf(char ** __restrict, const char * __restrict, ...)
__printflike(2, 3);
-int evasprintf(char ** __restrict, const char * __restrict,
- _BSD_VA_LIST_)
+int evasprintf(char ** __restrict, const char * __restrict, va_list)
__printflike(2, 0);
__END_DECLS
-# $NetBSD: Makefile,v 1.63 2010/01/27 19:10:31 drochner Exp $
+# $NetBSD: Makefile,v 1.73 2012/04/07 16:44:39 christos Exp $
# @(#)Makefile 8.1 (Berkeley) 6/4/93
USE_SHLIBDIR= yes
.include "${NETBSDSRCDIR}/common/lib/libutil/Makefile.inc"
-WARNS= 4
+WARNS?= 5
LIB= util
CPPFLAGS+=-DLIBC_SCCS -I${.CURDIR}
+LINTFLAGS+=-w
.if !defined(__MINIX)
SRCS+= efun.c getbootfile.c getlabelsector.c getmaxpartitions.c \
- getmntopts.c getrawpartition.c \
+ getfsspecname.c getmntopts.c getrawpartition.c getdiskrawname.c \
disklabel_dkcksum.c disklabel_scan.c \
if_media.c \
login.c loginx.c login_cap.c login_tty.c logout.c logoutx.c \
logwtmp.c logwtmpx.c opendisk.c parsedate.y \
passwd.c pw_scan.c pidfile.c pidlock.c pty.c \
raise_default_signal.c \
- secure_path.c stat_flags.c \
- ttyaction.c ttymsg.c \
+ secure_path.c sockaddr_snprintf.c stat_flags.c \
+ strpct.c ttyaction.c ttymsg.c
-MAN= efun.3 getbootfile.3 getlabelsector.3 getmaxpartitions.3 \
- getmntopts.3 \
- getrawpartition.3 \
+MAN= efun.3 getbootfile.3 getfstypename.3 getlabelsector.3 \
+ getmaxpartitions.3 getmntopts.3 getrawpartition.3 \
+ getdiskrawname.3 getfsspecname.3 \
login.3 login_cap.3 loginx.3 \
disklabel_dkcksum.3 disklabel_scan.3 \
opendisk.3 openpty.3 parsedate.3 pidfile.3 pidlock.3 \
- pw_getconf.3 pw_init.3 pw_lock.3 secure_path.3 \
+ proc_compare.3 pw_getconf.3 pw_init.3 pw_lock.3 secure_path.3 \
raise_default_signal.3 \
- snprintb.3 sockaddr_snprintf.3 stat_flags.3 ttyaction.3 \
+ snprintb.3 sockaddr_snprintf.3 stat_flags.3 strpct.3 ttyaction.3 \
ttymsg.3 util.3
.else
SRCS+= efun.c getbootfile.c \
- getmntopts.c sockaddr_snprintf.c\
+ getfsspecname.c getmntopts.c sockaddr_snprintf.c\
login.c loginx.c login_cap.c login_tty.c logout.c logoutx.c \
logwtmp.c logwtmpx.c opendisk.c \
passwd.c pw_scan.c pidfile.c pidlock.c pty.c \
ttyaction.c \
MAN= efun.3 getbootfile.3 \
- getmntopts.3 \
+ getmntopts.3 getfsspecname.3 \
login.3 login_cap.3 loginx.3 \
opendisk.3 openpty.3 pidfile.3 pidlock.3 \
pw_getconf.3 pw_init.3 pw_lock.3 secure_path.3 \
.if !defined(__MINIX)
MLINKS+=getlabelsector.3 getlabeloffset.3
.endif
+MLINKS+=getlabelsector.3 getlabelusesmbr.3
+MLINKS+=getdiskrawname.3 getdiskcookedname.3
MLINKS+=login.3 logout.3
MLINKS+=login.3 logwtmp.3
MLINKS+=login_cap.3 login_getclass.3
MLINKS+=stat_flags.3 string_to_flags.3
MLINKS+=stat_flags.3 flags_to_string.3
MLINKS+=snprintb.3 snprintb_m.3
+MLINKS+=util.3 libutil.3
+MLINKS+=strpct.3 strspct.3
.include <bsd.lib.mk>
--- /dev/null
+.\" $NetBSD: getdiskrawname.3,v 1.2 2012/04/08 16:06:23 wiz Exp $
+.\"
+.\" Copyright (c) 2012 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Christos Zoulas.
+.\"
+.\" 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+.\"
+.\"
+.Dd March 7, 2012
+.Dt GETDISKRAWNAME 3
+.Os
+.Sh NAME
+.Nm getdiskrawname
+.Nd get the the block/character device name for a disk
+.Sh LIBRARY
+.Lb libutil
+.Sh SYNOPSIS
+.In util.h
+.Ft const char *
+.Fn getdiskrawname "char *buf" "size_t buflen" "const char *name"
+.Ft const char *
+.Fn getdiskcookedname "char *buf" "size_t buflen" "const char *name"
+.Sh DESCRIPTION
+The
+.Fn getdiskrawname
+function converts the
+.Fa name
+argument thar contains a path to a disk block device node to the
+path that contains the corresponding character device node.
+The
+.Fn getdiskcookedname
+function converts the
+.Fa name
+argument thar contains a path to a disk character device node to the
+path that contains the corresponding block device node.
+.Sh RETURN VALUES
+On success the absolute pathname of the underlying device node is returned.
+On failure
+.Dv NULL
+is returned and
+.Va errno
+contains the reason for the error.
+.Sh HISTORY
+The
+.Fn getdiskrawname
+and
+.Fn getdiskcookedname
+functions appeared in
+.Nx 7.0 .
--- /dev/null
+/* $NetBSD: getdiskrawname.c,v 1.1 2012/04/07 16:44:39 christos Exp $ */
+
+/*-
+ * Copyright (c) 2012 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: getdiskrawname.c,v 1.1 2012/04/07 16:44:39 christos Exp $");
+
+#include <sys/stat.h>
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <util.h>
+
+const char *
+getdiskrawname(char *buf, size_t bufsiz, const char *name)
+{
+ const char *dp = strrchr(name, '/');
+ struct stat st;
+
+ if (dp == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ if (stat(name, &st) == -1)
+ return NULL;
+
+ if (!S_ISBLK(st.st_mode)) {
+ errno = EFTYPE;
+ return NULL;
+ }
+
+ (void)snprintf(buf, bufsiz, "%.*s/r%s", (int)(dp - name), name, dp + 1);
+
+ return buf;
+}
+
+const char *
+getdiskcookedname(char *buf, size_t bufsiz, const char *name)
+{
+ const char *dp;
+ struct stat st;
+
+ if ((dp = strrchr(name, '/')) == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+ if (stat(name, &st) == -1)
+ return NULL;
+
+ if (!S_ISCHR(st.st_mode)) {
+ errno = EFTYPE;
+ return NULL;
+ }
+ if (dp[1] != 'r') {
+ errno = EINVAL;
+ return NULL;
+ }
+ (void)snprintf(buf, bufsiz, "%.*s/%s", (int)(dp - name), name, dp + 2);
+
+ return buf;
+}
--- /dev/null
+.\" $NetBSD: getfsspecname.3,v 1.3 2012/04/07 16:22:42 christos Exp $
+.\"
+.\" Copyright (c) 2012 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Christos Zoulas.
+.\"
+.\" 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+.\"
+.\"
+.Dd March 6, 2012
+.Dt GETFSSPECNAME 3
+.Os
+.Sh NAME
+.Nm getfsspecname
+.Nd get the underlying wedge name from a label
+.Sh LIBRARY
+.Lb libutil
+.Sh SYNOPSIS
+.In util.h
+.Ft const char *
+.Fn getfsspecname "char *buf" "size_t buflen" "const char *spec"
+.Sh DESCRIPTION
+The
+.Fn getfsspecname
+function translates an
+.Ft fs_spec
+field in the
+.Fa spec
+argument of the form
+.Dq NAME=wedgename
+to the underlying
+.Xr dk 4
+device node, and places the resulting pathname in
+.Fa buf
+up to len
+.Fa bufsiz .
+.Pp
+If the
+.Fa spec
+argument is not of the form
+.Dq NAME=wedgename ,
+.Fa spec
+is copied
+to
+.Fa buf
+and returned.
+.Sh RETURN VALUES
+On success the absolute pathname of the underlying wedge device is returned,
+or the original
+.Fa spec
+argument.
+On failure
+.Dv NULL
+is return and
+.Fa buf
+contains the reason for the error.
+.Sh SEE ALSO
+.Xr fstab 5
+.Sh HISTORY
+The
+.Fn getfsspecname
+function appeared in
+.Nx 7.0 .
--- /dev/null
+/* $NetBSD: getfsspecname.c,v 1.3 2012/04/08 20:56:12 christos Exp $ */
+
+/*-
+ * Copyright (c) 2012 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: getfsspecname.c,v 1.3 2012/04/08 20:56:12 christos Exp $");
+
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <sys/disk.h>
+
+#include <stdio.h>
+#include <vis.h>
+#include <string.h>
+#include <fstab.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <util.h>
+
+#define COMPAT_DKWEDGE /* To be removed */
+
+const char *
+getfsspecname(char *buf, size_t bufsiz, const char *name)
+{
+ static const int mib[] = { CTL_HW, HW_DISKNAMES };
+ static const unsigned int miblen = __arraycount(mib);
+ char *drives, *dk;
+ size_t len;
+ int fd, savee;
+ char *vname;
+
+ drives = NULL;
+ vname = NULL;
+ if (strncasecmp(name, "NAME=", 5) != 0) {
+#ifdef COMPAT_DKWEDGE
+ /*
+ * We try to open the disk name, and if we fail with EBUSY
+ * we use the name as the label to find the wedge.
+ */
+ char rbuf[MAXPATHLEN];
+ if (name[0] == '/') {
+ if (getdiskrawname(rbuf, sizeof(rbuf), name) != NULL) {
+ if ((fd = open(rbuf, O_RDONLY)) == -1) {
+ if (errno == EBUSY) {
+ name = strrchr(name, '/') + 1;
+ goto search;
+ }
+ } else
+ close(fd);
+ }
+ }
+#endif
+ strlcpy(buf, name, bufsiz);
+ return buf;
+ } else
+ name += 5;
+
+#ifdef COMPAT_DKWEDGE
+search:
+#endif
+ vname = malloc(strlen(name) * 4 + 1);
+ if (vname == NULL) {
+ savee = errno;
+ strlcpy(buf, "malloc failed", bufsiz);
+ goto out;
+ }
+
+ strunvis(vname, name);
+
+ if (sysctl(mib, miblen, NULL, &len, NULL, 0) == -1) {
+ savee = errno;
+ strlcpy(buf, "sysctl hw.disknames failed", bufsiz);
+ goto out;
+ }
+
+ drives = malloc(len);
+ if (drives == NULL) {
+ savee = errno;
+ strlcpy(buf, "malloc failed", bufsiz);
+ goto out;
+ }
+ if (sysctl(mib, miblen, drives, &len, NULL, 0) == -1) {
+ savee = errno;
+ strlcpy(buf, "sysctl hw.disknames failed", bufsiz);
+ goto out;
+ }
+
+ for (dk = strtok(drives, " "); dk != NULL; dk = strtok(NULL, " ")) {
+ struct dkwedge_info dkw;
+ if (strncmp(dk, "dk", 2) != 0)
+ continue;
+ fd = opendisk(dk, O_RDONLY, buf, bufsiz, 0);
+ if (fd == -1)
+ continue;
+ if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == -1) {
+ savee = errno;
+ snprintf(buf, bufsiz, "%s: getwedgeinfo", dk);
+ (void)close(fd);
+ goto out;
+ }
+ (void)close(fd);
+ if (strcmp(vname, (char *)dkw.dkw_wname) == 0) {
+ char *p = strstr(buf, "/rdk");
+ if (p++ == NULL)
+ return buf;
+ strcpy(p, p + 1);
+ free(drives);
+ free(vname);
+ return buf;
+ }
+ }
+ savee = ESRCH;
+ snprintf(buf, bufsiz, "no match for `%s'", vname);
+out:
+ free(drives);
+ free(vname);
+ errno = savee;
+ return NULL;
+}
--- /dev/null
+.\" $NetBSD: getfstypename.3,v 1.3 2011/11/18 04:15:41 christos Exp $
+.\"
+.\" Copyright (c) 2011 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Christos Zoulas.
+.\"
+.\" 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+.\"
+.Dd November 13, 2011
+.Dt GETFSTYPENAME 3
+.Os
+.Sh NAME
+.Nm getfstypename
+.Nd convert a partition file system type integer to a wedge partition type name
+.Sh LIBRARY
+.Lb libutil
+.Sh SYNOPSIS
+.In util.h
+.Ft const char *
+.Fn getfstypename "int fstype"
+.Sh DESCRIPTION
+.Fn getfstypename
+returns the disk wedge partitition type name corresponding to the
+.Ar fstype
+argument as specified in
+.In sys/disk.h
+or
+.Dv DKW_PTYPE_UNKNOWN
+if none is found.
+.Sh HISTORY
+The
+.Fn getfstypename
+function call appeared in
+.Nx 6.0 .
-.\" $NetBSD: getlabelsector.3,v 1.5 2009/06/24 22:31:58 zafer Exp $
+.\" $NetBSD: getlabelsector.3,v 1.8 2011/09/14 11:43:29 njoly Exp $
.\"
.\"
.\" Copyright 2002 Wasabi Systems, Inc.
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
-.Dd December 11, 2002
+.Dd August 25, 2011
.Dt GETLABELSECTOR 3
.Os
.Sh NAME
.Nm getlabelsector ,
-.Nm getlabeloffset
-.Nd get the sector number and offset of the disklabel
+.Nm getlabeloffset ,
+.Nm getlabelusesmbr
+.Nd get disklabel location informations
.Sh LIBRARY
.Lb libutil
.Sh SYNOPSIS
.Fn getlabelsector void
.Ft off_t
.Fn getlabeloffset void
+.Ft int
+.Fn getlabelusesmbr void
.Sh DESCRIPTION
The
.Fn getlabelsector
.Dv LABELOFFSET
definitions previously used to derive the location of the
.Xr disklabel 5 .
+.Pp
+The
+.Fn getlabelusesmbr
+returns 1 if the disklabel is located inside a MBR partition, 0 if it's stored
+relative to the start of the disk, or \-1 on error.
.Sh SEE ALSO
.Xr sysctl 3 ,
.Xr disklabel 5
-/* $NetBSD: getlabelsector.c,v 1.3 2005/09/17 01:51:21 elad Exp $ */
+/* $NetBSD: getlabelsector.c,v 1.5 2011/09/04 12:34:49 jmcneill Exp $ */
/*
* Copyright 2002 Wasabi Systems, Inc.
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: getlabelsector.c,v 1.3 2005/09/17 01:51:21 elad Exp $");
+__RCSID("$NetBSD: getlabelsector.c,v 1.5 2011/09/04 12:34:49 jmcneill Exp $");
#endif
#include <sys/param.h>
return ((off_t)offset);
}
+
+int
+getlabelusesmbr(void)
+{
+ int use;
+ size_t uselen;
+
+ uselen = sizeof(use);
+ if (sysctlbyname("kern.labelusesmbr", &use, &uselen,
+ NULL, (size_t)0) < 0)
+ return (-1);
+
+ return use;
+}
-/* $NetBSD: getrawpartition.c,v 1.6 2008/04/28 20:23:03 martin Exp $ */
+/* $NetBSD: getrawpartition.c,v 1.7 2012/06/25 22:32:47 abs Exp $ */
/*-
* Copyright (c) 1996 The NetBSD Foundation, Inc.
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: getrawpartition.c,v 1.6 2008/04/28 20:23:03 martin Exp $");
+__RCSID("$NetBSD: getrawpartition.c,v 1.7 2012/06/25 22:32:47 abs Exp $");
#endif
#include <sys/param.h>
#include <util.h>
int
-getrawpartition()
+getrawpartition(void)
{
int rawpart, mib[2];
size_t varlen;
-/* $NetBSD: login_cap.c,v 1.29 2007/12/04 22:09:02 mjf Exp $ */
+/* $NetBSD: login_cap.c,v 1.30 2012/04/07 16:16:34 christos Exp $ */
/*-
* Copyright (c) 1995,1997 Berkeley Software Design, Inc. All rights reserved.
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: login_cap.c,v 1.29 2007/12/04 22:09:02 mjf Exp $");
+__RCSID("$NetBSD: login_cap.c,v 1.30 2012/04/07 16:16:34 christos Exp $");
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
return (-1);
}
-#define RCUR r.rlim_cur
-#define RMAX r.rlim_max
+#define RCUR ((quad_t)r.rlim_cur)
+#define RMAX ((quad_t)r.rlim_max)
switch (type) {
case R_CTIME:
- RCUR = login_getcaptime(lc, name, RCUR, RCUR);
- RMAX = login_getcaptime(lc, name, RMAX, RMAX);
+ r.rlim_cur = login_getcaptime(lc, name, RCUR, RCUR);
+ r.rlim_max = login_getcaptime(lc, name, RMAX, RMAX);
rl.rlim_cur = login_getcaptime(lc, name_cur, RCUR, RCUR);
rl.rlim_max = login_getcaptime(lc, name_max, RMAX, RMAX);
break;
case R_CSIZE:
- RCUR = login_getcapsize(lc, name, RCUR, RCUR);
- RMAX = login_getcapsize(lc, name, RMAX, RMAX);
+ r.rlim_cur = login_getcapsize(lc, name, RCUR, RCUR);
+ r.rlim_max = login_getcapsize(lc, name, RMAX, RMAX);
rl.rlim_cur = login_getcapsize(lc, name_cur, RCUR, RCUR);
rl.rlim_max = login_getcapsize(lc, name_max, RMAX, RMAX);
break;
case R_CNUMB:
- RCUR = login_getcapnum(lc, name, RCUR, RCUR);
- RMAX = login_getcapnum(lc, name, RMAX, RMAX);
+ r.rlim_cur = login_getcapnum(lc, name, RCUR, RCUR);
+ r.rlim_max = login_getcapnum(lc, name, RMAX, RMAX);
rl.rlim_cur = login_getcapnum(lc, name_cur, RCUR, RCUR);
rl.rlim_max = login_getcapnum(lc, name_max, RMAX, RMAX);
break;
/* allocate ptr array and string */
count = i;
- res = malloc(count * sizeof(char *) + strlen(str) + 1);
+ res = malloc(count * sizeof(*res) + strlen(str) + 1);
if (!res)
return -1;
if (!lc)
flc = lc = login_getclass(pwd ? pwd->pw_class : NULL);
-
+#ifdef __minix
#define LOGIN_SETLOGIN 0
+#endif
+
/*
* Without the pwd entry being passed we cannot set either
* the group or the login. We could complain about it.
-/* $NetBSD: login_tty.c,v 1.12 2008/02/09 05:07:26 dholland Exp $ */
+/* $NetBSD: login_tty.c,v 1.13 2011/09/16 16:13:16 plunky Exp $ */
/*-
* Copyright (c) 1990, 1993
#if 0
static char sccsid[] = "@(#)login_tty.c 8.1 (Berkeley) 6/4/93";
#else
-__RCSID("$NetBSD: login_tty.c,v 1.12 2008/02/09 05:07:26 dholland Exp $");
+__RCSID("$NetBSD: login_tty.c,v 1.13 2011/09/16 16:13:16 plunky Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
(void) setsid();
#ifdef TIOCSCTTY
- if (ioctl(fd, TIOCSCTTY, (char *)NULL) == -1)
+ if (ioctl(fd, TIOCSCTTY, NULL) == -1)
return (-1);
#endif
(void) dup2(fd, STDIN_FILENO);
-.\" $NetBSD: openpty.3,v 1.14 2008/11/28 07:17:17 dholland Exp $
+.\" $NetBSD: openpty.3,v 1.17 2012/07/27 21:33:46 christos Exp $
.\"
.\" Copyright (c) 1995
.\" The Regents of the University of California. All rights reserved.
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd November 28, 2008
+.Dd July 27, 2012
.Dt OPENPTY 3
.Os
.Sh NAME
.Fa name
is non-null, the filename of the slave is returned in
.Fa name .
+The length of
+.Fa name
+is limited to
+.Dv PATH_MAX
+as any other regular path name, so a buffer of this size should be used.
+.\" .Dv 16
+.\" characters in the current
+.\" .Xr ptm 4
+.\" device driver (including the terminating
+.\" .Dv NUL )
+.\" which limits the maximum to
+.\" .Dv 100,000
+.\" ptys.
If
.Fa termp
is non-null, the terminal parameters of the slave will be set to the
-.\" $NetBSD: parsedate.3,v 1.10 2010/12/22 09:12:28 wiz Exp $
+.\" $NetBSD: parsedate.3,v 1.11 2012/04/06 11:36:04 wiz Exp $
.\"
.\" Copyright (c) 2006 The NetBSD Foundation, Inc.
.\" All rights reserved.
.Fn parsedate
function parses a datetime from
.Ar datestr
-described in english relative to an optional
+described in English relative to an optional
.Ar time
point and an optional timezone offset in seconds specified in
.Ar tzoff .
10,
.Dv eleventh or eleven =
11,
-.Dv twelfth or twoelve =
+.Dv twelfth or twelve =
12.
.Pp
The following words are recognized in English only:
}
-static time_t
-ToSeconds(
- time_t Hours,
- time_t Minutes,
- time_t Seconds,
- MERIDIAN Meridian
-)
-{
- if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59)
- return -1;
- switch (Meridian) {
- case MER24:
- if (Hours < 0 || Hours > 23)
- return -1;
- return (Hours * 60L + Minutes) * 60L + Seconds;
- case MERam:
- if (Hours < 1 || Hours > 12)
- return -1;
- if (Hours == 12)
- Hours = 0;
- return (Hours * 60L + Minutes) * 60L + Seconds;
- case MERpm:
- if (Hours < 1 || Hours > 12)
- return -1;
- if (Hours == 12)
- Hours = 0;
- return ((Hours + 12) * 60L + Minutes) * 60L + Seconds;
- default:
- abort ();
- }
- /* NOTREACHED */
-}
-
-static int
-isLeap(int year)
-{
- return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
-}
-
-
/* Year is either
* A negative number, which means to use its absolute value (why?)
* A number from 0 to 99, which means a year from 1900 to 1999, or
* The actual year (>=100). */
static time_t
Convert(
- time_t Month,
- time_t Day,
- time_t Year,
- time_t Hours,
- time_t Minutes,
- time_t Seconds,
- time_t Timezone,
- MERIDIAN Meridian,
- DSTMODE DSTmode
+ time_t Month, /* month of year [1-12] */
+ time_t Day, /* day of month [1-31] */
+ time_t Year, /* year; see above comment */
+ time_t Hours, /* Hour of day [0-24] */
+ time_t Minutes, /* Minute of hour [0-59] */
+ time_t Seconds, /* Second of minute [0-60] */
+ time_t Timezone, /* Timezone as seconds west of UTC */
+ MERIDIAN Meridian, /* Hours are am/pm/24 hour clock */
+ DSTMODE DSTmode /* DST on/off/maybe */
)
{
- static int DaysInMonth[12] = {
- 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
- };
- time_t tod;
- time_t Julian, oJulian;
- int i;
+ struct tm tm;
/* XXX Y2K */
if (Year < 0)
Year += 2000;
else if (Year < 100)
Year += 1900;
- DaysInMonth[1] = isLeap(Year) ? 29 : 28;
- if (Year < EPOCH || Month < 1 || Month > 12
- /* Lint fluff: "conversion from long may lose accuracy" */
- || Day < 1 || Day > DaysInMonth[(int)--Month])
- /* FIXME:
- * It would be nice to set a global error string here.
- * "February 30 is not a valid date" is much more informative than
- * "Can't parse date/time: 100 months" when the user input was
- * "100 months" and addition resolved that to February 30, for
- * example. See rcs2-7 in src/sanity.sh for more. */
- return -1;
-
- for (Julian = Day - 1, i = 0; i < Month; i++)
- Julian += DaysInMonth[i];
- oJulian = Julian;
- for (i = EPOCH; i < Year; i++) {
- Julian += 365 + isLeap(i);
- if (oJulian > Julian)
- return -1;
- oJulian = Julian;
+ tm.tm_sec = Seconds;
+ tm.tm_min = Minutes;
+ tm.tm_hour = Hours + (Meridian == MERpm ? 12 : 0);
+ tm.tm_mday = Day;
+ tm.tm_mon = Month - 1;
+ tm.tm_year = Year - 1900;
+ switch (DSTmode) {
+ case DSTon: tm.tm_isdst = 1; break;
+ case DSToff: tm.tm_isdst = 0; break;
+ default: tm.tm_isdst = -1; break;
}
+ tm.tm_gmtoff = -Timezone;
- Julian *= SECSPERDAY;
- if (oJulian > Julian)
- return -1;
- oJulian = Julian;
- Julian += Timezone * 60L;
- if (Timezone > 0 && oJulian > Julian)
- return -1;
- oJulian = Julian;
-
- if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0)
- return -1;
-
- Julian += tod;
- if (oJulian > Julian)
- return -1;
-
- if (DSTmode == DSTon || (DSTmode == DSTmaybe)) {
- struct tm *tm;
- if ((tm = localtime(&Julian)) == NULL)
- return -1;
- if (tm->tm_isdst)
- Julian -= 60 * 60;
- }
- return Julian;
+ return mktime(&tm);
}
Start = Convert(param.yyMonth, param.yyDay, param.yyYear, param.yyHour,
param.yyMinutes, param.yySeconds, param.yyTimezone,
param.yyMeridian, param.yyDSTmode);
- if (Start < 0)
+ if (Start == -1)
return -1;
}
else {
-/* $NetBSD: passwd.c,v 1.50 2010/08/18 08:32:02 christos Exp $ */
+/* $NetBSD: passwd.c,v 1.52 2012/06/25 22:32:47 abs Exp $ */
/*
* Copyright (c) 1987, 1993, 1994, 1995
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: passwd.c,v 1.50 2010/08/18 08:32:02 christos Exp $");
+__RCSID("$NetBSD: passwd.c,v 1.52 2012/06/25 22:32:47 abs Exp $");
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
}
int
-pw_mkdb(username, secureonly)
- const char *username;
- int secureonly;
+pw_mkdb(const char *username, int secureonly)
{
const char *args[9];
int pstat, i;
}
if (WIFEXITED(pstat)) {
if (WEXITSTATUS(pstat) != 0) {
- warnx("pwd_mkdb exited with static %d",
+ warnx("pwd_mkdb exited with status %d",
WEXITSTATUS(pstat));
return -1;
}
-.\" $NetBSD: pidfile.3,v 1.12 2010/05/05 22:05:31 wiz Exp $
+.\" $NetBSD: pidfile.3,v 1.13 2011/03/29 13:55:37 jmmv Exp $
.\"
.\" Copyright (c) 1999 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
-.Dd May 4, 2010
+.Dd March 23, 2011
.Dt PIDFILE 3
.Os
.Sh NAME
.Sh SYNOPSIS
.In util.h
.Ft int
-.Fn pidfile "const char *basename"
+.Fn pidfile "const char *path"
.Sh DESCRIPTION
.Fn pidfile
-writes a file containing the process ID of the program to the
+creates a file containing the process ID of the caller program.
+The pid file can be used as a quick reference if
+the process needs to be sent a signal.
+When the program exits, the pid file is removed automatically, unless
+the program receives a fatal signal.
+.Pp
+If
+.Ar path
+is
+.Dv NULL
+or a plain basename (a name containing no directory components), the pid file
+is created in the
.Pa /var/run
directory.
The file name has the form
.Pa /var/run/basename.pid .
-If the
-.Ar basename
-argument is
+The basename part is either the value of
+.Ar path
+if it was not
.Dv NULL ,
-.Fn pidfile
-will determine the program name and use that instead.
+or the program name as returned by
+.Xr getprogname 3
+otherwise.
.Pp
-The pid file can be used as a quick reference if
-the process needs to be sent a signal.
-When the program exits, the pid file will be removed automatically, unless
-the program receives a fatal signal.
+If
+.Ar path
+is an absolute or relative path (i.e. it contains the
+.Sq /
+character),
+the pid file is created in the provided location.
.Pp
Note that only the first invocation of
.Fn pidfile
causes a pid file to be written; subsequent invocations have no effect
unless a new
-.Ar basename
+.Ar path
is supplied.
If called with a new
-.Ar basename ,
+.Ar path ,
.Fn pidfile
will remove the old pid file and write the new one.
.Sh RETURN VALUES
.Fn pidfile
function call appeared in
.Nx 1.5 .
+Support for creating pid files in any arbitrary path was added in
+.Nx 6.0 .
.Sh BUGS
.Fn pidfile
uses
.Xr atexit 3
-to ensure the pidfile is unlinked at program exit.
+to ensure the pid file is unlinked at program exit.
However, programs that use the
.Xr _exit 2
function (for example, in signal handlers)
-/* $NetBSD: pidfile.c,v 1.8 2008/04/28 20:23:03 martin Exp $ */
+/* $NetBSD: pidfile.c,v 1.9 2011/03/29 13:55:37 jmmv Exp $ */
/*-
* Copyright (c) 1999 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
- * by Jason R. Thorpe and Matthias Scheler.
+ * by Jason R. Thorpe, Matthias Scheler and Julio Merino.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: pidfile.c,v 1.8 2008/04/28 20:23:03 martin Exp $");
+__RCSID("$NetBSD: pidfile.c,v 1.9 2011/03/29 13:55:37 jmmv Exp $");
#endif
#include <sys/param.h>
+
#include <paths.h>
+#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <util.h>
-static int pidfile_atexit_done;
static pid_t pidfile_pid;
-static char *pidfile_basename;
static char *pidfile_path;
-static void pidfile_cleanup(void);
+/* Deletes an existent pidfile iff it was created by this process. */
+static void
+pidfile_cleanup(void)
+{
-int
-pidfile(const char *basename)
+ if ((pidfile_path != NULL) && (pidfile_pid == getpid()))
+ (void) unlink(pidfile_path);
+}
+
+/* Registers an atexit(3) handler to delete the pidfile we have generated.
+ * We only register the handler when we create a pidfile, so we can assume
+ * that the pidfile exists.
+ *
+ * Returns 0 on success or -1 if the handler could not be registered. */
+static int
+register_atexit_handler(void)
{
- FILE *f;
+ static bool done = false;
- /*
- * Register handler which will remove the pidfile later.
- */
- if (!pidfile_atexit_done) {
+ if (!done) {
if (atexit(pidfile_cleanup) < 0)
return -1;
- pidfile_atexit_done = 1;
+ done = true;
}
- if (basename == NULL)
- basename = getprogname();
+ return 0;
+}
- /*
- * If pidfile has already been created for the supplied basename
- * we don't need to create a pidfile again.
- */
+/* Given a new pidfile name in 'path', deletes any previously-created pidfile
+ * if the previous file differs to the new one.
+ *
+ * If a previous file is deleted, returns 1, which means that a new pidfile
+ * must be created. Otherwise, this returns 0, which means that the existing
+ * file does not need to be touched. */
+static int
+cleanup_old_pidfile(const char* path)
+{
if (pidfile_path != NULL) {
- if (strcmp(pidfile_basename, basename) == 0)
+ if (strcmp(pidfile_path, path) != 0) {
+ pidfile_cleanup();
+
+ free(pidfile_path);
+ pidfile_path = NULL;
+
+ return 1;
+ } else
return 0;
- /*
- * Remove existing pidfile if it was created by this process.
- */
- pidfile_cleanup();
+ } else
+ return 1;
+}
- free(pidfile_path);
- pidfile_path = NULL;
- free(pidfile_basename);
- pidfile_basename = NULL;
- }
+/* Constructs a name for a pidfile in the default location (/var/run). If
+ * 'basename' is NULL, uses the name of the current program for the name of
+ * the pidfile.
+ *
+ * Returns a pointer to a dynamically-allocatd string containing the absolute
+ * path to the pidfile; NULL on failure. */
+static char *
+generate_varrun_path(const char *basename)
+{
+ char *path;
- pidfile_pid = getpid();
+ if (basename == NULL)
+ basename = getprogname();
- pidfile_basename = strdup(basename);
- if (pidfile_basename == NULL)
+ /* _PATH_VARRUN includes trailing / */
+ (void) asprintf(&path, "%s%s.pid", _PATH_VARRUN, basename);
+ return path;
+}
+
+/* Creates a pidfile with the provided name. The new pidfile is "registered"
+ * in the global variables pidfile_path and pidfile_pid so that any further
+ * call to pidfile(3) can check if we are recreating the same file or a new
+ * one.
+ *
+ * Returns 0 on success or -1 if there is any error. */
+static int
+create_pidfile(const char* path)
+{
+ FILE *f;
+
+ if (register_atexit_handler() == -1)
return -1;
- /* _PATH_VARRUN includes trailing / */
- (void) asprintf(&pidfile_path, "%s%s.pid", _PATH_VARRUN, basename);
- if (pidfile_path == NULL) {
- free(pidfile_basename);
- pidfile_basename = NULL;
+ if (cleanup_old_pidfile(path) == 0)
+ return 0;
+
+ pidfile_path = strdup(path);
+ if (pidfile_path == NULL)
return -1;
- }
- if ((f = fopen(pidfile_path, "w")) == NULL) {
+ if ((f = fopen(path, "w")) == NULL) {
free(pidfile_path);
pidfile_path = NULL;
- free(pidfile_basename);
- pidfile_basename = NULL;
return -1;
}
+ pidfile_pid = getpid();
+
(void) fprintf(f, "%d\n", pidfile_pid);
(void) fclose(f);
+
return 0;
}
-static void
-pidfile_cleanup(void)
+int
+pidfile(const char *path)
{
- /* Only remove the pidfile if it was created by this process. */
- if ((pidfile_path != NULL) && (pidfile_pid == getpid()))
- (void) unlink(pidfile_path);
+
+ if (path == NULL || strchr(path, '/') == NULL) {
+ char *default_path;
+
+ if ((default_path = generate_varrun_path(path)) == NULL)
+ return -1;
+
+ if (create_pidfile(default_path) == -1) {
+ free(default_path);
+ return -1;
+ }
+
+ free(default_path);
+ return 0;
+ } else
+ return create_pidfile(path);
}
-/* $NetBSD: pidlock.c,v 1.15 2009/01/18 12:13:04 lukem Exp $ */
+/* $NetBSD: pidlock.c,v 1.16 2012/04/07 16:17:17 christos Exp $ */
/*
* Copyright 1996, 1997 by Curt Sampson <cjs@NetBSD.org>.
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: pidlock.c,v 1.15 2009/01/18 12:13:04 lukem Exp $");
+__RCSID("$NetBSD: pidlock.c,v 1.16 2012/04/07 16:17:17 christos Exp $");
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
char hostname[MAXHOSTNAMELEN + 1];
pid_t pid2 = -1;
struct stat st;
- int err;
- int f = -1;
+ ssize_t n;
+ int f = -1, savee;
char s[256];
char *p;
size_t len;
goto out;
/* Find out who has this lockfile. */
if ((f = open(lockfile, O_RDONLY, 0)) != -1) {
- if ((err = read(f, s, (size_t)11)) == -1)
+ if ((n = read(f, s, (size_t)11)) == -1)
goto out;
- if (err == 0) {
+ if (n == 0) {
errno = EINVAL;
goto out;
}
pid2 = atoi(s);
- if ((err = read(f, s, sizeof(s) - 2)) == -1)
+ if ((n = read(f, s, sizeof(s) - 2)) == -1)
goto out;
- if (err == 0)
+ if (n == 0)
*s = '\0';
s[sizeof(s) - 1] = '\0';
if ((p = strchr(s, '\n')) != NULL)
errno = 0;
return 0;
out:
- err = errno;
+ savee = errno;
if (f != -1)
(void)close(f);
(void)unlink(tempfile);
- errno = err;
+ errno = savee;
return -1;
}
--- /dev/null
+.\" $NetBSD: proc_compare.3,v 1.2 2011/10/21 12:58:53 wiz Exp $
+.\"
+.\" Copyright (c) 2011 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Christos Zoulas.
+.\"
+.\" 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 NetBSD
+.\" Foundation, Inc. and its contributors.
+.\" 4. Neither the name of The NetBSD Foundation 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+.\"
+.\"
+.Dd October 20, 2011
+.Dt PROC_COMPARE 3
+.Os
+.Sh NAME
+.Nm proc_compare
+.Nd compare two processes' interactivity
+.Sh LIBRARY
+.Lb libutil
+.Sh SYNOPSIS
+.In sys/sysctl.h
+.Ft int
+.Fn "proc_compare" "const struct kinfo_proc2 *p1" "const struct kinfo_lwp *l1" \
+"const struct kinfo_proc2 *p2" "const struct kinfo_lwp *l2"
+.Sh DESCRIPTION
+The
+.Fn proc_compare
+function compares two processes that are on the same terminal for their
+interactivity.
+This means that the process returned is the one that has a better chance
+being the active foreground process on that tty.
+This algorithm is used in the kernel for
+.Dv SIGINFO
+reporting and in userland by
+.Xr w 1 .
+.Pp
+The algorithm used is as follows:
+.Bl -bullet -compact -offset indent
+.It
+If one of them is runnable, it is preferred.
+.It
+If both are runnable, the one with the largest CPU percent is preferred.
+.It
+In a CPU percent tie, the one started more recently wins.
+.It
+If none are runnable, and one of them is a zombie, the non-zombie is preferred
+.It
+If both are zombies, the one started more recently wins.
+.It
+If neither is a zombie, the one with the smaller sleep time wins.
+.It
+In a tie, and one is sleeping in non-interruptible sleep, prefer that one.
+.It
+If both are in the same state, the one started more recently is preferred.
+.El
+In all cases where the most recently started wins, if there was no winner,
+the one with the largest PID wins.
+.Sh RETURN VALUES
+The
+.Fn proc_compare
+function returns
+.Dv 0
+if
+.Fa p1
+is to be preferred
+and
+.Dv 1
+if
+.Fa p2
+is to be preferred.
+.Sh SEE ALSO
+.Xr w 1
+.Sh HISTORY
+The
+.Fn proc_compare
+was extracted from
+.Pa src/sys/kern/tty.c
+and
+.Pa src/usr.bin/w/proc_compare.c
+and merged in
+.Nx 6.0 .
/* term may be NULL */
/* winp may be NULL */
-#ifndef __minix
if ((master = open("/dev/ptm", O_RDWR)) != -1) {
struct ptmget pt;
if (ioctl(master, TIOCPTMGET, &pt) != -1) {
}
(void)close(master);
}
-#endif
(void)getgrnam_r("tty", &grs, grbuf, sizeof(grbuf), &grp);
if (grp != NULL) {
for (cp = cp2 = TTY_OLD_SUFFIX TTY_NEW_SUFFIX; *cp2; cp2++) {
line[5] = 'p';
line[9] = *cp2;
- if ((master = open(line, O_RDWR | O_NOCTTY, 0)) == -1) {
+ if ((master = open(line, O_RDWR, 0)) == -1) {
if (errno != ENOENT)
continue; /* busy */
if ((size_t)(cp2 - cp + 1) < sizeof(TTY_OLD_SUFFIX))
linep = line;
if (chown(line, getuid(), ttygid) == 0 &&
chmod(line, mode) == 0 &&
-#ifndef __minix
revoke(line) == 0 &&
-#endif
- (slave = open(line, O_RDWR | O_NOCTTY, 0)) != -1) {
-#ifndef __minix
+ (slave = open(line, O_RDWR, 0)) != -1) {
gotit:
-#endif
*amaster = master;
*aslave = slave;
if (name)
-.\" $NetBSD: raise_default_signal.3,v 1.2 2008/04/30 13:10:52 martin Exp $
+.\" $NetBSD: raise_default_signal.3,v 1.3 2011/03/27 12:53:16 njoly Exp $
.\"
.\" Copyright (c) 2007 The NetBSD Foundation, Inc.
.\" All rights reserved.
.Bl -enum -offset indent
.It
Block all signals, using
-.Xr sigprocmask 3 .
+.Xr sigprocmask 2 .
.It
Set the signal handler for signal
.Fa sig
+# $NetBSD: shlib_version,v 1.51 2012/04/07 16:44:39 christos Exp $
+# Remember to update distrib/sets/lists/base/shl.* when changing
+#
+# MINIX Keep lower than NETBSD until we are ABI compatible
major=0
minor=0
#include <sys/un.h>
#include <netinet/in.h>
+#ifndef __minix
+#include <netatalk/at.h>
+#include <net/if_dl.h>
+#endif
#include <stdio.h>
#include <string.h>
if (addr == abuf)
name = addr;
+#ifndef __minix
+ if (a && getnameinfo(sa, (socklen_t)sa->sa_len, addr = abuf,
+#else
if (a && getnameinfo(sa, (socklen_t)len, addr = abuf,
+#endif
(unsigned int)sizeof(abuf), NULL, 0,
NI_NUMERICHOST|NI_NUMERICSERV) != 0)
return -1;
ADDS(nbuf);
break;
case 'l':
+#ifndef __minix
+ (void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_len);
+#else
(void)snprintf(nbuf, sizeof(nbuf), "%d", len);
+#endif
ADDS(nbuf);
break;
case 'A':
else if (!a)
ADDNA();
else {
+#ifndef __minix
+ getnameinfo(sa, (socklen_t)sa->sa_len,
+#else
getnameinfo(sa, (socklen_t)len,
+#endif
name = Abuf,
(unsigned int)sizeof(nbuf), NULL, 0, 0);
ADDS(name);
else if (p == -1)
ADDNA();
else {
+#ifndef __minix
+ getnameinfo(sa, (socklen_t)sa->sa_len, NULL, 0,
+#else
getnameinfo(sa, (socklen_t)len, NULL, 0,
+#endif
port = pbuf,
(unsigned int)sizeof(pbuf), 0);
ADDS(port);
-.\" $NetBSD: stat_flags.3,v 1.6 2010/05/04 06:53:35 jruoho Exp $
+.\" $NetBSD: stat_flags.3,v 1.7 2011/08/06 11:28:24 jruoho Exp $
.\"
.\" Copyright (c) 1996 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
-.Dd May 4, 2010
+.Dd August 6, 2011
.Dt STAT_FLAGS 3
.Os
.Sh NAME
.Fa stringp
to point to the first string that it failed to parse.
.Sh SEE ALSO
+.Xr chflags 2 ,
.Xr stat 2
--- /dev/null
+.\" $NetBSD: strpct.3,v 1.4 2012/01/07 18:40:55 christos Exp $
+.\"
+.\" Copyright (c) 2011 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This file was contributed to The NetBSD Foundation by Christos Zoulas.
+.\"
+.\" 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+.\"
+.Dd January 7, 2012
+.Dt STRPCT 3
+.Os
+.Sh NAME
+.Nm strpct ,
+.Nm strspct
+.Nd decimal percent formatters
+.Sh LIBRARY
+.Lb libutil
+.Sh SYNOPSIS
+.In util.h
+.Ft char *
+.Fn strpct "char *buf" "size_t bufsiz" "uintmax_t numerator" "uintmax_t denominator" "size_t precision"
+.Ft char *
+.Fn strspct "char *buf" "size_t bufsiz" "intmax_t numerator" "intmax_t denominator" "size_t precision"
+.Sh DESCRIPTION
+The
+.Fn strpct
+function formats the fraction represented by
+.Fa numerator
+and
+.Fa denominator
+into a percentage representation with given number of digits of
+.Fa precision
+without using floating point arithmetic.
+.Sh RETURN VALUES
+.Fn strpct
+and
+.Fn strspct
+always return a pointer to a NUL-terminated (unless
+.Fa buflen
+is
+.Dv 0 )
+formatted string which
+is placed in
+.Fa buf
+and is up to
+.Fa buflen
+characters.
+If there was an overflow, the formatted string will reflect that precision
+loss.
+.Sh EXAMPLES
+.Bd -literal -offset indent
+strpct(buf, buflen, 1, 16, 3);
+\(rA "6.250"
+strpct(buf, buflen, 1, 2, 0);
+\(rA "50"
+.Ed
+.Sh HISTORY
+.Fn strpct
+was originally implemented in
+.Xr csh 1
+for
+.Nx 1.3 .
+It printed into a static buffer, was not locale aware, handled
+.Ft unsigned long
+numbers, and printed a
+.Dq %
+at the end of the number.
+Other programs such as
+.Xr df 1
+and
+.Xr time 1
+started using it.
+.Fn strpct
+and
+.Fn strspct
+appeared separately in libutil for
+.Nx 6.0 .
+.Sh AUTHORS
+.An Erik E. Fair Aq fair@netbsd.org
--- /dev/null
+/* $NetBSD: strpct.c,v 1.3 2012/01/07 18:40:56 christos Exp $ */
+
+/*-
+ * Copyright (c) 1998 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Erik E. Fair
+ *
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+
+/*
+ * Calculate a percentage without resorting to floating point
+ * and return a pointer to a string
+ *
+ * "digits" is the number of digits past the decimal place you want
+ * (zero being the straight percentage with no decimals)
+ *
+ * Erik E. Fair <fair@clock.org>, May 8, 1997
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: strpct.c,v 1.3 2012/01/07 18:40:56 christos Exp $");
+
+#include <stdint.h>
+#include <locale.h>
+#include <limits.h>
+#include <stdio.h>
+#include <errno.h>
+#include <util.h>
+
+char *
+strspct(char *buf, size_t bufsiz, intmax_t numerator, intmax_t denominator,
+ size_t digits)
+{
+ int sign;
+
+ switch (bufsiz) {
+ case 1:
+ *buf = '\0';
+ /*FALLTHROUGH*/
+ case 0:
+ return buf;
+ default:
+ break;
+ }
+
+ if (denominator < 0) {
+ denominator = -denominator;
+ sign = 1;
+ } else
+ sign = 0;
+
+ if (numerator < 0) {
+ numerator = -numerator;
+ sign++;
+ }
+
+ sign &= 1;
+ (void)strpct(buf + sign, bufsiz - sign, (uintmax_t)numerator,
+ (uintmax_t)denominator, digits);
+ if (sign)
+ *buf = '-';
+ return buf;
+}
+
+char *
+strpct(char *buf, size_t bufsiz, uintmax_t numerator, uintmax_t denominator,
+ size_t digits)
+{
+ uintmax_t factor, result;
+ size_t u;
+
+ factor = 100;
+ for (u = 0; u < digits; u++) {
+ /* watch out for overflow! */
+ if (factor < (UINTMAX_MAX / 10))
+ factor *= 10;
+ else
+ break;
+ }
+
+ /* watch out for overflow! */
+ if (numerator < (UINTMAX_MAX / factor))
+ numerator *= factor;
+ else {
+ /* toss some of the bits of lesser significance */
+ denominator /= factor;
+ }
+
+ if (denominator == 0)
+ denominator = 1;
+
+ result = numerator / denominator;
+
+ if (digits == 0)
+ (void)snprintf(buf, bufsiz, "%ju", result);
+ else {
+ factor /= 100; /* undo initialization */
+
+ (void)snprintf(buf, bufsiz, "%ju%s%0*ju",
+ result / factor, localeconv()->decimal_point, (int)u,
+ result % factor);
+ }
+
+ return buf;
+}
-.\" $NetBSD: util.3,v 1.21 2010/05/05 06:11:14 jruoho Exp $
+.\" $NetBSD: util.3,v 1.24 2011/08/29 12:39:50 jruoho Exp $
.\"
.\" Copyright (c) 2001 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
-.Dd May 5, 2010
+.Dd August 29, 2011
.Dt UTIL 3
.Os
.Sh NAME
-.Nm util
+.Nm util ,
+.Nm libutil
.Nd system utilities library
.Sh LIBRARY
.Lb libutil
.It Xr setusercontext 3 Ta query login.conf database about a user class
.It Xr snprintb 3 Ta bitmask output conversion
.It Xr sockaddr_snprintf 3 Ta socket address formatting function
+.It Xr strpct 3 Ta decimal percent formatter
.It Xr ttyaction 3 Ta ttyaction utility function
.It Xr ttylock 3 Ta locks based on files containing PIDs
.It Xr ttymsg 3 Ta ttymsg utility function