aboutsummaryrefslogtreecommitdiffstats
path: root/tests/zfs-tests/cmd/mkbusy.c
blob: 78860381d880259f511685b511329b4533271135 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * 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>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>


static __attribute__((noreturn)) void
usage(const char *progname)
{
	(void) fprintf(stderr, "Usage: %s <dirname|filename>\n", progname);
	exit(1);
}

static __attribute__((noreturn)) void
fail(const char *err)
{
	perror(err);
	exit(1);
}

static void
daemonize(void)
{
	pid_t	pid;

	if ((pid = fork()) < 0) {
		fail("fork");
	} else if (pid != 0) {
		(void) fprintf(stdout, "%ld\n", (long)pid);
		exit(0);
	}

	(void) setsid();
	(void) close(0);
	(void) close(1);
	(void) close(2);
}


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);
}

int
main(int argc, char *argv[])
{
	int		c;
	boolean_t	isdir = B_FALSE;
	struct stat	sbuf;
	char		*fpath = NULL;
	char		*prog = argv[0];

	while ((c = getopt(argc, argv, "")) != -1) {
		switch (c) {
		default:
			usage(prog);
		}
	}

	argc -= optind;
	argv += optind;

	if (argc != 1)
		usage(prog);

	if (stat(argv[0], &sbuf) != 0) {
		char	*arg;
		const char	*dname, *fname;
		size_t	arglen;
		ssize_t	dnamelen;

		/*
		 * The argument supplied doesn't exist. Copy the path, and
		 * remove the trailing slash if present.
		 */
		if ((arg = strdup(argv[0])) == NULL)
			fail("strdup");
		arglen = strlen(arg);
		if (arg[arglen - 1] == '/')
			arg[arglen - 1] = '\0';

		/* Get the directory and file names. */
		fname = get_basename(arg);
		dname = arg;
		if ((dnamelen = get_dirnamelen(arg)) != -1)
			arg[dnamelen] = '\0';
		else
			dname = ".";

		/* The directory portion of the path must exist */
		if (stat(dname, &sbuf) != 0 || !(sbuf.st_mode & S_IFDIR))
			usage(prog);

		if (asprintf(&fpath, "%s/%s", dname, fname) == -1)
			fail("asprintf");

		free(arg);
	} else
		switch (sbuf.st_mode & S_IFMT) {
			case S_IFDIR:
				isdir = B_TRUE;
				zfs_fallthrough;
			case S_IFLNK:
			case S_IFCHR:
			case S_IFBLK:
				if ((fpath = strdup(argv[0])) == NULL)
					fail("strdup");
				break;
			default:
				usage(prog);
		}

	if (!isdir) {
		if (open(fpath, O_CREAT | O_RDWR, 0600) < 0)
			fail("open");
	} else {
		if (opendir(fpath) == NULL)
			fail("opendir");
	}
	free(fpath);

	daemonize();
	(void) pause();

	return (0);
}