summaryrefslogtreecommitdiffstats
path: root/fish/events.c
blob: b98bd589fac5008105f6b5c777c9cf3b1706698e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* guestfish - the filesystem interactive shell
 * Copyright (C) 2011 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
#include <libintl.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <guestfs.h>

#include "hash.h"
#include "hash-pjw.h"

#include "fish.h"

/* The hash table maps names to multiple (linked list of) event handlers. */
static Hash_table *event_handlers;

struct entry {
  struct entry *next;           /* Next entry in linked list. */
  char *name;                   /* Event name. */
  char *command;                /* Shell script / command that runs. */
  uint64_t event_bitmask;       /* Events this is registered for. */
  int eh;        /* Event handle (from guestfs_set_event_callback). */
};

static void
entry_free (void *x)
{
  if (x) {
    struct entry *p = x;
    entry_free (p->next);
    free (p->name);
    free (p->command);
    free (p);
  }
}

static size_t
entry_hash (void const *x, size_t table_size)
{
  struct entry const *p = x;
  return hash_pjw (p->name, table_size);
}

static bool
entry_compare (void const *x, void const *y)
{
  struct entry const *p = x;
  struct entry const *q = y;
  return STREQ (p->name, q->name);
}

void
init_event_handlers (void)
{
  assert (event_handlers == NULL);
  event_handlers =
    hash_initialize (64, NULL, entry_hash, entry_compare, entry_free);
}

void
free_event_handlers (void)
{
  assert (event_handlers != NULL);
  hash_free (event_handlers);
  event_handlers = NULL;
}

static void
do_event_handler (guestfs_h *g,
                  void *opaque,
                  uint64_t event,
                  int event_handle,
                  int flags,
                  const char *buf, size_t buf_len,
                  const uint64_t *array, size_t array_len)
{
  pid_t pid;
  const char *argv[8 + array_len];
  const char *shell;
  struct entry *entry = opaque;
  size_t i, j;
  char *s;

  pid = fork ();
  if (pid == -1) {
    perror ("event handler: fork");
    return;
  }

  if (pid == 0) {               /* Child process. */
    shell = getenv ("SHELL");
    if (!shell)
      shell = "/bin/sh";

    setenv ("EVENT", event_name_of_event_bitmask (event), 1);

    /* Construct the command and arguments. */
    i = 0;
    argv[i++] = shell;
    argv[i++] = "-c";
    argv[i++] = entry->command;
    argv[i++] = ""; /* $0 */

    if (buf != NULL)
      /* XXX: So far, buf is always ASCII NUL-terminated.  There is no
       * way to pass arbitrary 8 bit buffers.
       */
      argv[i++] = buf;

    for (j = 0; j < array_len; ++j) {
      if (asprintf (&s, "%" PRIu64, array[j]) == -1) {
        perror ("event handler: asprintf");
        _exit (EXIT_FAILURE);
      }
      argv[i++] = s;
    }

    argv[i++] = NULL;

    execvp (argv[0], (void *) argv);
    perror (argv[0]);

    _exit (EXIT_FAILURE);
  }

  if (waitpid (pid, NULL, 0) == -1)
    perror ("event handler: waitpid");
}

int
run_event (const char *cmd, size_t argc, char *argv[])
{
  int r;
  struct entry *entry = NULL, *old_entry;

  if (argc != 3) {
    fprintf (stderr,
             _("use 'event <name> <eventset> <script>' to register an event handler\n"));
    goto error;
  }

  entry = calloc (1, sizeof *entry);
  if (entry == NULL) {
    perror ("calloc");
    goto error;
  }
  entry->eh = -1;

  r = event_bitmask_of_event_set (argv[1], &entry->event_bitmask);
  if (r == -1)
    goto error;

  entry->name = strdup (argv[0]);
  if (entry->name == NULL) {
    perror ("strdup");
    goto error;
  }
  entry->command = strdup (argv[2]);
  if (entry->command == NULL) {
    perror ("strdup");
    goto error;
  }

  entry->eh =
    guestfs_set_event_callback (g, do_event_handler,
                                entry->event_bitmask, 0, entry);
  if (entry->eh == -1)
    goto error;

  r = hash_insert_if_absent (event_handlers, entry, (const void **) &old_entry);
  if (r == -1)
    goto error;
  if (r == 0) {                 /* old_entry set to existing entry */
    entry->next = old_entry->next;
    /* XXX are we allowed to update the old entry? */
    old_entry->next = entry;
  }

  return 0;

 error:
  if (entry) {
    if (entry->eh >= 0)
      guestfs_delete_event_callback (g, entry->eh);
    free (entry->name);
    free (entry->command);
    free (entry);
  }
  return -1;
}

int
run_delete_event (const char *cmd, size_t argc, char *argv[])
{
  if (argc != 1) {
    fprintf (stderr,
             _("use 'delete-event <name>' to delete an event handler\n"));
    return -1;
  }

  const struct entry key = { .name = bad_cast (argv[0]) };
  struct entry *entry, *p;

  entry = hash_delete (event_handlers, &key);
  if (!entry) {
    fprintf (stderr, _("delete-event: %s: no such event handler\n"), argv[0]);
    return -1;
  }

  /* Delete them from the handle. */
  p = entry;
  while (p) {
    guestfs_delete_event_callback (g, p->eh);
    p = p->next;
  }

  /* Free the structures. */
  entry_free (entry);

  return 0;
}

static bool
list_event (void *x, void *data)
{
  struct entry *entry = x;

  while (entry) {
    printf ("\"%s\" (%d): ", entry->name, entry->eh);
    print_event_set (entry->event_bitmask, stdout);
    printf (": %s\n", entry->command);
    entry = entry->next;
  }

  return 1;
}

int
run_list_events (const char *cmd, size_t argc, char *argv[])
{
  if (argc != 0) {
    fprintf (stderr,
             _("use 'list-events' to list event handlers\n"));
    return -1;
  }

  hash_do_for_each (event_handlers, list_event, NULL);
  return 0;
}