2015-07-01 22:23:09 +00:00
|
|
|
/*
|
|
|
|
* This file and its contents are supplied under the terms of the
|
|
|
|
* Common Development and Distribution License ("CDDL"), version 1.0.
|
|
|
|
* You may only use this file in accordance with the terms of version
|
|
|
|
* 1.0 of the CDDL.
|
|
|
|
*
|
|
|
|
* A full copy of the text of the CDDL should have accompanied this
|
|
|
|
* source. A copy of the CDDL is also available via the Internet at
|
|
|
|
* http://www.illumos.org/license/CDDL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 by Delphix. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make a directory busy. If the argument is an existing file or directory,
|
|
|
|
* simply open it directly and pause. If not, verify that the parent directory
|
|
|
|
* exists, and create a new file in that directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dirent.h>
|
2022-01-22 00:56:46 +00:00
|
|
|
#include <string.h>
|
2015-07-01 22:23:09 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-06-05 12:18:37 +00:00
|
|
|
|
2022-03-23 15:51:00 +00:00
|
|
|
static __attribute__((noreturn)) void
|
2022-04-19 18:38:30 +00:00
|
|
|
usage(const char *progname)
|
2015-07-01 22:23:09 +00:00
|
|
|
{
|
|
|
|
(void) fprintf(stderr, "Usage: %s <dirname|filename>\n", progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-03-23 15:51:00 +00:00
|
|
|
static __attribute__((noreturn)) void
|
2022-04-19 18:38:30 +00:00
|
|
|
fail(const char *err)
|
2015-07-01 22:23:09 +00:00
|
|
|
{
|
|
|
|
perror(err);
|
2021-06-05 12:18:37 +00:00
|
|
|
exit(1);
|
2015-07-01 22:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
daemonize(void)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
if ((pid = fork()) < 0) {
|
2021-06-05 12:18:37 +00:00
|
|
|
fail("fork");
|
2015-07-01 22:23:09 +00:00
|
|
|
} else if (pid != 0) {
|
|
|
|
(void) fprintf(stdout, "%ld\n", (long)pid);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) setsid();
|
|
|
|
(void) close(0);
|
|
|
|
(void) close(1);
|
|
|
|
(void) close(2);
|
|
|
|
}
|
|
|
|
|
2021-10-07 17:31:26 +00:00
|
|
|
|
|
|
|
static const char *
|
|
|
|
get_basename(const char *path)
|
|
|
|
{
|
|
|
|
const char *bn = strrchr(path, '/');
|
|
|
|
return (bn ? bn + 1 : path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
get_dirnamelen(const char *path)
|
|
|
|
{
|
|
|
|
const char *end = strrchr(path, '/');
|
|
|
|
return (end ? end - path : -1);
|
|
|
|
}
|
|
|
|
|
2015-07-01 22:23:09 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2021-06-05 12:18:37 +00:00
|
|
|
int c;
|
2015-07-01 22:23:09 +00:00
|
|
|
boolean_t isdir = B_FALSE;
|
|
|
|
struct stat sbuf;
|
|
|
|
char *fpath = NULL;
|
|
|
|
char *prog = argv[0];
|
|
|
|
|
2021-06-05 12:18:37 +00:00
|
|
|
while ((c = getopt(argc, argv, "")) != -1) {
|
2015-07-01 22:23:09 +00:00
|
|
|
switch (c) {
|
|
|
|
default:
|
|
|
|
usage(prog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc != 1)
|
|
|
|
usage(prog);
|
|
|
|
|
2021-06-05 12:18:37 +00:00
|
|
|
if (stat(argv[0], &sbuf) != 0) {
|
|
|
|
char *arg;
|
|
|
|
const char *dname, *fname;
|
|
|
|
size_t arglen;
|
|
|
|
ssize_t dnamelen;
|
2015-07-01 22:23:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The argument supplied doesn't exist. Copy the path, and
|
2020-06-10 04:24:09 +00:00
|
|
|
* remove the trailing slash if present.
|
2015-07-01 22:23:09 +00:00
|
|
|
*/
|
|
|
|
if ((arg = strdup(argv[0])) == NULL)
|
2021-06-05 12:18:37 +00:00
|
|
|
fail("strdup");
|
2015-07-01 22:23:09 +00:00
|
|
|
arglen = strlen(arg);
|
|
|
|
if (arg[arglen - 1] == '/')
|
|
|
|
arg[arglen - 1] = '\0';
|
|
|
|
|
2021-06-05 12:18:37 +00:00
|
|
|
/* Get the directory and file names. */
|
2021-10-07 17:31:26 +00:00
|
|
|
fname = get_basename(arg);
|
2021-06-05 12:18:37 +00:00
|
|
|
dname = arg;
|
2021-10-07 17:31:26 +00:00
|
|
|
if ((dnamelen = get_dirnamelen(arg)) != -1)
|
2021-06-05 12:18:37 +00:00
|
|
|
arg[dnamelen] = '\0';
|
|
|
|
else
|
|
|
|
dname = ".";
|
2015-07-01 22:23:09 +00:00
|
|
|
|
|
|
|
/* The directory portion of the path must exist */
|
2021-06-05 12:18:37 +00:00
|
|
|
if (stat(dname, &sbuf) != 0 || !(sbuf.st_mode & S_IFDIR))
|
2015-07-01 22:23:09 +00:00
|
|
|
usage(prog);
|
|
|
|
|
2021-06-05 12:18:37 +00:00
|
|
|
if (asprintf(&fpath, "%s/%s", dname, fname) == -1)
|
|
|
|
fail("asprintf");
|
2015-07-01 22:23:09 +00:00
|
|
|
|
2021-06-05 12:18:37 +00:00
|
|
|
free(arg);
|
|
|
|
} else
|
|
|
|
switch (sbuf.st_mode & S_IFMT) {
|
|
|
|
case S_IFDIR:
|
|
|
|
isdir = B_TRUE;
|
2022-02-15 16:58:59 +00:00
|
|
|
zfs_fallthrough;
|
2021-06-05 12:18:37 +00:00
|
|
|
case S_IFLNK:
|
|
|
|
case S_IFCHR:
|
|
|
|
case S_IFBLK:
|
|
|
|
if ((fpath = strdup(argv[0])) == NULL)
|
|
|
|
fail("strdup");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(prog);
|
|
|
|
}
|
2015-07-01 22:23:09 +00:00
|
|
|
|
2021-06-05 12:18:37 +00:00
|
|
|
if (!isdir) {
|
Cleanup: Address Clang's static analyzer's unused code complaints
These were categorized as the following:
* Dead assignment 23
* Dead increment 4
* Dead initialization 6
* Dead nested assignment 18
Most of these are harmless, but since actual issues can hide among them,
we correct them.
That said, there were a few return values that were being ignored that
appeared to merit some correction:
* `destroy_callback()` in `cmd/zfs/zfs_main.c` ignored the error from
`destroy_batched()`. We handle it by returning -1 if there is an
error.
* `zfs_do_upgrade()` in `cmd/zfs/zfs_main.c` ignored the error from
`zfs_for_each()`. We handle it by doing a binary OR of the error
value from the subsequent `zfs_for_each()` call to the existing
value. This is how errors are mostly handled inside `zfs_for_each()`.
The error value here is passed to exit from the zfs command, so doing
a binary or on it is better than what we did previously.
* `get_zap_prop()` in `module/zfs/zcp_get.c` ignored the error from
`dsl_prop_get_ds()` when the property is not of type string. We
return an error when it does. There is a small concern that the
`zfs_get_temporary_prop()` call would handle things, but in the case
that it does not, we would be pushing an uninitialized numval onto
the lua stack. It is expected that `dsl_prop_get_ds()` will succeed
anytime that `zfs_get_temporary_prop()` does, so that not giving it a
chance to fix things is not a problem.
* `draid_merge_impl()` in `tests/zfs-tests/cmd/draid.c` used
`nvlist_add_nvlist()` twice in ways in which errors are expected to
be impossible, so we switch to `fnvlist_add_nvlist()`.
A few notable ones did not merit use of the return value, so we
suppressed it with `(void)`:
* `write_free_diffs()` in `lib/libzfs/libzfs_diff.c` ignored the error
value from `describe_free()`. A look through the commit history
revealed that this was intentional.
* `arc_evict_hdr()` in `module/zfs/arc.c` did not need to use the
returned handle from `arc_hdr_realloc()` because it is already
referenced in lists.
* `spa_vdev_detach()` in `module/zfs/spa.c` has a comment explicitly
saying not to use the error from `vdev_label_init()` because whatever
causes the error could be the reason why a detach is being done.
Unfortunately, I am not presently able to analyze the kernel modules
with Clang's static analyzer, so I could have missed some cases of this.
In cases where reports were present in code that is duplicated between
Linux and FreeBSD, I made a conscious effort to fix the FreeBSD version
too.
After this commit is merged, regressions like dee8934 should become
extremely obvious with Clang's static analyzer since a regression would
appear in the results as the only instance of unused code. That assumes
that Coverity does not catch the issue first.
My local branch with fixes from all of my outstanding non-draft pull
requests shows 118 reports from Clang's static anlayzer after this
patch. That is down by 51 from 169.
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Cedric Berger <cedric@precidata.com>
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Closes #13986
2022-10-14 20:37:54 +00:00
|
|
|
if (open(fpath, O_CREAT | O_RDWR, 0600) < 0)
|
2021-06-05 12:18:37 +00:00
|
|
|
fail("open");
|
2015-07-01 22:23:09 +00:00
|
|
|
} else {
|
Cleanup: Address Clang's static analyzer's unused code complaints
These were categorized as the following:
* Dead assignment 23
* Dead increment 4
* Dead initialization 6
* Dead nested assignment 18
Most of these are harmless, but since actual issues can hide among them,
we correct them.
That said, there were a few return values that were being ignored that
appeared to merit some correction:
* `destroy_callback()` in `cmd/zfs/zfs_main.c` ignored the error from
`destroy_batched()`. We handle it by returning -1 if there is an
error.
* `zfs_do_upgrade()` in `cmd/zfs/zfs_main.c` ignored the error from
`zfs_for_each()`. We handle it by doing a binary OR of the error
value from the subsequent `zfs_for_each()` call to the existing
value. This is how errors are mostly handled inside `zfs_for_each()`.
The error value here is passed to exit from the zfs command, so doing
a binary or on it is better than what we did previously.
* `get_zap_prop()` in `module/zfs/zcp_get.c` ignored the error from
`dsl_prop_get_ds()` when the property is not of type string. We
return an error when it does. There is a small concern that the
`zfs_get_temporary_prop()` call would handle things, but in the case
that it does not, we would be pushing an uninitialized numval onto
the lua stack. It is expected that `dsl_prop_get_ds()` will succeed
anytime that `zfs_get_temporary_prop()` does, so that not giving it a
chance to fix things is not a problem.
* `draid_merge_impl()` in `tests/zfs-tests/cmd/draid.c` used
`nvlist_add_nvlist()` twice in ways in which errors are expected to
be impossible, so we switch to `fnvlist_add_nvlist()`.
A few notable ones did not merit use of the return value, so we
suppressed it with `(void)`:
* `write_free_diffs()` in `lib/libzfs/libzfs_diff.c` ignored the error
value from `describe_free()`. A look through the commit history
revealed that this was intentional.
* `arc_evict_hdr()` in `module/zfs/arc.c` did not need to use the
returned handle from `arc_hdr_realloc()` because it is already
referenced in lists.
* `spa_vdev_detach()` in `module/zfs/spa.c` has a comment explicitly
saying not to use the error from `vdev_label_init()` because whatever
causes the error could be the reason why a detach is being done.
Unfortunately, I am not presently able to analyze the kernel modules
with Clang's static analyzer, so I could have missed some cases of this.
In cases where reports were present in code that is duplicated between
Linux and FreeBSD, I made a conscious effort to fix the FreeBSD version
too.
After this commit is merged, regressions like dee8934 should become
extremely obvious with Clang's static analyzer since a regression would
appear in the results as the only instance of unused code. That assumes
that Coverity does not catch the issue first.
My local branch with fixes from all of my outstanding non-draft pull
requests shows 118 reports from Clang's static anlayzer after this
patch. That is down by 51 from 169.
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Cedric Berger <cedric@precidata.com>
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Closes #13986
2022-10-14 20:37:54 +00:00
|
|
|
if (opendir(fpath) == NULL)
|
2021-06-05 12:18:37 +00:00
|
|
|
fail("opendir");
|
2015-07-01 22:23:09 +00:00
|
|
|
}
|
|
|
|
free(fpath);
|
|
|
|
|
2021-06-05 12:18:37 +00:00
|
|
|
daemonize();
|
2015-07-01 22:23:09 +00:00
|
|
|
(void) pause();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|