xref: /freebsd/tests/sys/ses/common.h (revision 978c7e224719f27f66b5f40a55aeec996758f681)
1eea7c615SAlan Somers /*-
2eea7c615SAlan Somers  * Copyright (C) 2021 Axcient, Inc. All rights reserved.
3eea7c615SAlan Somers  *
4eea7c615SAlan Somers  * Redistribution and use in source and binary forms, with or without
5eea7c615SAlan Somers  * modification, are permitted provided that the following conditions
6eea7c615SAlan Somers  * are met:
7eea7c615SAlan Somers  * 1. Redistributions of source code must retain the above copyright
8eea7c615SAlan Somers  *    notice, this list of conditions and the following disclaimer.
9eea7c615SAlan Somers  * 2. Redistributions in binary form must reproduce the above copyright
10eea7c615SAlan Somers  *    notice, this list of conditions and the following disclaimer in the
11eea7c615SAlan Somers  *    documentation and/or other materials provided with the distribution.
12eea7c615SAlan Somers  *
13eea7c615SAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14eea7c615SAlan Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15eea7c615SAlan Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16eea7c615SAlan Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17eea7c615SAlan Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18eea7c615SAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19eea7c615SAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20eea7c615SAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21eea7c615SAlan Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22eea7c615SAlan Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23eea7c615SAlan Somers  * SUCH DAMAGE.
24eea7c615SAlan Somers  *
25eea7c615SAlan Somers  * $FreeBSD$
26eea7c615SAlan Somers  */
27eea7c615SAlan Somers 
28eea7c615SAlan Somers typedef bool(*ses_cb)(const char *devname, int fd);
29eea7c615SAlan Somers 
30eea7c615SAlan Somers // Run a test function on every available ses device
31eea7c615SAlan Somers static void
32eea7c615SAlan Somers for_each_ses_dev(ses_cb cb, int oflags)
33eea7c615SAlan Somers {
34eea7c615SAlan Somers 	glob_t g;
35eea7c615SAlan Somers 	int r;
36eea7c615SAlan Somers 	unsigned i;
37eea7c615SAlan Somers 
38eea7c615SAlan Somers 	g.gl_pathc = 0;
39eea7c615SAlan Somers 	g.gl_pathv = NULL;
40eea7c615SAlan Somers 	g.gl_offs = 0;
41eea7c615SAlan Somers 
422c449a4cSLi-Wen Hsu 	r = glob("/dev/ses*", GLOB_NOCHECK | GLOB_NOSORT, NULL, &g);
43eea7c615SAlan Somers 	ATF_REQUIRE_EQ(r, 0);
442c449a4cSLi-Wen Hsu 	if (g.gl_matchc == 0)
452c449a4cSLi-Wen Hsu 		return;
46eea7c615SAlan Somers 
472c449a4cSLi-Wen Hsu 	for(i = 0; i < g.gl_matchc; i++) {
48eea7c615SAlan Somers 		int fd;
49eea7c615SAlan Somers 
50eea7c615SAlan Somers 		fd = open(g.gl_pathv[i], oflags);
51eea7c615SAlan Somers 		ATF_REQUIRE(fd >= 0);
522c449a4cSLi-Wen Hsu 		cb(g.gl_pathv[i], fd);
53eea7c615SAlan Somers 		close(fd);
54eea7c615SAlan Somers 	}
55eea7c615SAlan Somers 
56eea7c615SAlan Somers 	globfree(&g);
57eea7c615SAlan Somers }
582c449a4cSLi-Wen Hsu 
592c449a4cSLi-Wen Hsu static bool
60*978c7e22SJohn Baldwin has_ses(void)
612c449a4cSLi-Wen Hsu {
622c449a4cSLi-Wen Hsu 	glob_t g;
632c449a4cSLi-Wen Hsu 	int r;
642c449a4cSLi-Wen Hsu 
652c449a4cSLi-Wen Hsu 	r = glob("/dev/ses*", GLOB_NOCHECK | GLOB_NOSORT, NULL, &g);
662c449a4cSLi-Wen Hsu 	ATF_REQUIRE_EQ(r, 0);
672c449a4cSLi-Wen Hsu 
682c449a4cSLi-Wen Hsu 	return (g.gl_matchc != 0);
692c449a4cSLi-Wen Hsu }
70