Update setmode module from NetBSD

Merge some interesting changes.
This commit is contained in:
Guillem Jover 2013-05-25 15:31:45 +02:00
parent a4812cdf24
commit 96a2dae352
2 changed files with 126 additions and 61 deletions

View File

@ -1,3 +1,5 @@
.\" $NetBSD: setmode.3,v 1.18.28.1 2009/01/04 17:02:19 christos Exp $
.\"
.\" Copyright (c) 1989, 1991, 1993 .\" Copyright (c) 1989, 1991, 1993
.\" The Regents of the University of California. All rights reserved. .\" The Regents of the University of California. All rights reserved.
.\" .\"
@ -9,7 +11,7 @@
.\" 2. Redistributions in binary form must reproduce the above copyright .\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the .\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution. .\" documentation and/or other materials provided with the distribution.
.\" 4. Neither the name of the University nor the names of its contributors .\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software .\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission. .\" without specific prior written permission.
.\" .\"
@ -28,7 +30,7 @@
.\" @(#)setmode.3 8.2 (Berkeley) 4/28/95 .\" @(#)setmode.3 8.2 (Berkeley) 4/28/95
.\" $FreeBSD: src/lib/libc/gen/setmode.3,v 1.12 2007/01/09 00:27:55 imp Exp $ .\" $FreeBSD: src/lib/libc/gen/setmode.3,v 1.12 2007/01/09 00:27:55 imp Exp $
.\" .\"
.Dd April 28, 1995 .Dd January 4, 2009
.Dt SETMODE 3 .Dt SETMODE 3
.Os .Os
.Sh NAME .Sh NAME
@ -40,34 +42,40 @@
.Lb libbsd .Lb libbsd
.Sh SYNOPSIS .Sh SYNOPSIS
.In bsd/unistd.h .In bsd/unistd.h
.Ft mode_t
.Fn getmode "const void *set" "mode_t mode"
.Ft void * .Ft void *
.Fn setmode "const char *mode_str" .Fn setmode "const char *mode_str"
.Ft mode_t
.Fn getmode "const void *set" "mode_t mode"
.Sh DESCRIPTION .Sh DESCRIPTION
The The
.Fn setmode
function accepts a string representation of a file mode change,
compiles it to binary form, and returns an abstract representation
that may be passed to
.Fn getmode .
The string may be an numeric (octal) or symbolic string of the form
accepted by
.Xr chmod 1 ,
and may represent either an exact mode to set or a change to make to
the existing mode.
.Pp
The
.Fn getmode .Fn getmode
function function
returns a copy of the file permission bits adjusts the file permission bits given by
.Fa mode .Fa mode
as altered by the values pointed to by according to the compiled change representation
.Fa set . .Fa set ,
While only the mode bits are altered, other parts of the file mode and returns the adjusted mode.
may be examined. While only the permission bits are altered, other parts of the file
mode, particularly the type, may be examined.
.Pp .Pp
The Because some of the possible symbolic values are defined relative to
.Fn setmode the file creation mask,
function
takes an absolute (octal) or symbolic value, as described in
.Xr chmod 1 ,
as an argument
and returns a pointer to mode values to be supplied to
.Fn getmode .
Because some of the symbolic values are relative to the file
creation mask,
.Fn setmode .Fn setmode
may call may call
.Xr umask 2 . .Xr umask 2 ,
temporarily changing the mask.
If this occurs, the file creation mask will be restored before If this occurs, the file creation mask will be restored before
.Fn setmode .Fn setmode
returns. returns.
@ -75,13 +83,13 @@ If the calling program changes the value of its file creation mask
after calling after calling
.Fn setmode , .Fn setmode ,
.Fn setmode .Fn setmode
must be called again if must be called again to recompile the mode string if
.Fn getmode .Fn getmode
is to modify future file modes correctly. is to modify future file modes correctly.
.Pp .Pp
If the mode passed to If the mode passed to
.Fn setmode .Fn setmode
is invalid or if memory cannot be allocated for the return value, is invalid,
.Fn setmode .Fn setmode
returns returns
.Dv NULL . .Dv NULL .
@ -94,13 +102,40 @@ and should be returned to the system with
.Fn free .Fn free
when the program is done with it, generally after a call to when the program is done with it, generally after a call to
.Fn getmode . .Fn getmode .
.Sh EXAMPLES
The effects of the shell command
.Ql "chmod a+x myscript.sh"
can be duplicated as follows:
.Bd -literal -offset indent
const char *file = "myscript.sh";
struct stat st;
mode_t newmode;
stat(file, \*[Am]st);
newmode = getmode(setmode("a+x"), st.st_mode);
chmod(file, newmode);
.Ed
.Sh ERRORS .Sh ERRORS
The The
.Fn setmode .Fn setmode
function function
may fail and set errno for any of the errors specified for the library may fail and set
routine .Va errno
.Xr malloc 3 . for any of the errors specified for the library routines
.Xr malloc 3
or
.Xr strtol 3 .
In addition,
.Fn setmode
will fail and set
.Va errno
to:
.Bl -tag -width Er
.It Bq Er EINVAL
The
.Fa mode
argument does not represent a valid mode.
.El
.Sh SEE ALSO .Sh SEE ALSO
.Xr chmod 1 , .Xr chmod 1 ,
.Xr stat 2 , .Xr stat 2 ,
@ -113,3 +148,9 @@ and
.Fn setmode .Fn setmode
functions first appeared in functions first appeared in
.Bx 4.4 . .Bx 4.4 .
.Sh BUGS
The type of
.Fa set
should really be some opaque struct type used only by these functions
rather than
.Ft void * .

View File

@ -1,3 +1,5 @@
/* $NetBSD: setmode.c,v 1.34 2012/06/25 22:32:43 abs Exp $ */
/* /*
* Copyright (c) 1989, 1993, 1994 * Copyright (c) 1989, 1993, 1994
* The Regents of the University of California. All rights reserved. * The Regents of the University of California. All rights reserved.
@ -13,7 +15,7 @@
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 4. Neither the name of the University nor the names of its contributors * 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
@ -30,19 +32,23 @@
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)setmode.c 8.2 (Berkeley) 3/25/94";
#endif /* LIBC_SCCS and not lint */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__FBSDID("$FreeBSD: src/lib/libc/gen/setmode.c,v 1.11 2007/01/09 00:27:55 imp Exp $"); #if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid[] = "@(#)setmode.c 8.2 (Berkeley) 3/25/94";
#else
__RCSID("$NetBSD: setmode.c,v 1.34 2012/06/25 22:32:43 abs Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h>
#include <signal.h> #include <signal.h>
#include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <limits.h>
#include <unistd.h> #include <unistd.h>
#ifdef SETMODE_DEBUG #ifdef SETMODE_DEBUG
@ -64,7 +70,7 @@ typedef struct bitcmd {
#define CMD2_OBITS 0x08 #define CMD2_OBITS 0x08
#define CMD2_UBITS 0x10 #define CMD2_UBITS 0x10
static BITCMD *addcmd(BITCMD *, int, int, int, u_int); static BITCMD *addcmd(BITCMD *, mode_t, mode_t, mode_t, mode_t);
static void compress_mode(BITCMD *); static void compress_mode(BITCMD *);
#ifdef SETMODE_DEBUG #ifdef SETMODE_DEBUG
static void dumpmode(BITCMD *); static void dumpmode(BITCMD *);
@ -144,38 +150,38 @@ common: if (set->cmd2 & CMD2_CLR) {
} }
} }
#define ADDCMD(a, b, c, d) \ #define ADDCMD(a, b, c, d) do { \
if (set >= endset) { \ if (set >= endset) { \
BITCMD *newset; \ BITCMD *newset; \
setlen += SET_LEN_INCR; \ setlen += SET_LEN_INCR; \
newset = realloc(saveset, sizeof(BITCMD) * setlen); \ newset = realloc(saveset, sizeof(BITCMD) * setlen); \
if (!newset) { \ if (newset == NULL) \
if (saveset) \ goto out; \
free(saveset); \
saveset = NULL; \
return (NULL); \
} \
set = newset + (set - saveset); \ set = newset + (set - saveset); \
saveset = newset; \ saveset = newset; \
endset = newset + (setlen - 2); \ endset = newset + (setlen - 2); \
} \ } \
set = addcmd(set, (a), (b), (c), (d)) set = addcmd(set, (mode_t)(a), (mode_t)(b), (mode_t)(c), (d)); \
} while (/*CONSTCOND*/0)
#define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
void * void *
setmode(const char *p) setmode(const char *p)
{ {
int perm, who; int serrno;
char op, *ep; char op, *ep;
BITCMD *set, *saveset, *endset; BITCMD *set, *saveset, *endset;
sigset_t sigset, sigoset; sigset_t signset, sigoset;
mode_t mask; mode_t mask, perm, permXbits, who;
int equalopdone=0, permXbits, setlen; long lval;
long perml; int equalopdone = 0; /* pacify gcc */
int setlen;
if (!*p) if (!*p) {
return (NULL); errno = EINVAL;
return NULL;
}
/* /*
* Get a copy of the mask for the permissions that are mask relative. * Get a copy of the mask for the permissions that are mask relative.
@ -183,8 +189,8 @@ setmode(const char *p)
* the caller is opening files inside a signal handler, protect them * the caller is opening files inside a signal handler, protect them
* as best we can. * as best we can.
*/ */
sigfillset(&sigset); sigfillset(&signset);
(void)sigprocmask(SIG_BLOCK, &sigset, &sigoset); (void)sigprocmask(SIG_BLOCK, &signset, &sigoset);
(void)umask(mask = umask(0)); (void)umask(mask = umask(0));
mask = ~mask; mask = ~mask;
(void)sigprocmask(SIG_SETMASK, &sigoset, NULL); (void)sigprocmask(SIG_SETMASK, &sigoset, NULL);
@ -201,12 +207,19 @@ setmode(const char *p)
* or illegal bits. * or illegal bits.
*/ */
if (isdigit((unsigned char)*p)) { if (isdigit((unsigned char)*p)) {
perml = strtol(p, &ep, 8); errno = 0;
if (*ep || perml < 0 || perml & ~(STANDARD_BITS|S_ISTXT)) { lval = strtol(p, &ep, 8);
free(saveset); if (*ep) {
return (NULL); errno = EINVAL;
goto out;
} }
perm = (mode_t)perml; if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
goto out;
if (lval & ~(STANDARD_BITS|S_ISTXT)) {
errno = EINVAL;
goto out;
}
perm = (mode_t)lval;
ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask); ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
set->cmd = 0; set->cmd = 0;
return (saveset); return (saveset);
@ -238,8 +251,8 @@ setmode(const char *p)
} }
getop: if ((op = *p++) != '+' && op != '-' && op != '=') { getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
free(saveset); errno = EINVAL;
return (NULL); goto out;
} }
if (op == '=') if (op == '=')
equalopdone = 0; equalopdone = 0;
@ -251,13 +264,19 @@ getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
perm |= S_IRUSR|S_IRGRP|S_IROTH; perm |= S_IRUSR|S_IRGRP|S_IROTH;
break; break;
case 's': case 's':
/* If only "other" bits ignore set-id. */ /*
if (!who || who & ~S_IRWXO) * If specific bits where requested and
* only "other" bits ignore set-id.
*/
if (who == 0 || (who & ~S_IRWXO))
perm |= S_ISUID|S_ISGID; perm |= S_ISUID|S_ISGID;
break; break;
case 't': case 't':
/* If only "other" bits ignore sticky. */ /*
if (!who || who & ~S_IRWXO) { * If specific bits where requested and
* only "other" bits ignore set-id.
*/
if (who == 0 || (who & ~S_IRWXO)) {
who |= S_ISTXT; who |= S_ISTXT;
perm |= S_ISTXT; perm |= S_ISTXT;
} }
@ -328,10 +347,15 @@ apply: if (!*p)
dumpmode(saveset); dumpmode(saveset);
#endif #endif
return (saveset); return (saveset);
out:
serrno = errno;
free(saveset);
errno = serrno;
return NULL;
} }
static BITCMD * static BITCMD *
addcmd(BITCMD *set, int op, int who, int oparg, u_int mask) addcmd(BITCMD *set, mode_t op, mode_t who, mode_t oparg, mode_t mask)
{ {
switch (op) { switch (op) {
case '=': case '=':