Skip to content

Commit

Permalink
Add a blacklist option (-b) to define a comma-separated list of devic…
Browse files Browse the repository at this point in the history
…es to ignore if the -a option is given.
  • Loading branch information
mrclksr committed Jun 4, 2019
1 parent f1425c1 commit 1bfa749
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
5 changes: 5 additions & 0 deletions dsbmc-cli.1
Expand Up @@ -12,6 +12,7 @@
.Op Fl L Ar ...
.Nm
.Fl a
.Op Fl b Ar dev1,dev2,...
.Bo
.Fl U Ar time
.Bc
Expand Down Expand Up @@ -103,6 +104,10 @@ option can be specified for each event. If
automounts a device, it executes the command defined for the
.Em mount
event.
.It Fl b
A comma-separated list of devices to ignore if the
.Fl a
option is given.
.It Fl e Ar device , Fl e Ar <mount point>
Eject the given
.Ar device
Expand Down
49 changes: 43 additions & 6 deletions dsbmc-cli.c
Expand Up @@ -41,7 +41,8 @@

#include "libdsbmc/libdsbmc.h"

#define PATH_LOCKF ".dsbmc-cli.lock"
#define MAX_BLISTSZ 256
#define PATH_LOCKF ".dsbmc-cli.lock"

#define EXEC(dh, f) do { \
if (f == -1) \
Expand Down Expand Up @@ -95,10 +96,13 @@ static void sighandler(int signo);
static void pdie(void);
static void remove_thread(pthread_t tid);
static void add_unmount_thread(const dsbmc_dev_t *dev);
static bool blacklisted(const char *);
static void *auto_unmount(void *arg);
static char *dtype_to_name(uint8_t type);
static const dsbmc_dev_t *dev_from_mnt(const char *mnt);

static char *blist[MAX_BLISTSZ];
static size_t blistsz;
static size_t unmount_time;
static size_t ntids;
static dsbmc_t *dh;
Expand All @@ -109,17 +113,17 @@ int
main(int argc, char *argv[])
{
int i, ch, speed;
bool sflag, vflag, eflag, fflag, Uflag;
bool sflag, vflag, eflag, fflag, bflag, Uflag;
bool Lflag, aflag, mflag, uflag, lflag, iflag;
char *image;
char *image, *p;
const char seq[] = "-|/-\\|/";
struct stat sb;
dsbmc_event_t e;
const dsbmc_dev_t *dev;

fflag = lflag = sflag = vflag = Uflag = false;
fflag = lflag = sflag = vflag = bflag = Uflag = false;
Lflag = aflag = eflag = mflag = uflag = iflag = false;
while ((ch = getopt(argc, argv, "L:U:afimusehv:l")) != -1) {
while ((ch = getopt(argc, argv, "L:U:ab:fimusehv:l")) != -1) {
switch (ch) {
case 'L':
Lflag = true;
Expand All @@ -132,6 +136,15 @@ main(int argc, char *argv[])
case 'a':
aflag = true;
break;
case 'b':
bflag = true;
for (p = optarg;
(p = strtok(p, ",")) != NULL &&
blistsz < MAX_BLISTSZ; p = NULL)
blist[blistsz++] = p;
if (blistsz >= MAX_BLISTSZ)
warnx("MAX_BLISTSZ (%d) exceeded", MAX_BLISTSZ);
break;
case 'f':
fflag = true;
break;
Expand Down Expand Up @@ -174,6 +187,8 @@ main(int argc, char *argv[])
usage();
if (!!mflag + !!uflag + !!eflag + !!sflag + !!vflag + !!iflag > 1)
usage();
if (bflag && !aflag)
usage();
if ((dh = dsbmc_alloc_handle()) == NULL)
err(EXIT_FAILURE, "dsbmc_alloc_handle()");
if (dsbmc_connect(dh) == -1)
Expand Down Expand Up @@ -415,6 +430,25 @@ dtype_to_name(uint8_t type)
return (empty);
}

static bool
blacklisted(const char *dev)
{
int i;
char *path = NULL;
bool listed = false;

for (i = 0; i < blistsz && !listed; i++) {
if ((path = realpath(blist[i], NULL)) == NULL) {
warn("realpath(%s)", blist[i]);
continue;
}
if (strcmp(dev, path) == 0)
listed = true;
free(path);
}
return (listed);
}

static void
cb(int code, const dsbmc_dev_t *d)
{
Expand Down Expand Up @@ -599,6 +633,8 @@ do_listen(bool automount, bool autounmount)
continue;
if (dev->mounted)
continue;
if (blacklisted(dev->dev))
continue;
do_mount(dev);
exec_event_command(EVENT_MOUNT, dev);
if (autounmount)
Expand Down Expand Up @@ -658,7 +694,8 @@ static void
usage()
{
PU("!-L <event> <command> [arg ...] ; [ -L ...]");
PU("-a [-U <time>] [[-L <event> <command> [arg ...] ; [-L ...]]");
PU("-a [-b dev1,dev2,...] [-U <time>] [[-L <event> <command> " \
"[arg ...] ; [-L ...]]");
PU("{{-e | -u} [-f] | {-m | -s | -v <speed>}} <device>");
PU("{-e | -u} [-f] <mount point>");
PU("-i <disk image>");
Expand Down
6 changes: 6 additions & 0 deletions readme.mdoc
Expand Up @@ -21,6 +21,7 @@ mount, unmount and eject these.
.br
.Nm
.Fl a
.Op Fl b Ar dev1,dev2,...
.Bo
.Fl U Ar time
.Bc
Expand All @@ -29,6 +30,7 @@ mount, unmount and eject these.
\;
.Op Fl L Ar ...
.Bc
.br
.Nm
.Brq Bro Fl e | u Brc Bo Fl f Bc | Bro Fl m | s | u | v Ar speed Brc
.Ar device
Expand Down Expand Up @@ -109,6 +111,10 @@ option can be specified for each event. If
automounts a device, it executes the command defined for the
.Em mount
event.
.It Fl b
A comma-separated list of devices to ignore if the
.Fl a
option is given.
.It Fl e Ar device , Fl e Ar <mount point>
Eject the given
.Ar device
Expand Down

0 comments on commit 1bfa749

Please sign in to comment.