1 /* $NetBSD: k_helper.c,v 1.2 2008/03/02 11:22:11 jmmv Exp $ */ 2 /* 3 * Copyright (c) 2008 The NetBSD Foundation, Inc. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this 15 * software must display the following acknowledgement: 16 * This product includes software developed by the NetBSD 17 * Foundation, Inc. and its contributors. 18 * 4. Neither the name of The NetBSD Foundation nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 27 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 29 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 31 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 33 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: k_helper.c,v 1.2 2008/03/02 11:22:11 jmmv Exp $"); 38 39 #include <sys/param.h> 40 #include <sys/kernel.h> 41 #include <sys/module.h> 42 #include <sys/sysctl.h> 43 44 #include <prop/proplib.h> 45 46 MODULE(MODULE_CLASS_MISC, k_helper, NULL); 47 48 /* --------------------------------------------------------------------- */ 49 /* Sysctl interface to query information about the module. */ 50 /* --------------------------------------------------------------------- */ 51 52 /* TODO: Change the integer variables below that represent booleans to 53 * bools, once sysctl(8) supports CTLTYPE_BOOL nodes. */ 54 55 static struct sysctllog *clog; 56 static int present = 1; 57 static int prop_str_ok; 58 static char prop_str_val[128]; 59 static int prop_int_ok; 60 static int prop_int_val; 61 62 #define K_HELPER 0x12345678 63 #define K_HELPER_PRESENT 0 64 #define K_HELPER_PROP_STR_OK 1 65 #define K_HELPER_PROP_STR_VAL 2 66 #define K_HELPER_PROP_INT_OK 3 67 #define K_HELPER_PROP_INT_VAL 4 68 69 SYSCTL_SETUP(sysctl_k_helper_setup, "sysctl k_helper subtree setup") 70 { 71 72 sysctl_createv(clog, 0, NULL, NULL, 73 CTLFLAG_PERMANENT, 74 CTLTYPE_NODE, "k_helper", NULL, 75 NULL, 0, NULL, 0, 76 CTL_VENDOR, K_HELPER, CTL_EOL); 77 78 sysctl_createv(clog, 0, NULL, NULL, 79 CTLFLAG_PERMANENT, 80 CTLTYPE_INT, "present", 81 SYSCTL_DESCR("Whether the module was loaded or not"), 82 NULL, 0, &present, 0, 83 CTL_VENDOR, K_HELPER, K_HELPER_PRESENT, CTL_EOL); 84 85 sysctl_createv(clog, 0, NULL, NULL, 86 CTLFLAG_PERMANENT, 87 CTLTYPE_INT, "prop_str_ok", 88 SYSCTL_DESCR("String property's validity"), 89 NULL, 0, &prop_str_ok, 0, 90 CTL_VENDOR, K_HELPER, K_HELPER_PROP_STR_OK, CTL_EOL); 91 92 sysctl_createv(clog, 0, NULL, NULL, 93 CTLFLAG_PERMANENT, 94 CTLTYPE_STRING, "prop_str_val", 95 SYSCTL_DESCR("String property's value"), 96 NULL, 0, &prop_str_val, 0, 97 CTL_VENDOR, K_HELPER, K_HELPER_PROP_STR_VAL, CTL_EOL); 98 99 sysctl_createv(clog, 0, NULL, NULL, 100 CTLFLAG_PERMANENT, 101 CTLTYPE_INT, "prop_int_ok", 102 SYSCTL_DESCR("String property's validity"), 103 NULL, 0, &prop_int_ok, 0, 104 CTL_VENDOR, K_HELPER, K_HELPER_PROP_INT_OK, CTL_EOL); 105 106 sysctl_createv(clog, 0, NULL, NULL, 107 CTLFLAG_PERMANENT, 108 CTLTYPE_INT, "prop_int_val", 109 SYSCTL_DESCR("String property's value"), 110 NULL, 0, &prop_int_val, 0, 111 CTL_VENDOR, K_HELPER, K_HELPER_PROP_INT_VAL, CTL_EOL); 112 } 113 114 /* --------------------------------------------------------------------- */ 115 /* Module management. */ 116 /* --------------------------------------------------------------------- */ 117 118 static 119 int 120 k_helper_init(prop_dictionary_t props) 121 { 122 prop_object_t p; 123 124 p = prop_dictionary_get(props, "prop_str"); 125 if (p == NULL) 126 prop_str_ok = 0; 127 else if (prop_object_type(p) != PROP_TYPE_STRING) 128 prop_str_ok = 0; 129 else { 130 const char *msg = prop_string_cstring_nocopy(p); 131 if (msg == NULL) 132 prop_str_ok = 0; 133 else { 134 strlcpy(prop_str_val, msg, sizeof(prop_str_val)); 135 prop_str_ok = 1; 136 } 137 } 138 if (!prop_str_ok) 139 strlcpy(prop_str_val, "", sizeof(prop_str_val)); 140 141 p = prop_dictionary_get(props, "prop_int"); 142 if (p == NULL) 143 prop_int_ok = 0; 144 else if (prop_object_type(p) != PROP_TYPE_NUMBER) 145 prop_int_ok = 0; 146 else { 147 prop_int_val = prop_number_integer_value(p); 148 prop_int_ok = 1; 149 } 150 if (!prop_int_ok) 151 prop_int_val = -1; 152 153 sysctl_k_helper_setup(&clog); 154 155 return 0; 156 } 157 158 static 159 int 160 k_helper_fini(void *arg) 161 { 162 163 sysctl_teardown(&clog); 164 165 return 0; 166 } 167 168 static 169 int 170 k_helper_modcmd(modcmd_t cmd, void *arg) 171 { 172 int ret; 173 174 switch (cmd) { 175 case MODULE_CMD_INIT: 176 ret = k_helper_init(arg); 177 break; 178 179 case MODULE_CMD_FINI: 180 ret = k_helper_fini(arg); 181 break; 182 183 case MODULE_CMD_STAT: 184 default: 185 ret = ENOTTY; 186 } 187 188 return ret; 189 } 190