xref: /freebsd/tools/test/callout_free/callout_free.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1*8c5a9161SEric van Gyzen /*-
2*8c5a9161SEric van Gyzen  * SPDX-License-Identifier: BSD-2-Clause
3*8c5a9161SEric van Gyzen  *
4*8c5a9161SEric van Gyzen  * Copyright (c) 2019 Eric van Gyzen
5*8c5a9161SEric van Gyzen  *
6*8c5a9161SEric van Gyzen  * Redistribution and use in source and binary forms, with or without
7*8c5a9161SEric van Gyzen  * modification, are permitted provided that the following conditions
8*8c5a9161SEric van Gyzen  * are met:
9*8c5a9161SEric van Gyzen  * 1. Redistributions of source code must retain the above copyright
10*8c5a9161SEric van Gyzen  *    notice, this list of conditions and the following disclaimer.
11*8c5a9161SEric van Gyzen  * 2. Redistributions in binary form must reproduce the above copyright
12*8c5a9161SEric van Gyzen  *    notice, this list of conditions and the following disclaimer in the
13*8c5a9161SEric van Gyzen  *    documentation and/or other materials provided with the distribution.
14*8c5a9161SEric van Gyzen  *
15*8c5a9161SEric van Gyzen  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*8c5a9161SEric van Gyzen  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*8c5a9161SEric van Gyzen  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*8c5a9161SEric van Gyzen  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*8c5a9161SEric van Gyzen  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*8c5a9161SEric van Gyzen  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*8c5a9161SEric van Gyzen  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*8c5a9161SEric van Gyzen  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*8c5a9161SEric van Gyzen  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*8c5a9161SEric van Gyzen  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*8c5a9161SEric van Gyzen  * SUCH DAMAGE.
26*8c5a9161SEric van Gyzen  */
27*8c5a9161SEric van Gyzen 
28*8c5a9161SEric van Gyzen /*
29*8c5a9161SEric van Gyzen  * Free a pending callout.  This was useful for testing the
30*8c5a9161SEric van Gyzen  * "show callout_last" ddb command.
31*8c5a9161SEric van Gyzen  */
32*8c5a9161SEric van Gyzen 
33*8c5a9161SEric van Gyzen #include <sys/param.h>
34*8c5a9161SEric van Gyzen #include <sys/conf.h>
35*8c5a9161SEric van Gyzen #include <sys/kernel.h>
36*8c5a9161SEric van Gyzen #include <sys/lock.h>
37*8c5a9161SEric van Gyzen #include <sys/module.h>
38*8c5a9161SEric van Gyzen #include <sys/mutex.h>
39*8c5a9161SEric van Gyzen #include <sys/systm.h>
40*8c5a9161SEric van Gyzen 
41*8c5a9161SEric van Gyzen static struct callout callout_free;
42*8c5a9161SEric van Gyzen static struct mtx callout_free_mutex;
43*8c5a9161SEric van Gyzen static int callout_free_arg;
44*8c5a9161SEric van Gyzen 
45*8c5a9161SEric van Gyzen static void
callout_free_func(void * arg)46*8c5a9161SEric van Gyzen callout_free_func(void *arg)
47*8c5a9161SEric van Gyzen {
48*8c5a9161SEric van Gyzen 	printf("squirrel!\n");
49*8c5a9161SEric van Gyzen 	mtx_destroy(&callout_free_mutex);
50*8c5a9161SEric van Gyzen 	memset(&callout_free, 'C', sizeof(callout_free));
51*8c5a9161SEric van Gyzen }
52*8c5a9161SEric van Gyzen 
53*8c5a9161SEric van Gyzen static int
callout_free_load(module_t mod,int cmd,void * arg)54*8c5a9161SEric van Gyzen callout_free_load(module_t mod, int cmd, void *arg)
55*8c5a9161SEric van Gyzen {
56*8c5a9161SEric van Gyzen 	int error;
57*8c5a9161SEric van Gyzen 
58*8c5a9161SEric van Gyzen 	switch (cmd) {
59*8c5a9161SEric van Gyzen 	case MOD_LOAD:
60*8c5a9161SEric van Gyzen 		mtx_init(&callout_free_mutex, "callout_free", NULL, MTX_DEF);
61*8c5a9161SEric van Gyzen 		/*
62*8c5a9161SEric van Gyzen 		 * Do not pass CALLOUT_RETURNUNLOCKED so the callout
63*8c5a9161SEric van Gyzen 		 * subsystem will unlock the "destroyed" mutex.
64*8c5a9161SEric van Gyzen 		 */
65*8c5a9161SEric van Gyzen 		callout_init_mtx(&callout_free, &callout_free_mutex, 0);
66*8c5a9161SEric van Gyzen 		printf("callout_free_func = %p\n", callout_free_func);
67*8c5a9161SEric van Gyzen 		printf("callout_free_arg = %p\n", &callout_free_arg);
68*8c5a9161SEric van Gyzen 		callout_reset(&callout_free, hz/10, callout_free_func,
69*8c5a9161SEric van Gyzen 		    &callout_free_arg);
70*8c5a9161SEric van Gyzen 		error = 0;
71*8c5a9161SEric van Gyzen 		break;
72*8c5a9161SEric van Gyzen 
73*8c5a9161SEric van Gyzen 	case MOD_UNLOAD:
74*8c5a9161SEric van Gyzen 		error = 0;
75*8c5a9161SEric van Gyzen 		break;
76*8c5a9161SEric van Gyzen 
77*8c5a9161SEric van Gyzen 	default:
78*8c5a9161SEric van Gyzen 		error = EOPNOTSUPP;
79*8c5a9161SEric van Gyzen 		break;
80*8c5a9161SEric van Gyzen 	}
81*8c5a9161SEric van Gyzen 
82*8c5a9161SEric van Gyzen 	return (error);
83*8c5a9161SEric van Gyzen }
84*8c5a9161SEric van Gyzen 
85*8c5a9161SEric van Gyzen DEV_MODULE(callout_free, callout_free_load, NULL);
86