forked from jcnelson/vdev
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.cpp
More file actions
Latest commit
772 lines (549 loc) · 18.4 KB
/
Copy pathaction.cpp
File metadata and controls
772 lines (549 loc) · 18.4 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
/*
vdev: a virtual device manager for *nix
Copyright (C) 2014 Jude Nelson
This program is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 or later as
published by the Free Software Foundation. For the terms of this
license, see LICENSE.LGPLv3+ or <http://www.gnu.org/licenses/>.
You are free to use this program under the terms of the GNU General
Public License, 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.
Alternatively, you are free to use this program under the terms of the
Internet Software Consortium License, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the terms of this license, see LICENSE.ISC or
<http://www.isc.org/downloads/software-support-policy/isc-license/>.
*/
#include"action.h"
#include"match.h"
#defineINI_MAX_LINE4096
#defineINI_STOP_ON_FIRST_ERROR1
#include"ini.h"
// initialize an action
intvdev_action_init( structvdev_action* act, vdev_device_request_t trigger, char* path, char* command, bool async ) {
int rc = 0;
memset( act, 0, sizeof(structvdev_action) );
act->dev_params = new (nothrow) vdev_device_params_t();
if( act->dev_params == NULL ) {
return -ENOMEM;
}
act->trigger = trigger;
act->path = vdev_strdup_or_null( path );
act->command = vdev_strdup_or_null( command );
act->async = async;
if( act->path != NULL ) {
rc = vdev_match_regex_init( &act->path_regex, path );
if( rc != 0 ) {
vdev_error("vdev_match_regex_init('%s') rc = %d\n", path );
vdev_action_free( act );
return rc;
}
}
return0;
}
// add a device parameter to match on
intvdev_action_add_param( structvdev_action* act, charconst* name, charconst* value ) {
try {
(*act->dev_params)[ string(name) ] = string(value);
return0;
}
catch( bad_alloc& ba ) {
return -ENOMEM;
}
}
// free an action
intvdev_action_free( structvdev_action* act ) {
if( act->command != NULL ) {
free( act->command );
act->command = NULL;
}
if( act->rename_command != NULL ) {
free( act->rename_command );
act->rename_command = NULL;
}
if( act->dev_params != NULL ) {
delete act->dev_params;
act->dev_params = NULL;
}
if( act->path != NULL ) {
free( act->path );
act->path = NULL;
regfree( &act->path_regex );
}
return0;
}
// convert an action into a device request
vdev_device_request_tvdev_action_parse_trigger( charconst* type ) {
if( strcmp(type, VDEV_ACTION_EVENT_ADD) == 0 ) {
returnVDEV_DEVICE_ADD;
}
elseif( strcmp(type, VDEV_ACTION_EVENT_REMOVE) == 0 ) {
returnVDEV_DEVICE_REMOVE;
}
elseif( strcmp(type, VDEV_ACTION_EVENT_ANY) == 0 ) {
returnVDEV_DEVICE_ANY;
}
returnVDEV_DEVICE_INVALID;
}
// parse an action from an ini file
// return 1 for parsed
// return 0 for not parsed
staticintvdev_action_ini_parser( void* userdata, charconst* section, charconst* name, charconst* value ) {
structvdev_action* act = (structvdev_action*)userdata;
int rc = 0;
if( strcmp(section, VDEV_ACTION_NAME) == 0 ) {
if( strcmp(name, VDEV_ACTION_NAME_PATH) == 0 ) {
// path?
if( act->path != NULL ) {
free( act->path );
}
act->path = vdev_strdup_or_null( value );
if( act->path == NULL ) {
// EOM
return0;
}
else {
rc = vdev_match_regex_init( &act->path_regex, act->path );
if( rc != 0 ) {
vdev_error("vdev_match_regex_init('%s') rc = %d\n", act->path );
return0;
}
}
return1;
}
if( strcmp(name, VDEV_ACTION_NAME_RENAME) == 0 ) {
// rename command
if( act->rename_command != NULL ) {
free( act->rename_command );
}
act->rename_command = vdev_strdup_or_null( value );
return1;
}
if( strcmp(name, VDEV_ACTION_NAME_EVENT) == 0 ) {
// event?
act->trigger = vdev_action_parse_trigger( value );
if( act->trigger == VDEV_DEVICE_INVALID ) {
fprintf(stderr, "Invalid event type '%s'\n", value);
return0;
}
else {
return1;
}
}
if( strcmp(name, VDEV_ACTION_NAME_SHELL) == 0 ) {
// shell command
if( act->command != NULL ) {
free( act->command );
}
act->command = vdev_strdup_or_null( value );
return1;
}
if( strcmp(name, VDEV_ACTION_NAME_ASYNC) == 0 ) {
// async?
if( strcasecmp(value, "true") == 0 || strcmp(value, "1") == 0 ) {
act->async = true;
return1;
}
elseif( strcasecmp(value, "false") == 0 || strcmp(value, "0") == 0 ) {
act->async = false;
return1;
}
else {
fprintf(stderr, "Invalid async value '%s'\n", value);
return0;
}
}
if( strncmp(name, VDEV_ACTION_NAME_OS_PREFIX, strlen(VDEV_ACTION_NAME_OS_PREFIX)) == 0 ) {
// OS-specific param
rc = vdev_action_add_param( act, name + strlen(VDEV_ACTION_NAME_OS_PREFIX), value );
if( rc == 0 ) {
return1;
}
else {
vdev_error("vdev_action_add_param( '%s', '%s' ) rc = %d\n", name, value, rc );
return0;
}
}
fprintf(stderr, "Unknown field '%s' in section '%s'\n", name, section );
return0;
}
fprintf(stderr, "Unknown section '%s'\n", section);
return0;
}
// sanity check an action
// return 0 on success
// return negative on invalid
intvdev_action_sanity_check( structvdev_action* act ) {
int rc = 0;
if( act->command == NULL && act->rename_command == NULL ) {
fprintf(stderr, "Action is missing 'command=' or 'rename_command='\n");
rc = -EINVAL;
}
if( act->trigger == VDEV_DEVICE_INVALID ) {
fprintf(stderr, "Action is missing 'event='\n");
rc = -EINVAL;
}
return rc;
}
// load an action from a path
intvdev_action_load( charconst* path, structvdev_action* act ) {
int rc = 0;
FILE* f = NULL;
rc = vdev_action_init( act, VDEV_DEVICE_INVALID, NULL, NULL, false );
if( rc != 0 ) {
vdev_error("vdev_action_init('%s') rc = %d\n", path );
return rc;
}
f = fopen( path, "r" );
if( f == NULL ) {
vdev_action_free( act );
rc = -errno;
return rc;
}
rc = vdev_action_load_file( f, act );
fclose( f );
if( rc == -EINVAL ) {
vdev_action_free( act );
vdev_error("Invalid action '%s'\n", path );
}
return rc;
}
// load an action from a file
intvdev_action_load_file( FILE* f, structvdev_action* act ) {
int rc = 0;
rc = ini_parse_file( f, vdev_action_ini_parser, act );
if( rc != 0 ) {
vdev_error("ini_parse_file(action) rc = %d\n", rc );
}
else {
rc = vdev_action_sanity_check( act );
}
return rc;
}
// free a vector of actions
staticvoidvdev_action_free_vector( vector<structvdev_action>* acts ) {
for( unsignedint i = 0; i < acts->size(); i++ ) {
vdev_action_free( &acts->at(i) );
}
acts->clear();
}
// free a C-style list of actions (including the list itself)
intvdev_action_free_all( structvdev_action* act_list, size_t num_acts ) {
int rc = 0;
for( unsignedint i = 0; i < num_acts; i++ ) {
rc = vdev_action_free( &act_list[i] );
if( rc != 0 ) {
return rc;
}
}
free( act_list );
return rc;
}
// action loader
intvdev_action_loader( charconst* path, void* cls ) {
int rc = 0;
structvdev_action act;
structstat sb;
// skip if not a regular file
rc = stat( path, &sb );
if( rc != 0 ) {
rc = -errno;
vdev_error("stat(%s) rc = %d\n", path, rc );
return rc;
}
if( !S_ISREG( sb.st_mode ) ) {
return0;
}
vdev_debug("Load Action %s\n", path );
vector<structvdev_action>* acts = (vector<structvdev_action>*)cls;
memset( &act, 0, sizeof(structvdev_action) );
rc = vdev_action_load( path, &act );
if( rc != 0 ) {
vdev_error("vdev_acl_load(%s) rc = %d\n", path, rc );
return rc;
}
// save this action
try {
acts->push_back( act );
}
catch( bad_alloc& ba ) {
return -ENOMEM;
}
return0;
}
// load all actions in a directory
// return 0 on success
// return negative on error
intvdev_action_load_all( charconst* dir_path, structvdev_action** ret_acts, size_t* ret_num_acts ) {
int rc = 0;
vector<structvdev_action> acts;
rc = vdev_load_all( dir_path, vdev_action_loader, &acts );
if( rc != 0 ) {
vdev_action_free_vector( &acts );
return rc;
}
else {
if( acts.size() == 0 ) {
// nothing
*ret_acts = NULL;
*ret_num_acts = 0;
}
else {
// extract values
structvdev_action* act_list = VDEV_CALLOC( structvdev_action, acts.size() );
if( act_list == NULL ) {
vdev_action_free_vector( &acts );
return -ENOMEM;
}
// NOTE: vectors are contiguous in memory
memcpy( act_list, &acts[0], sizeof(structvdev_action) * acts.size() );
*ret_acts = act_list;
*ret_num_acts = acts.size();
}
}
return0;
}
// carry out a command, synchronously, using an environment given by vreq.
// return the exit status on success (non-negative)
// return negative on error
intvdev_action_run_sync( structvdev_device_request* vreq, charconst* command, char** output, size_t max_output ) {
int rc = 0;
int exit_status = 0;
char** req_env = NULL;
size_t num_env = 0;
// convert to environment variables
rc = vdev_device_request_to_env( vreq, &req_env, &num_env );
if( rc != 0 ) {
vdev_error("vdev_device_request_to_env(%s) rc = %d\n", vreq->path, rc );
return rc;
}
vdev_debug("run command: '%s'\n", command );
for( unsignedint i = 0; i < num_env; i++ ) {
vdev_debug("command env: '%s'\n", req_env[i] );
}
rc = vdev_subprocess( command, req_env, output, max_output, &exit_status );
if( output != NULL && *output != NULL ) {
vdev_debug("command output: '%s'\n", *output );
}
VDEV_FREE_LIST( req_env );
if( rc != 0 ) {
vdev_error("vdev_subprocess('%s') rc = %d\n", command, rc );
return rc;
}
if( exit_status != 0 ) {
vdev_error("vdev_subprocess('%s') exit status = %d\n", command, exit_status );
}
return exit_status;
}
// carry out an action, asynchronously.
// return 0 if we were able to fork
intvdev_action_run_async( structvdev_device_request* req, charconst* command ) {
int rc = 0;
pid_t pid = 0;
int max_fd = 0;
pid = fork();
if( pid == 0 ) {
// child
max_fd = sysconf(_SC_OPEN_MAX);
// close everything
for( int i = 0; i < max_fd; i++ ) {
close( i );
}
clearenv();
// generate the environment
char** env = NULL;
size_t num_env = 0;
rc = vdev_device_request_to_env( req, &env, &num_env );
if( rc != 0 ) {
vdev_error("vdev_device_request_to_env('%s') rc = %d\n", req->path, rc );
exit(1);
}
// run the command
execle( "/bin/sh", "sh", "-c", command, (char*)0, env );
// keep gcc happy
return0;
}
elseif( pid > 0 ) {
// parent; succeeded
return0;
}
else {
// error
rc = -errno;
vdev_error("fork() rc = %d\n", rc);
return rc;
}
}
// match device request against action
// return 1 if match
// return 0 if not match
intvdev_action_match( structvdev_device_request* vreq, structvdev_action* act ) {
int rc = 0;
// action match?
if( act->trigger != vreq->type && act->trigger != VDEV_DEVICE_ANY ) {
return0;
}
// path match?
if( act->path != NULL ) {
rc = vdev_match_regex( vreq->path, &act->path_regex );
if( rc == 0 ) {
// no match
return0;
}
if( rc < 0 ) {
// some error
return rc;
}
}
// OS parameter match?
if( act->dev_params != NULL ) {
for( vdev_device_params_t::iterator itr = act->dev_params->begin(); itr != act->dev_params->end(); itr++ ) {
const string& act_param_name = itr->first;
vdev_device_params_t::iterator vreq_itr = vreq->params->find( act_param_name );
if( vreq_itr != vreq->params->end() ) {
// vreq has this parameter
charconst* vreq_param_value = vreq_itr->second.c_str();
charconst* act_param_value = itr->second.c_str();
// if the action has no value (value of length 0), then it matches any vreq value
if( strlen( vreq_param_value ) == 0 ) {
continue;
}
// otherwise, compare the values
if( strcmp( vreq_param_value, act_param_value ) != 0 ) {
// values don't match
return0;
}
}
else {
// vreq does not have this parameter, so no match
return0;
}
}
}
// match!
return1;
}
// find the next action in a list of actions to run, given the path
// return the index into acts of the next match, if found
// return num_acts if not found
// return negative on error
intvdev_action_find_next( structvdev_device_request* vreq, structvdev_action* acts, size_t num_acts ) {
int rc = 0;
int i = 0;
for( i = 0; (unsigned)i < num_acts; i++ ) {
rc = vdev_action_match( vreq, &acts[i] );
if( rc > 0 ) {
return i;
}
elseif( rc < 0 ) {
return rc;
}
}
return num_acts;
}
// find the path to create for the given device request.
// *path will be filled in with path to the device node, relative to the mountpoint. *path must be NULL on call.
// return 0 on success
// return negative on failure
intvdev_action_create_path( structvdev_device_request* vreq, structvdev_action* acts, size_t num_acts, char** path ) {
int rc = 0;
int act_offset = 0;
int i = 0;
char* new_path = NULL;
if( *path != NULL ) {
return -EINVAL;
}
while( act_offset < (signed)num_acts ) {
// skip this action if there is no rename command
if( acts[act_offset].rename_command == NULL ) {
act_offset++;
continue;
}
// find the next action that matches this path
rc = vdev_action_find_next( vreq, acts + act_offset, num_acts - act_offset );
if( rc == (signed)(num_acts - act_offset) ) {
// not found
rc = 0;
break;
}
elseif( rc < 0 ) {
// error
vdev_error("vdev_action_find_next(%s, offset = %d) rc = %d\n", vreq->path, act_offset, rc );
break;
}
else {
// matched! advance offset to next action
i = act_offset + rc;
act_offset += rc + 1;
if( acts[i].rename_command == NULL ) {
continue;
}
// generate the new name
rc = vdev_action_run_sync( vreq, acts[i].rename_command, &new_path, PATH_MAX + 1 );
if( rc < 0 ) {
vdev_error("vdev_action_run_sync('%s') rc = %d\n", acts[i].rename_command, rc );
break;
}
else {
if( *path != NULL ) {
free( *path );
}
*path = new_path;
new_path = NULL;
}
}
rc = 0;
}
return rc;
}
// run all actions for a device, sequentially, in lexographic order
// return 0 on success
// return negative on failure
intvdev_action_run_commands( structvdev_device_request* vreq, structvdev_action* acts, size_t num_acts ) {
int rc = 0;
int act_offset = 0;
int i = 0;
charconst* method = NULL;
while( act_offset < (signed)num_acts ) {
// skip this action if there is no command
if( acts[act_offset].command == NULL ) {
act_offset++;
continue;
}
// find the next action that matches this path
rc = vdev_action_find_next( vreq, acts + act_offset, num_acts - act_offset );
if( rc == (signed)(num_acts - act_offset) ) {
// not found
rc = 0;
break;
}
elseif( rc < 0 ) {
vdev_error("vdev_action_find_next(%s, offset = %d) rc = %d\n", vreq->path, act_offset, rc );
break;
}
else {
// matched! advance offset to next action
i = act_offset + rc;
act_offset += rc + 1;
rc = 0;
if( acts[i].command == NULL ) {
continue;
}
// what kind of action?
if( acts[i].async ) {
method = "vdev_action_run_async";
rc = vdev_action_run_async( vreq, acts[i].command );
}
else {
method = "vdev_action_run_sync";
rc = vdev_action_run_sync( vreq, acts[i].command, NULL, 0 );
}
if( rc != 0 ) {
vdev_error("%s('%s') rc = %d\n", method, acts[i].command, rc );
return rc;
}
}
}
return rc;
}