Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Generic dynamic event control interface |
| 4 | * |
| 5 | * Copyright (C) 2018 Masami Hiramatsu <mhiramat@kernel.org> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/debugfs.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/list.h> |
| 11 | #include <linux/mm.h> |
| 12 | #include <linux/mutex.h> |
| 13 | #include <linux/tracefs.h> |
| 14 | |
| 15 | #include "trace.h" |
Steven Rostedt (VMware) | 1d18538 | 2021-08-16 23:42:57 -0400 | [diff] [blame] | 16 | #include "trace_output.h" /* for trace_event_sem */ |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 17 | #include "trace_dynevent.h" |
| 18 | |
| 19 | static DEFINE_MUTEX(dyn_event_ops_mutex); |
| 20 | static LIST_HEAD(dyn_event_ops_list); |
| 21 | |
Steven Rostedt (VMware) | 1d18538 | 2021-08-16 23:42:57 -0400 | [diff] [blame] | 22 | bool trace_event_dyn_try_get_ref(struct trace_event_call *dyn_call) |
| 23 | { |
| 24 | struct trace_event_call *call; |
| 25 | bool ret = false; |
| 26 | |
| 27 | if (WARN_ON_ONCE(!(dyn_call->flags & TRACE_EVENT_FL_DYNAMIC))) |
| 28 | return false; |
| 29 | |
| 30 | down_read(&trace_event_sem); |
| 31 | list_for_each_entry(call, &ftrace_events, list) { |
| 32 | if (call == dyn_call) { |
| 33 | atomic_inc(&dyn_call->refcnt); |
| 34 | ret = true; |
| 35 | } |
| 36 | } |
| 37 | up_read(&trace_event_sem); |
| 38 | return ret; |
| 39 | } |
| 40 | |
| 41 | void trace_event_dyn_put_ref(struct trace_event_call *call) |
| 42 | { |
| 43 | if (WARN_ON_ONCE(!(call->flags & TRACE_EVENT_FL_DYNAMIC))) |
| 44 | return; |
| 45 | |
| 46 | if (WARN_ON_ONCE(atomic_read(&call->refcnt) <= 0)) { |
| 47 | atomic_set(&call->refcnt, 0); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | atomic_dec(&call->refcnt); |
| 52 | } |
| 53 | |
| 54 | bool trace_event_dyn_busy(struct trace_event_call *call) |
| 55 | { |
| 56 | return atomic_read(&call->refcnt) != 0; |
| 57 | } |
| 58 | |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 59 | int dyn_event_register(struct dyn_event_operations *ops) |
| 60 | { |
| 61 | if (!ops || !ops->create || !ops->show || !ops->is_busy || |
| 62 | !ops->free || !ops->match) |
| 63 | return -EINVAL; |
| 64 | |
| 65 | INIT_LIST_HEAD(&ops->list); |
| 66 | mutex_lock(&dyn_event_ops_mutex); |
| 67 | list_add_tail(&ops->list, &dyn_event_ops_list); |
| 68 | mutex_unlock(&dyn_event_ops_mutex); |
| 69 | return 0; |
| 70 | } |
| 71 | |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 72 | int dyn_event_release(const char *raw_command, struct dyn_event_operations *type) |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 73 | { |
| 74 | struct dyn_event *pos, *n; |
| 75 | char *system = NULL, *event, *p; |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 76 | int argc, ret = -ENOENT; |
| 77 | char **argv; |
| 78 | |
| 79 | argv = argv_split(GFP_KERNEL, raw_command, &argc); |
| 80 | if (!argv) |
| 81 | return -ENOMEM; |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 82 | |
Masami Hiramatsu | 1ce25e9 | 2018-11-05 18:04:57 +0900 | [diff] [blame] | 83 | if (argv[0][0] == '-') { |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 84 | if (argv[0][1] != ':') { |
| 85 | ret = -EINVAL; |
| 86 | goto out; |
| 87 | } |
Masami Hiramatsu | 1ce25e9 | 2018-11-05 18:04:57 +0900 | [diff] [blame] | 88 | event = &argv[0][2]; |
| 89 | } else { |
| 90 | event = strchr(argv[0], ':'); |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 91 | if (!event) { |
| 92 | ret = -EINVAL; |
| 93 | goto out; |
| 94 | } |
Masami Hiramatsu | 1ce25e9 | 2018-11-05 18:04:57 +0900 | [diff] [blame] | 95 | event++; |
| 96 | } |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 97 | |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 98 | p = strchr(event, '/'); |
| 99 | if (p) { |
| 100 | system = event; |
| 101 | event = p + 1; |
| 102 | *p = '\0'; |
| 103 | } |
Christophe JAILLET | 8db403b | 2021-04-11 12:21:54 +0200 | [diff] [blame] | 104 | if (event[0] == '\0') { |
| 105 | ret = -EINVAL; |
| 106 | goto out; |
| 107 | } |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 108 | |
| 109 | mutex_lock(&event_mutex); |
| 110 | for_each_dyn_event_safe(pos, n) { |
| 111 | if (type && type != pos->ops) |
| 112 | continue; |
Masami Hiramatsu | 3019913 | 2019-06-20 00:07:39 +0900 | [diff] [blame] | 113 | if (!pos->ops->match(system, event, |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 114 | argc - 1, (const char **)argv + 1, pos)) |
Masami Hiramatsu | cb8e7a8 | 2019-06-20 00:07:29 +0900 | [diff] [blame] | 115 | continue; |
| 116 | |
| 117 | ret = pos->ops->free(pos); |
| 118 | if (ret) |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 119 | break; |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 120 | } |
| 121 | mutex_unlock(&event_mutex); |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 122 | out: |
| 123 | argv_free(argv); |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 124 | return ret; |
| 125 | } |
| 126 | |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 127 | static int create_dyn_event(const char *raw_command) |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 128 | { |
| 129 | struct dyn_event_operations *ops; |
Frank Rowand | 3dee10d | 2019-03-21 23:58:20 -0700 | [diff] [blame] | 130 | int ret = -ENODEV; |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 131 | |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 132 | if (raw_command[0] == '-' || raw_command[0] == '!') |
| 133 | return dyn_event_release(raw_command, NULL); |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 134 | |
| 135 | mutex_lock(&dyn_event_ops_mutex); |
| 136 | list_for_each_entry(ops, &dyn_event_ops_list, list) { |
Masami Hiramatsu | d262271 | 2021-02-01 13:48:11 -0600 | [diff] [blame] | 137 | ret = ops->create(raw_command); |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 138 | if (!ret || ret != -ECANCELED) |
| 139 | break; |
| 140 | } |
| 141 | mutex_unlock(&dyn_event_ops_mutex); |
| 142 | if (ret == -ECANCELED) |
| 143 | ret = -EINVAL; |
| 144 | |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | /* Protected by event_mutex */ |
| 149 | LIST_HEAD(dyn_event_list); |
| 150 | |
| 151 | void *dyn_event_seq_start(struct seq_file *m, loff_t *pos) |
| 152 | { |
| 153 | mutex_lock(&event_mutex); |
| 154 | return seq_list_start(&dyn_event_list, *pos); |
| 155 | } |
| 156 | |
| 157 | void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos) |
| 158 | { |
| 159 | return seq_list_next(v, &dyn_event_list, pos); |
| 160 | } |
| 161 | |
| 162 | void dyn_event_seq_stop(struct seq_file *m, void *v) |
| 163 | { |
| 164 | mutex_unlock(&event_mutex); |
| 165 | } |
| 166 | |
| 167 | static int dyn_event_seq_show(struct seq_file *m, void *v) |
| 168 | { |
| 169 | struct dyn_event *ev = v; |
| 170 | |
| 171 | if (ev && ev->ops) |
| 172 | return ev->ops->show(m, ev); |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static const struct seq_operations dyn_event_seq_op = { |
| 178 | .start = dyn_event_seq_start, |
| 179 | .next = dyn_event_seq_next, |
| 180 | .stop = dyn_event_seq_stop, |
| 181 | .show = dyn_event_seq_show |
| 182 | }; |
| 183 | |
| 184 | /* |
| 185 | * dyn_events_release_all - Release all specific events |
| 186 | * @type: the dyn_event_operations * which filters releasing events |
| 187 | * |
| 188 | * This releases all events which ->ops matches @type. If @type is NULL, |
| 189 | * all events are released. |
| 190 | * Return -EBUSY if any of them are in use, and return other errors when |
| 191 | * it failed to free the given event. Except for -EBUSY, event releasing |
| 192 | * process will be aborted at that point and there may be some other |
| 193 | * releasable events on the list. |
| 194 | */ |
| 195 | int dyn_events_release_all(struct dyn_event_operations *type) |
| 196 | { |
| 197 | struct dyn_event *ev, *tmp; |
| 198 | int ret = 0; |
| 199 | |
| 200 | mutex_lock(&event_mutex); |
| 201 | for_each_dyn_event(ev) { |
| 202 | if (type && ev->ops != type) |
| 203 | continue; |
| 204 | if (ev->ops->is_busy(ev)) { |
| 205 | ret = -EBUSY; |
| 206 | goto out; |
| 207 | } |
| 208 | } |
| 209 | for_each_dyn_event_safe(ev, tmp) { |
| 210 | if (type && ev->ops != type) |
| 211 | continue; |
| 212 | ret = ev->ops->free(ev); |
| 213 | if (ret) |
| 214 | break; |
| 215 | } |
| 216 | out: |
| 217 | mutex_unlock(&event_mutex); |
| 218 | |
| 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | static int dyn_event_open(struct inode *inode, struct file *file) |
| 223 | { |
| 224 | int ret; |
| 225 | |
Steven Rostedt (VMware) | 8530dec | 2019-10-11 17:39:57 -0400 | [diff] [blame] | 226 | ret = tracing_check_open_get_tr(NULL); |
| 227 | if (ret) |
| 228 | return ret; |
| 229 | |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 230 | if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { |
| 231 | ret = dyn_events_release_all(NULL); |
| 232 | if (ret < 0) |
| 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | return seq_open(file, &dyn_event_seq_op); |
| 237 | } |
| 238 | |
| 239 | static ssize_t dyn_event_write(struct file *file, const char __user *buffer, |
| 240 | size_t count, loff_t *ppos) |
| 241 | { |
| 242 | return trace_parse_run_command(file, buffer, count, ppos, |
| 243 | create_dyn_event); |
| 244 | } |
| 245 | |
| 246 | static const struct file_operations dynamic_events_ops = { |
| 247 | .owner = THIS_MODULE, |
| 248 | .open = dyn_event_open, |
| 249 | .read = seq_read, |
| 250 | .llseek = seq_lseek, |
| 251 | .release = seq_release, |
| 252 | .write = dyn_event_write, |
| 253 | }; |
| 254 | |
| 255 | /* Make a tracefs interface for controlling dynamic events */ |
| 256 | static __init int init_dynamic_event(void) |
| 257 | { |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 258 | struct dentry *entry; |
Wei Yang | 22c36b1 | 2020-07-12 09:10:36 +0800 | [diff] [blame] | 259 | int ret; |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 260 | |
Wei Yang | 22c36b1 | 2020-07-12 09:10:36 +0800 | [diff] [blame] | 261 | ret = tracing_init_dentry(); |
| 262 | if (ret) |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 263 | return 0; |
| 264 | |
Steven Rostedt (VMware) | 21ccc9c | 2021-08-18 11:24:51 -0400 | [diff] [blame] | 265 | entry = tracefs_create_file("dynamic_events", TRACE_MODE_WRITE, NULL, |
Masami Hiramatsu | 5448d44 | 2018-11-05 18:02:08 +0900 | [diff] [blame] | 266 | NULL, &dynamic_events_ops); |
| 267 | |
| 268 | /* Event list interface */ |
| 269 | if (!entry) |
| 270 | pr_warn("Could not create tracefs 'dynamic_events' entry\n"); |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | fs_initcall(init_dynamic_event); |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 275 | |
| 276 | /** |
| 277 | * dynevent_arg_add - Add an arg to a dynevent_cmd |
| 278 | * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd |
| 279 | * @arg: The argument to append to the current cmd |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 280 | * @check_arg: An (optional) pointer to a function checking arg sanity |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 281 | * |
| 282 | * Append an argument to a dynevent_cmd. The argument string will be |
| 283 | * appended to the current cmd string, followed by a separator, if |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 284 | * applicable. Before the argument is added, the @check_arg function, |
| 285 | * if present, will be used to check the sanity of the current arg |
| 286 | * string. |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 287 | * |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 288 | * The cmd string and separator should be set using the |
| 289 | * dynevent_arg_init() before any arguments are added using this |
| 290 | * function. |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 291 | * |
| 292 | * Return: 0 if successful, error otherwise. |
| 293 | */ |
| 294 | int dynevent_arg_add(struct dynevent_cmd *cmd, |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 295 | struct dynevent_arg *arg, |
| 296 | dynevent_check_arg_fn_t check_arg) |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 297 | { |
| 298 | int ret = 0; |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 299 | |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 300 | if (check_arg) { |
| 301 | ret = check_arg(arg); |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 302 | if (ret) |
| 303 | return ret; |
| 304 | } |
| 305 | |
Tom Zanussi | 2b90927 | 2020-01-31 15:55:34 -0600 | [diff] [blame] | 306 | ret = seq_buf_printf(&cmd->seq, " %s%c", arg->str, arg->separator); |
| 307 | if (ret) { |
| 308 | pr_err("String is too long: %s%c\n", arg->str, arg->separator); |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 309 | return -E2BIG; |
| 310 | } |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 311 | |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * dynevent_arg_pair_add - Add an arg pair to a dynevent_cmd |
| 317 | * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd |
| 318 | * @arg_pair: The argument pair to append to the current cmd |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 319 | * @check_arg: An (optional) pointer to a function checking arg sanity |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 320 | * |
| 321 | * Append an argument pair to a dynevent_cmd. An argument pair |
| 322 | * consists of a left-hand-side argument and a right-hand-side |
| 323 | * argument separated by an operator, which can be whitespace, all |
| 324 | * followed by a separator, if applicable. This can be used to add |
| 325 | * arguments of the form 'type variable_name;' or 'x+y'. |
| 326 | * |
| 327 | * The lhs argument string will be appended to the current cmd string, |
Qiujun Huang | 2b5894c | 2020-10-29 23:05:54 +0800 | [diff] [blame] | 328 | * followed by an operator, if applicable, followed by the rhs string, |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 329 | * followed finally by a separator, if applicable. Before the |
| 330 | * argument is added, the @check_arg function, if present, will be |
| 331 | * used to check the sanity of the current arg strings. |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 332 | * |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 333 | * The cmd strings, operator, and separator should be set using the |
| 334 | * dynevent_arg_pair_init() before any arguments are added using this |
| 335 | * function. |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 336 | * |
| 337 | * Return: 0 if successful, error otherwise. |
| 338 | */ |
| 339 | int dynevent_arg_pair_add(struct dynevent_cmd *cmd, |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 340 | struct dynevent_arg_pair *arg_pair, |
| 341 | dynevent_check_arg_fn_t check_arg) |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 342 | { |
| 343 | int ret = 0; |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 344 | |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 345 | if (check_arg) { |
| 346 | ret = check_arg(arg_pair); |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 347 | if (ret) |
| 348 | return ret; |
| 349 | } |
| 350 | |
Tom Zanussi | 2b90927 | 2020-01-31 15:55:34 -0600 | [diff] [blame] | 351 | ret = seq_buf_printf(&cmd->seq, " %s%c%s%c", arg_pair->lhs, |
| 352 | arg_pair->operator, arg_pair->rhs, |
| 353 | arg_pair->separator); |
| 354 | if (ret) { |
| 355 | pr_err("field string is too long: %s%c%s%c\n", arg_pair->lhs, |
| 356 | arg_pair->operator, arg_pair->rhs, |
| 357 | arg_pair->separator); |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 358 | return -E2BIG; |
| 359 | } |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 360 | |
| 361 | return ret; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * dynevent_str_add - Add a string to a dynevent_cmd |
| 366 | * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd |
| 367 | * @str: The string to append to the current cmd |
| 368 | * |
| 369 | * Append a string to a dynevent_cmd. The string will be appended to |
| 370 | * the current cmd string as-is, with nothing prepended or appended. |
| 371 | * |
| 372 | * Return: 0 if successful, error otherwise. |
| 373 | */ |
| 374 | int dynevent_str_add(struct dynevent_cmd *cmd, const char *str) |
| 375 | { |
| 376 | int ret = 0; |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 377 | |
Tom Zanussi | 2b90927 | 2020-01-31 15:55:34 -0600 | [diff] [blame] | 378 | ret = seq_buf_puts(&cmd->seq, str); |
| 379 | if (ret) { |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 380 | pr_err("String is too long: %s\n", str); |
| 381 | return -E2BIG; |
| 382 | } |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 383 | |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * dynevent_cmd_init - Initialize a dynevent_cmd object |
| 389 | * @cmd: A pointer to the dynevent_cmd struct representing the cmd |
| 390 | * @buf: A pointer to the buffer to generate the command into |
| 391 | * @maxlen: The length of the buffer the command will be generated into |
| 392 | * @type: The type of the cmd, checked against further operations |
| 393 | * @run_command: The type-specific function that will actually run the command |
| 394 | * |
| 395 | * Initialize a dynevent_cmd. A dynevent_cmd is used to build up and |
| 396 | * run dynamic event creation commands, such as commands for creating |
| 397 | * synthetic and kprobe events. Before calling any of the functions |
| 398 | * used to build the command, a dynevent_cmd object should be |
| 399 | * instantiated and initialized using this function. |
| 400 | * |
| 401 | * The initialization sets things up by saving a pointer to the |
| 402 | * user-supplied buffer and its length via the @buf and @maxlen |
| 403 | * params, and by saving the cmd-specific @type and @run_command |
| 404 | * params which are used to check subsequent dynevent_cmd operations |
| 405 | * and actually run the command when complete. |
| 406 | */ |
| 407 | void dynevent_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen, |
| 408 | enum dynevent_type type, |
| 409 | dynevent_create_fn_t run_command) |
| 410 | { |
| 411 | memset(cmd, '\0', sizeof(*cmd)); |
| 412 | |
Tom Zanussi | 2b90927 | 2020-01-31 15:55:34 -0600 | [diff] [blame] | 413 | seq_buf_init(&cmd->seq, buf, maxlen); |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 414 | cmd->type = type; |
| 415 | cmd->run_command = run_command; |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * dynevent_arg_init - Initialize a dynevent_arg object |
| 420 | * @arg: A pointer to the dynevent_arg struct representing the arg |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 421 | * @separator: An (optional) separator, appended after adding the arg |
| 422 | * |
| 423 | * Initialize a dynevent_arg object. A dynevent_arg represents an |
| 424 | * object used to append single arguments to the current command |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 425 | * string. After the arg string is successfully appended to the |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 426 | * command string, the optional @separator is appended. If no |
| 427 | * separator was specified when initializing the arg, a space will be |
| 428 | * appended. |
| 429 | */ |
| 430 | void dynevent_arg_init(struct dynevent_arg *arg, |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 431 | char separator) |
| 432 | { |
| 433 | memset(arg, '\0', sizeof(*arg)); |
| 434 | |
| 435 | if (!separator) |
| 436 | separator = ' '; |
| 437 | arg->separator = separator; |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /** |
| 441 | * dynevent_arg_pair_init - Initialize a dynevent_arg_pair object |
| 442 | * @arg_pair: A pointer to the dynevent_arg_pair struct representing the arg |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 443 | * @operator: An (optional) operator, appended after adding the first arg |
| 444 | * @separator: An (optional) separator, appended after adding the second arg |
| 445 | * |
| 446 | * Initialize a dynevent_arg_pair object. A dynevent_arg_pair |
| 447 | * represents an object used to append argument pairs such as 'type |
| 448 | * variable_name;' or 'x+y' to the current command string. An |
| 449 | * argument pair consists of a left-hand-side argument and a |
| 450 | * right-hand-side argument separated by an operator, which can be |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 451 | * whitespace, all followed by a separator, if applicable. After the |
| 452 | * first arg string is successfully appended to the command string, |
| 453 | * the optional @operator is appended, followed by the second arg and |
Randy Dunlap | 5c8c206 | 2020-08-06 20:32:59 -0700 | [diff] [blame] | 454 | * optional @separator. If no separator was specified when |
Tom Zanussi | 74403b6 | 2020-01-31 15:55:32 -0600 | [diff] [blame] | 455 | * initializing the arg, a space will be appended. |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 456 | */ |
| 457 | void dynevent_arg_pair_init(struct dynevent_arg_pair *arg_pair, |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 458 | char operator, char separator) |
| 459 | { |
| 460 | memset(arg_pair, '\0', sizeof(*arg_pair)); |
| 461 | |
| 462 | if (!operator) |
| 463 | operator = ' '; |
| 464 | arg_pair->operator = operator; |
| 465 | |
| 466 | if (!separator) |
| 467 | separator = ' '; |
| 468 | arg_pair->separator = separator; |
Tom Zanussi | 86c5426 | 2020-01-29 12:59:24 -0600 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /** |
| 472 | * dynevent_create - Create the dynamic event contained in dynevent_cmd |
| 473 | * @cmd: The dynevent_cmd object containing the dynamic event creation command |
| 474 | * |
| 475 | * Once a dynevent_cmd object has been successfully built up via the |
| 476 | * dynevent_cmd_init(), dynevent_arg_add() and dynevent_arg_pair_add() |
| 477 | * functions, this function runs the final command to actually create |
| 478 | * the event. |
| 479 | * |
| 480 | * Return: 0 if the event was successfully created, error otherwise. |
| 481 | */ |
| 482 | int dynevent_create(struct dynevent_cmd *cmd) |
| 483 | { |
| 484 | return cmd->run_command(cmd); |
| 485 | } |
| 486 | EXPORT_SYMBOL_GPL(dynevent_create); |