xref: /linux/tools/testing/selftests/proc/proc-self-wchan.c (revision ee9294d6ddbd418807ae49f4ab4e6441c9da2a18)
1*ee9294d6SAlexey Dobriyan /*
2*ee9294d6SAlexey Dobriyan  * Copyright © 2018 Alexey Dobriyan <adobriyan@gmail.com>
3*ee9294d6SAlexey Dobriyan  *
4*ee9294d6SAlexey Dobriyan  * Permission to use, copy, modify, and distribute this software for any
5*ee9294d6SAlexey Dobriyan  * purpose with or without fee is hereby granted, provided that the above
6*ee9294d6SAlexey Dobriyan  * copyright notice and this permission notice appear in all copies.
7*ee9294d6SAlexey Dobriyan  *
8*ee9294d6SAlexey Dobriyan  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9*ee9294d6SAlexey Dobriyan  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10*ee9294d6SAlexey Dobriyan  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11*ee9294d6SAlexey Dobriyan  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12*ee9294d6SAlexey Dobriyan  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13*ee9294d6SAlexey Dobriyan  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14*ee9294d6SAlexey Dobriyan  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*ee9294d6SAlexey Dobriyan  */
169cd65655SAlexey Dobriyan #include <sys/types.h>
179cd65655SAlexey Dobriyan #include <sys/stat.h>
189cd65655SAlexey Dobriyan #include <fcntl.h>
199cd65655SAlexey Dobriyan #include <errno.h>
209cd65655SAlexey Dobriyan #include <unistd.h>
219cd65655SAlexey Dobriyan 
229cd65655SAlexey Dobriyan int main(void)
239cd65655SAlexey Dobriyan {
249cd65655SAlexey Dobriyan 	char buf[64];
259cd65655SAlexey Dobriyan 	int fd;
269cd65655SAlexey Dobriyan 
279cd65655SAlexey Dobriyan 	fd = open("/proc/self/wchan", O_RDONLY);
289cd65655SAlexey Dobriyan 	if (fd == -1) {
299cd65655SAlexey Dobriyan 		if (errno == ENOENT)
309cd65655SAlexey Dobriyan 			return 2;
319cd65655SAlexey Dobriyan 		return 1;
329cd65655SAlexey Dobriyan 	}
339cd65655SAlexey Dobriyan 
349cd65655SAlexey Dobriyan 	buf[0] = '\0';
359cd65655SAlexey Dobriyan 	if (read(fd, buf, sizeof(buf)) != 1)
369cd65655SAlexey Dobriyan 		return 1;
379cd65655SAlexey Dobriyan 	if (buf[0] != '0')
389cd65655SAlexey Dobriyan 		return 1;
399cd65655SAlexey Dobriyan 	return 0;
409cd65655SAlexey Dobriyan }
41