Project

General

Profile

Bug #5918 ยป destroy-leak.c

Jan Schlien, 2015-05-11 08:58 AM

 
1
#include <libzfs.h>
2
#include <stdio.h>
3
#include <string.h>
4
#include <errno.h>
5
#include <unistd.h>
6

    
7
int
8
main(int argc, char **argv)
9
{
10
	libzfs_handle_t *zhl;
11
	zfs_handle_t *zfh_fs;
12
	zfs_handle_t *zfh_snap;
13
	char snap[ZFS_MAXNAMELEN];
14
	char clone[ZFS_MAXNAMELEN];
15
	int ret;
16

    
17
	if (argc != 2) {
18
		fprintf(stderr, "file system arg required\n");
19
		return 1;
20
	}
21

    
22
	zhl = libzfs_init();
23
	zfh_fs = zfs_open(zhl, argv[1], ZFS_TYPE_FILESYSTEM);
24
	if (!zfh_fs) {
25
		fprintf(stderr, "cannot open zfs %s: %s\n",
26
			argv[1], libzfs_error_description(zhl));
27
		return 1;
28
	}
29

    
30
	snprintf(snap, sizeof(snap), "%s@%s", argv[1], "snap");
31
	ret = zfs_snapshot(zhl, snap, 0, NULL);
32
	if (ret) {
33
		fprintf(stderr, "zfs_snapshot(%s) failed: %s\n", snap,
34
			libzfs_error_description(zhl));
35
		return 1;
36
	}
37

    
38
	zfh_snap = zfs_open(zhl, snap, ZFS_TYPE_SNAPSHOT);
39
	if (!zfh_snap) {
40
		fprintf(stderr, "snap %s created, but zfs_open failed: %s\n",
41
			snap, libzfs_error_description(zhl));
42
		return 1;
43
	}
44

    
45
	strlcpy(clone, argv[1], sizeof(clone));
46
	strlcat(clone, "-clone", sizeof(clone));
47
	ret = zfs_clone(zfh_snap, clone, NULL);
48
	if (ret) {
49
		fprintf(stderr, "failed to create clone %s of %s: %s\n", clone,
50
			snap, libzfs_error_description(zhl));
51
		return 1;
52
	}
53
	zfs_close(zfh_snap);
54

    
55
	ret = zfs_destroy_snaps(zfh_fs, "snap", 0);
56
	if (!ret) {
57
		fprintf(stderr, "snapshot destroyed, demonstration failed "
58
			"with %d\n", libzfs_errno(zhl));
59
		return 1;
60
	}
61

    
62
	printf("ok, snapshot destruction failed with %d. check ::findleaks\n",
63
		libzfs_errno(zhl));
64
	zfs_close(zfh_fs);
65
	libzfs_fini(zhl);
66

    
67
	sleep(999999);
68

    
69
	return 0;
70
}
    (1-1/1)