chown/chgrp: Add SIGINFO handler

PR:		191884
Submitted by:	Dan McGregor <dan.mcgregor at usask.ca>
Reviewed by:	mjg@ (earlier version)
This commit is contained in:
Conrad Meyer 2017-01-08 06:58:42 +00:00
parent a758566cb8
commit bb577bb699
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=311669
3 changed files with 65 additions and 31 deletions

View File

@ -31,7 +31,7 @@
.\" @(#)chgrp.1 8.3 (Berkeley) 3/31/94
.\" $FreeBSD$
.\"
.Dd April 20, 2015
.Dd January 7, 2017
.Dt CHGRP 1
.Os
.Sh NAME
@ -120,6 +120,17 @@ The user invoking
.Nm
must belong to the specified group and be the owner of the file,
or be the super-user.
.Pp
If
.Nm
receives a
.Dv SIGINFO
signal (see the
.Cm status
argument for
.Xr stty 1 ) ,
then the current filename as well as the old and new group names are
displayed.
.Sh FILES
.Bl -tag -width /etc/group -compact
.It Pa /etc/group

View File

@ -28,7 +28,7 @@
.\" @(#)chown.8 8.3 (Berkeley) 3/31/94
.\" $FreeBSD$
.\"
.Dd April 20, 2015
.Dd January 7, 2017
.Dt CHOWN 8
.Os
.Sh NAME
@ -135,6 +135,17 @@ group name.
.Pp
The ownership of a file may only be altered by a super-user for
obvious security reasons.
.Pp
If
.Nm
receives a
.Dv SIGINFO
signal (see the
.Cm status
argument for
.Xr stty 1 ) ,
then the current filename as well as the old and new file owner and group
are displayed.
.Sh EXIT STATUS
.Ex -std
.Sh COMPATIBILITY

View File

@ -52,6 +52,7 @@ __FBSDID("$FreeBSD$");
#include <grp.h>
#include <libgen.h>
#include <pwd.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@ -63,11 +64,20 @@ static void a_uid(const char *);
static void chownerr(const char *);
static uid_t id(const char *, const char *);
static void usage(void);
static void print_info(const FTSENT *, int);
static uid_t uid;
static gid_t gid;
static int ischown;
static const char *gname;
static volatile sig_atomic_t siginfo;
static void
siginfo_handler(int sig __unused)
{
siginfo = 1;
}
int
main(int argc, char **argv)
@ -119,6 +129,8 @@ main(int argc, char **argv)
if (argc < 2)
usage();
(void)signal(SIGINFO, siginfo_handler);
if (Rflag) {
if (hflag && (Hflag || Lflag))
errx(1, "the -R%c and -h options may not be "
@ -189,6 +201,10 @@ main(int argc, char **argv)
default:
break;
}
if (siginfo) {
print_info(p, 2);
siginfo = 0;
}
if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) &&
(gid == (gid_t)-1 || gid == p->fts_statp->st_gid))
continue;
@ -196,35 +212,8 @@ main(int argc, char **argv)
== -1 && !fflag) {
chownerr(p->fts_path);
rval = 1;
} else if (vflag) {
printf("%s", p->fts_path);
if (vflag > 1) {
if (ischown) {
printf(": %ju:%ju -> %ju:%ju",
(uintmax_t)
p->fts_statp->st_uid,
(uintmax_t)
p->fts_statp->st_gid,
(uid == (uid_t)-1) ?
(uintmax_t)
p->fts_statp->st_uid :
(uintmax_t)uid,
(gid == (gid_t)-1) ?
(uintmax_t)
p->fts_statp->st_gid :
(uintmax_t)gid);
} else {
printf(": %ju -> %ju",
(uintmax_t)
p->fts_statp->st_gid,
(gid == (gid_t)-1) ?
(uintmax_t)
p->fts_statp->st_gid :
(uintmax_t)gid);
}
}
printf("\n");
}
} else if (vflag)
print_info(p, vflag);
}
if (errno)
err(1, "fts_read");
@ -315,3 +304,26 @@ usage(void)
"usage: chgrp [-fhvx] [-R [-H | -L | -P]] group file ...");
exit(1);
}
static void
print_info(const FTSENT *p, int vflag)
{
printf("%s", p->fts_path);
if (vflag > 1) {
if (ischown) {
printf(": %ju:%ju -> %ju:%ju",
(uintmax_t)p->fts_statp->st_uid,
(uintmax_t)p->fts_statp->st_gid,
(uid == (uid_t)-1) ?
(uintmax_t)p->fts_statp->st_uid : (uintmax_t)uid,
(gid == (gid_t)-1) ?
(uintmax_t)p->fts_statp->st_gid : (uintmax_t)gid);
} else {
printf(": %ju -> %ju", (uintmax_t)p->fts_statp->st_gid,
(gid == (gid_t)-1) ?
(uintmax_t)p->fts_statp->st_gid : (uintmax_t)gid);
}
}
printf("\n");
}