xref: /freebsd/tests/sys/ses/common.h (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
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 
26eea7c615SAlan Somers typedef bool(*ses_cb)(const char *devname, int fd);
27eea7c615SAlan Somers 
28eea7c615SAlan Somers // Run a test function on every available ses device
29eea7c615SAlan Somers static void
for_each_ses_dev(ses_cb cb,int oflags)30eea7c615SAlan Somers for_each_ses_dev(ses_cb cb, int oflags)
31eea7c615SAlan Somers {
32eea7c615SAlan Somers 	glob_t g;
33eea7c615SAlan Somers 	int r;
34eea7c615SAlan Somers 	unsigned i;
35eea7c615SAlan Somers 
36eea7c615SAlan Somers 	g.gl_pathc = 0;
37eea7c615SAlan Somers 	g.gl_pathv = NULL;
38eea7c615SAlan Somers 	g.gl_offs = 0;
39eea7c615SAlan Somers 
402c449a4cSLi-Wen Hsu 	r = glob("/dev/ses*", GLOB_NOCHECK | GLOB_NOSORT, NULL, &g);
41eea7c615SAlan Somers 	ATF_REQUIRE_EQ(r, 0);
422c449a4cSLi-Wen Hsu 	if (g.gl_matchc == 0)
432c449a4cSLi-Wen Hsu 		return;
44eea7c615SAlan Somers 
452c449a4cSLi-Wen Hsu 	for(i = 0; i < g.gl_matchc; i++) {
46eea7c615SAlan Somers 		int fd;
47eea7c615SAlan Somers 
48eea7c615SAlan Somers 		fd = open(g.gl_pathv[i], oflags);
49eea7c615SAlan Somers 		ATF_REQUIRE(fd >= 0);
502c449a4cSLi-Wen Hsu 		cb(g.gl_pathv[i], fd);
51eea7c615SAlan Somers 		close(fd);
52eea7c615SAlan Somers 	}
53eea7c615SAlan Somers 
54eea7c615SAlan Somers 	globfree(&g);
55eea7c615SAlan Somers }
562c449a4cSLi-Wen Hsu 
572c449a4cSLi-Wen Hsu static bool
has_ses(void)58*978c7e22SJohn Baldwin has_ses(void)
592c449a4cSLi-Wen Hsu {
602c449a4cSLi-Wen Hsu 	glob_t g;
612c449a4cSLi-Wen Hsu 	int r;
622c449a4cSLi-Wen Hsu 
632c449a4cSLi-Wen Hsu 	r = glob("/dev/ses*", GLOB_NOCHECK | GLOB_NOSORT, NULL, &g);
642c449a4cSLi-Wen Hsu 	ATF_REQUIRE_EQ(r, 0);
652c449a4cSLi-Wen Hsu 
662c449a4cSLi-Wen Hsu 	return (g.gl_matchc != 0);
672c449a4cSLi-Wen Hsu }
68