| 1 | /* |
| 2 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
| 3 | * Copyright 2017 Philippe Proulx <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | * of this software and associated documentation files (the "Software"), to deal |
| 7 | * in the Software without restriction, including without limitation the rights |
| 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | * copies of the Software, and to permit persons to whom the Software is |
| 10 | * furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included in |
| 13 | * all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | * SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include <stdbool.h> |
| 25 | #include <stdio.h> |
| 26 | #include <assert.h> |
| 27 | #include <babeltrace/babeltrace-internal.h> |
| 28 | #include <babeltrace/graph/component.h> |
| 29 | #include <babeltrace/graph/clock-class-priority-map.h> |
| 30 | #include <glib.h> |
| 31 | |
| 32 | struct dmesg_component; |
| 33 | |
| 34 | struct dmesg_notif_iter { |
| 35 | struct dmesg_component *dmesg_comp; |
| 36 | FILE *fp; |
| 37 | }; |
| 38 | |
| 39 | struct dmesg_component { |
| 40 | struct { |
| 41 | GString *path; |
| 42 | bool read_from_stdin; |
| 43 | } params; |
| 44 | |
| 45 | struct bt_ctf_packet *packet; |
| 46 | struct bt_ctf_event_class *event_class; |
| 47 | struct bt_ctf_stream *stream; |
| 48 | struct bt_clock_class_priority_map *cc_prio_map; |
| 49 | }; |
| 50 | |
| 51 | static |
| 52 | int check_params(struct dmesg_component *dmesg_comp, struct bt_value *params) |
| 53 | { |
| 54 | struct bt_value *read_from_stdin; |
| 55 | struct bt_value *path; |
| 56 | const char *path_str; |
| 57 | int ret = 0; |
| 58 | |
| 59 | if (!params || !bt_value_is_map(params)) { |
| 60 | fprintf(stderr, "Expecting a map value as parameters.\n"); |
| 61 | goto error; |
| 62 | } |
| 63 | |
| 64 | read_from_stdin = bt_value_map_get(params, "read-from-stdin"); |
| 65 | if (read_from_stdin) { |
| 66 | if (!bt_value_is_bool(read_from_stdin)) { |
| 67 | fprintf(stderr, "Expecting a boolean value for `read-from-stdin` parameter.\n"); |
| 68 | goto error; |
| 69 | } |
| 70 | |
| 71 | ret = bt_value_bool_get(read_from_stdin, |
| 72 | &dmesg_comp->params.read_from_stdin); |
| 73 | assert(ret == 0); |
| 74 | } |
| 75 | |
| 76 | path = bt_value_map_get(params, "path"); |
| 77 | if (path) { |
| 78 | if (dmesg_comp->params.read_from_stdin) { |
| 79 | fprintf(stderr, "Cannot specify both `read-from-stdin` and `path` parameters.\n"); |
| 80 | goto error; |
| 81 | } |
| 82 | |
| 83 | if (!bt_value_is_string(path)) { |
| 84 | fprintf(stderr, "Expecting a string value for `path` parameter.\n"); |
| 85 | goto error; |
| 86 | } |
| 87 | |
| 88 | ret = bt_value_bool_get(path, &path_str); |
| 89 | assert(ret == 0); |
| 90 | g_string_assign(dmesg_comp->params.path, path_str); |
| 91 | } else { |
| 92 | if (!dmesg_comp->params.read_from_stdin) { |
| 93 | fprintf(stderr, "Expecting `path` parameter or `read-from-stdin` parameter set to true.\n"); |
| 94 | goto error; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | goto end; |
| 99 | |
| 100 | error: |
| 101 | ret = -1; |
| 102 | |
| 103 | end: |
| 104 | bt_put(read_from_stdin); |
| 105 | bt_put(path); |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | static |
| 110 | int create_stream() |
| 111 | |
| 112 | static |
| 113 | void destroy_dmesg_component(struct dmesg_component *dmesg_comp) |
| 114 | { |
| 115 | if (!dmesg_comp) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | if (dmesg_comp->params.path) { |
| 120 | g_string_free(dmesg_comp->params.path, TRUE); |
| 121 | } |
| 122 | |
| 123 | bt_put(dmesg_comp->packet); |
| 124 | bt_put(dmesg_comp->event_class); |
| 125 | bt_put(dmesg_comp->stream); |
| 126 | bt_put(dmesg_comp->cc_prio_map); |
| 127 | g_free(dmesg_comp); |
| 128 | } |
| 129 | |
| 130 | BT_HIDDEN |
| 131 | enum bt_component_status dmesg_init(struct bt_private_component *priv_comp, |
| 132 | struct bt_value *params, void *init_method_data) |
| 133 | { |
| 134 | int ret = 0; |
| 135 | struct dmesg_component *dmesg_comp = g_new0(struct dmesg_component, 1); |
| 136 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; |
| 137 | |
| 138 | if (!dmesg_comp) { |
| 139 | goto error; |
| 140 | } |
| 141 | |
| 142 | dmesg_comp->params.path = g_string_new(NULL); |
| 143 | if (!dmesg_comp->params.path) { |
| 144 | goto error; |
| 145 | } |
| 146 | |
| 147 | ret = check_params(dmesg_comp, params); |
| 148 | if (ret) { |
| 149 | goto error; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | |
| 154 | goto end; |
| 155 | |
| 156 | error: |
| 157 | destroy_dmesg_component(dmesg_comp); |
| 158 | status = BT_COMPONENT_STATUS_ERROR; |
| 159 | |
| 160 | end: |
| 161 | return status; |
| 162 | } |
| 163 | |
| 164 | BT_HIDDEN |
| 165 | void dmesg_finalize(struct bt_private_component *priv_comp) |
| 166 | { |
| 167 | |
| 168 | } |
| 169 | |
| 170 | BT_HIDDEN |
| 171 | enum bt_notification_iterator_status dmesg_notif_iter_init( |
| 172 | struct bt_private_notification_iterator *priv_notif_iter, |
| 173 | struct bt_private_port *priv_port) |
| 174 | { |
| 175 | |
| 176 | } |
| 177 | |
| 178 | BT_HIDDEN |
| 179 | void dmesg_iterator_finalize( |
| 180 | struct bt_private_notification_iterator *priv_notif_iter) |
| 181 | { |
| 182 | |
| 183 | } |
| 184 | |
| 185 | BT_HIDDEN |
| 186 | struct bt_notification_iterator_next_return dmesg_notif_iter_next( |
| 187 | struct bt_private_notification_iterator *priv_notif_iter) |
| 188 | { |
| 189 | |
| 190 | } |