[go: up one dir, main page]

Menu

[r100]: / trunk / sane.c  Maximize  Restore  History

Download this file

1359 lines (1233 with data), 55.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
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
/*
* $Id$
*
* scanbd - KMUX scanner button daemon
*
* Copyright (C) 2008 - 2012 Wilhelm Meier (wilhelm.meier@fh-kl.de)
*
* 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 "scanbd.h"
#include "scanbd_dbus.h"
// all programm-global sane functions use this mutex to avoid races
#ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
// this is non-portable
static pthread_mutex_t sane_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
#else
static pthread_mutex_t sane_mutex;
#endif
static pthread_cond_t sane_cv = PTHREAD_COND_INITIALIZER;
// the following locking strategie must be obeyed:
// 1) lock the sane_mutex
// 2) lock the device specific mutex
// in this order to avoid deadlocks
// holding more than these two locks is not intended
#ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
void sane_init_mutex()
{
slog(SLOG_INFO, "sane_init_mutex");
pthread_mutexattr_t mutexattr;
if (pthread_mutexattr_init(&mutexattr) < 0) {
slog(SLOG_ERROR, "Can't initialize mutex attr");
exit(EXIT_FAILURE);
}
if (pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE) < 0) {
slog(SLOG_ERROR, "Can't set mutex attr");
exit(EXIT_FAILURE);
}
if (pthread_mutex_init(&sane_mutex, &mutexattr) < 0) {
slog(SLOG_ERROR, "Can't init mutex");
exit(EXIT_FAILURE);
}
}
#endif
struct sane_opt_value {
unsigned long num_value; // before-value or after-value or actual-value (BOOL|INT|FIXED)
struct { // (STRING)
char* str; // actual-value
regex_t* reg; // before-regex or after-regex
} str_value;
};
typedef struct sane_opt_value sane_opt_value_t;
struct sane_dev_option {
int number; // the option-number of the device-option
sane_opt_value_t from_value; // the before-value of the option
sane_opt_value_t to_value; // the after-value of the option (to
// fire the trigger)
sane_opt_value_t value; // the option value (from the last
// polling cycle)
const char* script; // the found (matched) script to be called if
// the option-valued changes
const char* action_name; // the name of this action as
// specified in the config file
};
typedef struct sane_dev_option sane_dev_option_t;
struct sane_dev_function {
int number; // the option-number of the
// device-option
const char* env; // the name of the environment-var to
// pass to option value in
};
typedef struct sane_dev_function sane_dev_function_t;
// each polling thread is represented by struct sane_thread
// there is no locking, since this is "thread private data"
struct sane_thread {
pthread_t tid; // the thread-id of the polling
// thread
pthread_mutex_t mutex; // mutex for this data-structure
pthread_cond_t cv; // cv for this data-structure
bool triggered; // a rule for this device has fired (triggered == true)
int triggered_option; // the action number which triggered
const SANE_Device* dev; // the device
int num_of_options; // the number of all options for
// this device
SANE_Handle h; // the handle of the opened device
sane_dev_option_t *opts; // the list of matched actions
// for this device
int num_of_options_with_scripts; // the number of elements in the
// above list
sane_dev_function_t *functions; // the list of matched functions
// for this device
int num_of_options_with_functions;// the number of elements in the
// above list
};
typedef struct sane_thread sane_thread_t;
// the list of all polling threads
static sane_thread_t* sane_poll_threads = NULL;
// the list of all devices locally connected to our system
static const SANE_Device** sane_device_list = NULL;
// the number of devices = the number of polling threads
static int num_devices = 0;
void get_sane_devices(void) {
// detect all the scanners we have
slog(SLOG_INFO, "Scanning for local-only devices" );
if (pthread_mutex_lock(&sane_mutex) < 0) {
// if we can't get the mutex, something is heavily wrong!
slog(SLOG_ERROR, "pthread_mutex_lock: %s", strerror(errno));
return;
}
SANE_Status sane_status = 0;
sane_device_list = NULL;
num_devices = 0;
if ((sane_status = sane_get_devices(&sane_device_list, SANE_TRUE)) != SANE_STATUS_GOOD) {
slog(SLOG_WARN, "Can't get the sane device list");
}
const SANE_Device** dev = sane_device_list;
if (dev == NULL) {
slog(SLOG_DEBUG, "device list null");
goto cleanup;
}
while(*dev != NULL) {
slog(SLOG_DEBUG, "found device: %s %s %s %s",
(*dev)->name, (*dev)->vendor, (*dev)->model, (*dev)->type);
num_devices += 1;
dev++;
}
if (pthread_cond_broadcast(&sane_cv)) {
slog(SLOG_ERROR, "pthread_cond_broadcast: %s", strerror(errno));
}
cleanup:
if (pthread_mutex_unlock(&sane_mutex) < 0) {
// if we can't unlock the mutex, something is heavily wrong!
slog(SLOG_ERROR, "pthread_mutex_unlock: %s", strerror(errno));
return;
}
}
// simple hash function for C-strings
static unsigned long hash(const char *str) {
unsigned long hash = 5381;
int c;
while ((c = *str++)) {
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}
return hash;
}
static void sane_option_value_init(sane_opt_value_t* v) {
v->num_value = 0;
v->str_value.str = NULL;
v->str_value.reg = NULL;
}
static void sane_option_value_free(sane_opt_value_t* v) {
if (v->str_value.str != NULL) {
free((void*)v->str_value.str);
v->str_value.str = NULL;
}
if (v->str_value.reg != NULL) {
regfree(v->str_value.reg);
free(v->str_value.reg);
v->str_value.reg = NULL;
}
}
static sane_opt_value_t get_sane_option_value(SANE_Handle* h, int index) {
slog(SLOG_DEBUG, "get_sane_option_value");
// get the value of option with index of the device (opened) with
// handle h
// if option can't be found or other catastrophy happens, the
// value 0 gets returned
sane_opt_value_t res;
sane_option_value_init(&res);
const SANE_Option_Descriptor* odesc = NULL;
if ((odesc = sane_get_option_descriptor(h, index)) == NULL) {
return res;
}
if ((odesc->type == SANE_TYPE_BOOL) || (odesc->type == SANE_TYPE_INT) ||
(odesc->type == SANE_TYPE_FIXED) || (odesc->type == SANE_TYPE_BUTTON)) {
unsigned long int value = 0;
if ((unsigned int)odesc->size <= sizeof(long int)) {
//if we can store it in an long int
SANE_Status status;
if ((status = sane_control_option(h, index, SANE_ACTION_GET_VALUE,
&value, NULL)) != SANE_STATUS_GOOD) {
slog(SLOG_WARN, "Can't read value of %s: %s",
odesc->name, sane_strstatus(status));
return res;
}
res.num_value = value;
return res;
}
else {
// shouldn't happen
slog(SLOG_WARN, "Value of %s, sane-type %d too big", odesc->name, odesc->type);
return res;
}
}
else if (odesc->type == SANE_TYPE_STRING) {
res.str_value.str = calloc(odesc->size + 1, sizeof(char));
assert(res.str_value.str != NULL);
SANE_Status status;
if ((status = sane_control_option(h, index, SANE_ACTION_GET_VALUE,
res.str_value.str, NULL)) != SANE_STATUS_GOOD) {
slog(SLOG_WARN, "Can't read value of %s: %s", odesc->name, sane_strstatus(status));
return res;
}
res.str_value.str[odesc->size] = '\0';
size_t slen = strlen(res.str_value.str);
res.num_value = hash(res.str_value.str);
slog(SLOG_INFO, "Value of %s as string (len %d, hash %d): %s",
odesc->name, slen, res.num_value, res.str_value.str);
return res;
}
else {
slog(SLOG_WARN, "Can't read option %s of type %d", odesc->name, odesc->type);
}
return res;
}
// cleanup handler for sane_poll
static void sane_thread_cleanup_mutex(void* arg) {
assert(arg != NULL);
slog(SLOG_DEBUG, "sane_thread_cleanup_mutex");
pthread_mutex_t* mutex = (pthread_mutex_t*)arg;
if (pthread_mutex_unlock(mutex) < 0) {
// if we can't unlock the mutex, something is heavily wrong!
slog(SLOG_ERROR, "pthread_mutex_unlock: %s", strerror(errno));
}
}
// cleanup handler for sane_poll
static void sane_thread_cleanup_value(void* arg) {
assert(arg != NULL);
slog(SLOG_DEBUG, "sane_thread_cleanup_value");
sane_opt_value_t* v = (sane_opt_value_t*)arg;
sane_option_value_free(v);
}
// this function can only be used in the critical region of *st
static void sane_find_matching_functions(sane_thread_t* st, cfg_t* sec) {
// TODO: use of recursive mutex???
slog(SLOG_DEBUG, "sane_find_matching_functions");
const char* title = cfg_title(sec);
if (title == NULL) {
title = SCANBD_NULL_STRING;
}
int functions = cfg_size(sec, C_FUNCTION);
if (functions <= 0) {
slog(SLOG_INFO, "no matching functions in section %s", title);
return;
}
slog(SLOG_INFO, "found %d functions in section %s", functions, title);
// iterate over all global functions
for(int i = 0; i < functions; i += 1) {
// get the function from the config file
cfg_t* function_i = cfg_getnsec(sec, C_FUNCTION, i);
assert(function_i != NULL);
// get the filter-regex from the config-file
const char* opt_regex = cfg_getstr(function_i, C_FILTER);
assert(opt_regex != NULL);
const char* title = cfg_title(function_i);
if (title == NULL) {
title = "(none)";
}
// compile the filter-regex
slog(SLOG_DEBUG, "checking function %s with filter: %s",
title, opt_regex);
regex_t creg;
int ret = regcomp(&creg, opt_regex, REG_EXTENDED | REG_NOSUB);
if (ret < 0) {
char err_text[1024];
regerror(ret, &creg, err_text, 1024);
slog(SLOG_WARN, "Can't compile regex: %s : %s", opt_regex, err_text);
continue;
}
// look for matching option-names
for(int opt = 1; opt < st->num_of_options; opt += 1) {
const SANE_Option_Descriptor* odesc = NULL;
if ((odesc = sane_get_option_descriptor(st->h, opt)) == NULL) {
// no valid option-descriptor available
// skip it
slog(SLOG_INFO, "option[%d] has no valid descriptor", opt);
continue;
}
assert(odesc);
if (!SANE_OPTION_IS_ACTIVE(odesc->cap)) {
slog(SLOG_INFO, "option[%d] is not active", opt);
continue;
}
// option is active
// only use active (user controllable) options
if (odesc->name == NULL) {
// we need a valid option-name
slog(SLOG_INFO, "option[%d] has no name", opt);
continue;
}
assert(odesc->name);
if (!((odesc->type == SANE_TYPE_BOOL) || (odesc->type == SANE_TYPE_INT) ||
(odesc->type == SANE_TYPE_FIXED)|| (odesc->type == SANE_TYPE_STRING) ||
(odesc->type == SANE_TYPE_BUTTON))) {
slog(SLOG_WARN, "option[%d] %s for device %s not of "
"type BOOL|INT|FIXED|STRING|BUTTON. Skipping",
opt, odesc->name, st->dev->name);
continue;
}
slog(SLOG_INFO, "found active option[%d] %s (type: %d) for device %s",
opt, odesc->name, odesc->type, st->dev->name);
// regex compare with the filter
if (regexec(&creg, odesc->name, 0, NULL, 0) != 0) {
// no match
continue;
}
// match
// now get the script
const char* env = cfg_getstr(function_i, C_ENV);
assert(env != NULL);
slog(SLOG_INFO, "installing function %s for %s, option[%d]: %s as env: %s",
title, st->dev->name, opt, odesc->name, env);
// looking for option already present in the
// array
int n = 0;
for(n = 0; n < st->num_of_options_with_functions; n += 1) {
if (st->functions[n].number == opt) {
slog(SLOG_WARN, "function %s overrides function of option[%d]",
title, n);
// break out with n == index_of_found_option
break;
}
}
// 0 <= n < st->num_of_options_with_scripts:
// we found it
// n == st->num_of_options_with_scripts:
// not found => new
st->functions[n].number = opt;
st->functions[n].env = env;
if (n == st->num_of_options_with_functions) {
// not found in the list
// we have a new option to be polled
st->num_of_options_with_functions += 1;
}
} // foreach option
// this compiled regex isn't used anymore
regfree(&creg);
} // foreach action
}
// this function can only be used in the critical region of *st
static void sane_find_matching_options(sane_thread_t* st, cfg_t* sec) {
slog(SLOG_DEBUG, "sane_find_matching_options");
const char* title = cfg_title(sec);
if (title == NULL) {
title = SCANBD_NULL_STRING;
}
// TODO: use of recursive mutex???
int actions = cfg_size(sec, C_ACTION);
if (actions <= 0) {
slog(SLOG_INFO, "no matching actions in section %s", title);
return;
}
slog(SLOG_INFO, "found %d actions in section %s", actions, title);
// iterate over all global actions
for(int i = 0; i < actions; i += 1) {
// get the action from the config file
cfg_t* action_i = cfg_getnsec(sec, C_ACTION, i);
assert(action_i != NULL);
// get the filter-regex from the config-file
const char* opt_regex = cfg_getstr(action_i, C_FILTER);
assert(opt_regex != NULL);
const char* title = cfg_title(action_i);
if (title == NULL) {
title = "(none)";
}
// compile the filter-regex
slog(SLOG_DEBUG, "checking action %s with filter: %s",
title, opt_regex);
regex_t creg;
int ret = regcomp(&creg, opt_regex, REG_EXTENDED | REG_NOSUB);
if (ret < 0) {
char err_text[1024];
regerror(ret, &creg, err_text, 1024);
slog(SLOG_WARN, "Can't compile regex: %s : %s", opt_regex, err_text);
continue;
}
// look for matching option-names
for(int opt = 1; opt < st->num_of_options; opt += 1) {
const SANE_Option_Descriptor* odesc = NULL;
if ((odesc = sane_get_option_descriptor(st->h, opt)) == NULL) {
// no valid option-descriptor available
// skip it
continue;
}
assert(odesc);
if (!SANE_OPTION_IS_ACTIVE(odesc->cap)) {
continue;
}
// option is active
// only use active (user controllable) options
if (odesc->name == NULL) {
// we need a valid option-name
continue;
}
assert(odesc->name);
if (!((odesc->type == SANE_TYPE_BOOL) || (odesc->type == SANE_TYPE_INT) ||
(odesc->type == SANE_TYPE_FIXED)|| (odesc->type == SANE_TYPE_STRING) ||
(odesc->type == SANE_TYPE_BUTTON))) {
slog(SLOG_WARN, "option[%d] %s for device %s not of "
"type BOOL|INT|FIXED|STRING|BUTTON. Skipping",
opt, odesc->name, st->dev->name);
continue;
}
slog(SLOG_INFO, "found active option[%d] %s (type: %d) for device %s",
opt, odesc->name, odesc->type, st->dev->name);
// regex compare with the filter
if (regexec(&creg, odesc->name, 0, NULL, 0) != 0) {
// no match
continue;
}
// match
// now get the script
const char* script = cfg_getstr(action_i, C_SCRIPT);
if (!script || (strlen(script) == 0)) {
script = SCANBD_NULL_STRING;
}
assert(script != NULL);
slog(SLOG_INFO, "installing action %s (%d) for %s, option[%d]: %s as: %s",
title, st->num_of_options_with_scripts, st->dev->name, opt, odesc->name, script);
cfg_t* cfg_sec_global = NULL;
cfg_sec_global = cfg_getsec(cfg, C_GLOBAL);
assert(cfg_sec_global);
bool multiple_actions = cfg_getbool(cfg_sec_global, C_MULTIPLE_ACTIONS);
slog(SLOG_INFO, "multiple actions allowed");
// looking for option already present in the
// array
int n = 0;
for(n = 0; n < st->num_of_options_with_scripts; n += 1) {
if (st->opts[n].number == opt) {
if (!multiple_actions) {
slog(SLOG_WARN, "action %s overrides script %s of option[%d] with %s",
title, st->opts[n].script, opt, script);
// break out with n == index_of_found_option
break;
}
else {
if (n < st->num_of_options) {
n = st->num_of_options_with_scripts;
slog(SLOG_INFO, "adding additional action %s (%d) for option[%d] with %s",
title, n, opt, script);
break;
}
else {
slog(SLOG_INFO, "can't add additional action %s for option[%d] with %s",
title, opt, script);
break;
}
}
}
}
// 0 <= n < st->num_of_options_with_scripts:
// we found it (override now)
// n == st->num_of_options_with_scripts:
// not found => new
if (n == st->num_of_options) {
continue; // no space left in array
}
st->opts[n].number = opt;
st->opts[n].action_name = title;
st->opts[n].script = script;
sane_option_value_free(&st->opts[n].from_value);
sane_option_value_free(&st->opts[n].to_value);
sane_option_value_free(&st->opts[n].value);
if ((odesc->type == SANE_TYPE_BOOL) || (odesc->type == SANE_TYPE_INT) ||
(odesc->type == SANE_TYPE_FIXED) || (odesc->type == SANE_TYPE_BUTTON)) {
// numerical option
cfg_t* num_trigger = cfg_getsec(action_i, C_NUMERICAL_TRIGGER);
assert(num_trigger);
st->opts[n].from_value.num_value = cfg_getint(num_trigger,
C_FROM_VALUE);
st->opts[n].to_value.num_value = cfg_getint(num_trigger, C_TO_VALUE);
st->opts[n].value = get_sane_option_value(st->h, opt);
slog(SLOG_INFO, "Initial value of option %s is %d", odesc->name,
st->opts[n].value);
} // type BOOL | INT || FIXED
else if (odesc->type == SANE_TYPE_STRING) {
bool valid = true;
// string option
cfg_t* str_trigger = cfg_getsec(action_i, C_STRING_TRIGGER);
assert(str_trigger);
st->opts[n].from_value.str_value.str =
strdup(cfg_getstr(str_trigger,
C_FROM_VALUE));
st->opts[n].from_value.str_value.reg = malloc(sizeof(regex_t));
int ret = 0;
ret = regcomp(st->opts[n].from_value.str_value.reg,
st->opts[n].from_value.str_value.str,
REG_EXTENDED | REG_NOSUB);
if (ret < 0) {
char err_text[1024];
regerror(ret, &creg, err_text, 1024);
slog(SLOG_WARN, "Can't compile regex: %s : %s",
st->opts[n].from_value.str_value.str, err_text);
valid = false;;
}
st->opts[n].to_value.str_value.str =
strdup(cfg_getstr(str_trigger,
C_TO_VALUE));
st->opts[n].to_value.str_value.reg = malloc(sizeof(regex_t));
ret = regcomp(st->opts[n].to_value.str_value.reg,
st->opts[n].to_value.str_value.str,
REG_EXTENDED | REG_NOSUB);
if (ret < 0) {
char err_text[1024];
regerror(ret, &creg, err_text, 1024);
slog(SLOG_WARN, "Can't compile regex: %s : %s",
st->opts[n].to_value.str_value.str, err_text);
valid = false;;
}
st->opts[n].value = get_sane_option_value(st->h, opt);
if (!valid) {
sane_option_value_free(&st->opts[n].from_value);
sane_option_value_free(&st->opts[n].to_value);
sane_option_value_free(&st->opts[n].value);
continue;
}
} // type STRING
else {
assert(false); // should not happen
}
if (n == st->num_of_options_with_scripts) {
// not found in the list
// we have a new option to be polled
st->num_of_options_with_scripts += 1;
}
} // foreach option
// this compiled regex isn't used anymore
regfree(&creg);
} // foreach action
}
// thread start funktion
// TODO: refactor, this is awfull long!
static void* sane_poll(void* arg) {
sane_thread_t* st = (sane_thread_t*)arg;
assert(st != NULL);
slog(SLOG_DEBUG, "sane_poll");
// we only expect the main thread to handle signals
sigset_t mask;
sigfillset(&mask);
pthread_sigmask(SIG_BLOCK, &mask, NULL);
static int si = 0;
// this thread uses the device and the san_thread_t datastructure
// lock it
pthread_cleanup_push(sane_thread_cleanup_mutex, ((void*)&st->mutex));
if (pthread_mutex_lock(&st->mutex) < 0) {
// if we can't get the mutex, something is heavily wrong!
slog(SLOG_ERROR, "pthread_mutex_lock: %s", strerror(errno));
pthread_exit(NULL);
}
// open the device this thread should poll
SANE_Status status = 0;
if ((status = sane_open(st->dev->name, &st->h)) != SANE_STATUS_GOOD) {
slog(SLOG_ERROR, "Can't open device %s: %s", st->dev->name, sane_strstatus(status));
slog(SLOG_WARN, "abandon polling of %s", st->dev->name);
pthread_exit(NULL);
}
// figure out the number of options this device has
// option 0 (zero) is guaranteed to exist with the total number of
// options of that device (including option 0)
st->num_of_options = 0;
if ((status = sane_control_option(st->h, 0, SANE_ACTION_GET_VALUE,
&st->num_of_options, 0)) != SANE_STATUS_GOOD) {
slog(SLOG_ERROR, "Can't get the number of scanner options");
pthread_exit(NULL);
}
if (st->num_of_options == 0) {
// no options -> nothing to poll
slog(SLOG_INFO, "No options for device %s", st->dev->name);
pthread_exit(NULL);
}
slog(SLOG_INFO, "found %d options for device %s", st->num_of_options, st->dev->name);
// allocate an array of options for the matching actions
//
// only one script is possible per option, later matching
// actions overwrite previous ones
// initialize the list of matching options
if (st->opts != NULL) {
slog(SLOG_ERROR, "possible memory leak: %s, %d", __FILE__, __LINE__);
}
st->opts = NULL;
st->opts = (sane_dev_option_t*) calloc(st->num_of_options,
sizeof(sane_dev_option_t));
assert(st->opts != NULL);
for(int i = 0; i < st->num_of_options; i += 1) {
sane_option_value_init(&st->opts[i].from_value);
sane_option_value_init(&st->opts[i].to_value);
sane_option_value_init(&st->opts[i].value);
}
// the number of valid entries in the above list
st->num_of_options_with_scripts = 0;
// initialize the list of matching functions
if (st->functions != NULL) {
slog(SLOG_ERROR, "possible memory leak: %s, %d", __FILE__, __LINE__);
}
st->functions = NULL;
st->functions = (sane_dev_function_t*) calloc(st->num_of_options,
sizeof(sane_dev_function_t));
assert(st->functions != NULL);
for(int i = 0; i < st->num_of_options; i += 1) {
st->functions[i].number = 0;
st->functions[i].env = NULL;
}
// the number of valid entries in the above list
st->num_of_options_with_functions = 0;
// find out the functions and actions
// get the global sconfig section
cfg_t* cfg_sec_global = NULL;
cfg_sec_global = cfg_getsec(cfg, C_GLOBAL);
assert(cfg_sec_global);
// find the global actions
sane_find_matching_options(st, cfg_sec_global);
// find the global functions
sane_find_matching_functions(st, cfg_sec_global);
// find (if any) device specifc sections
// these override global definitions, if any
int local_sections = cfg_size(cfg, C_DEVICE);
slog(SLOG_DEBUG, "found %d local device sections", local_sections);
for(int loc = 0; loc < local_sections; loc += 1) {
cfg_t* loc_i = cfg_getnsec(cfg, C_DEVICE, loc);
assert(loc_i != NULL);
// get the filter-regex from the config-file
const char* loc_regex = cfg_getstr(loc_i, C_FILTER);
assert(loc_regex != NULL);
const char* title = cfg_title(loc_i);
if (title == NULL) {
title = "(none)";
}
// compile the filter-regex
slog(SLOG_INFO, "checking device section %s with filter: %s",
title, loc_regex);
regex_t creg;
int ret = regcomp(&creg, loc_regex, REG_EXTENDED | REG_NOSUB);
if (ret < 0) {
char err_text[1024];
regerror(ret, &creg, err_text, 1024);
slog(SLOG_WARN, "Can't compile regex: %s : %s", loc_regex, err_text);
continue;
}
// compare the regex against the device name
if (regexec(&creg, st->dev->name, 0, NULL, 0) == 0) {
// match
int loc_actions = cfg_size(loc_i, C_ACTION);
slog(SLOG_INFO, "found %d local action for device %s [%s]",
loc_actions, st->dev->name, title);
// get the local actions for this device
sane_find_matching_options(st, loc_i);
// get the local functions for this device
sane_find_matching_functions(st, loc_i);
}
regfree(&creg);
} // foreach local section
int timeout = cfg_getint(cfg_sec_global, C_TIMEOUT);
if (timeout <= 0) {
timeout = C_TIMEOUT_DEF;
}
slog(SLOG_DEBUG, "timeout: %d ms", timeout);
slog(SLOG_DEBUG, "Start the polling for device %s", st->dev->name);
while(true) {
slog(SLOG_DEBUG, "polling thread for %s cancellation point", st->dev->name);
// special cancellation point
pthread_testcancel();
slog(SLOG_DEBUG, "polling device %s", st->dev->name);
for(si = 0; si < st->num_of_options_with_scripts; si += 1) {
const SANE_Option_Descriptor* odesc = NULL;
odesc = sane_get_option_descriptor(st->h, st->opts[si].number);
assert(odesc);
if (st->opts[si].script != NULL) {
if (strlen(st->opts[si].script) <= 0) {
slog(SLOG_WARN, "No valid script for option %s for device %s",
odesc->name, st->dev->name);
continue;
}
}
else {
slog(SLOG_WARN, "No script for option %s for device %s",
odesc->name, st->dev->name);
continue;
}
assert(st->opts[si].script != NULL);
assert(strlen(st->opts[si].script) > 0);
sane_opt_value_t value;
sane_option_value_init(&value);
// push the cleanup-handle to free the value storage
pthread_cleanup_push(sane_thread_cleanup_value, &value);
// get the actual value
// but don't query an option twice or more (see config multiple_actions)
// because this may reset the values and no other value changes can be
// detected
int o = 0;
bool gotAlready = false;
for(o = 0; o < si; o += 1) {
if (st->opts[o].number == st->opts[si].number) {
gotAlready = true;
break;
}
}
if (!gotAlready) {
// first query of option with this number
value = get_sane_option_value(st->h, st->opts[si].number);
}
else {
// additional query, so copy the value
slog(SLOG_INFO, "got the value already -> copy");
// found: copy the value
slog(SLOG_DEBUG, "copy the value of option %d", st->opts[o].number);
value.num_value = st->opts[o].value.num_value;
if (st->opts[o].value.str_value.str != NULL) {
value.str_value.str = strdup(st->opts[o].value.str_value.str);
assert(value.str_value.str != NULL);
}
}
slog(SLOG_INFO, "checking option %s number %d (%d) for device %s: value: %d",
odesc->name, st->opts[si].number, si,
st->dev->name, value);
if ((odesc->type == SANE_TYPE_BOOL) || (odesc->type == SANE_TYPE_INT) ||
(odesc->type == SANE_TYPE_FIXED) || (odesc->type == SANE_TYPE_BUTTON)) {
if ((st->opts[si].from_value.num_value == st->opts[si].value.num_value) &&
(st->opts[si].to_value.num_value == value.num_value)) {
slog(SLOG_DEBUG, "value trigger: numerical");
st->triggered = true;
st->triggered_option = si;
// we need to trigger all waiting threads
if (pthread_cond_broadcast(&st->cv) < 0) {
slog(SLOG_ERROR, "pthread_cond_broadcats: this shouln't happen");
}
}
}
else if (odesc->type == SANE_TYPE_STRING) {
if ((regexec(st->opts[si].from_value.str_value.reg,
st->opts[si].value.str_value.str, 0, NULL, 0) == 0) &&
(regexec(st->opts[si].to_value.str_value.reg,
value.str_value.str, 0, NULL, 0) == 0)) {
slog(SLOG_DEBUG, "value trigger: string");
st->triggered = true;
st->triggered_option = si;
// we need to trigger all waiting threads
if (pthread_cond_broadcast(&st->cv) < 0) {
slog(SLOG_ERROR, "pthread_cond_broadcats: this shouln't happen");
}
}
}
else {
assert(false);
}
// free the previous allocated value
sane_option_value_free(&st->opts[si].value);
// pass the responsibility to free the value to the main
// thread, if this thread gets canceled
if (pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) < 0) {
slog(SLOG_ERROR, "pthread_setcancelstate: %s", strerror(errno));
}
st->opts[si].value = value;
pthread_cleanup_pop(0);
if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) < 0) {
slog(SLOG_ERROR, "pthread_setcancelstate: %s", strerror(errno));
}
// was there a value change?
if (st->triggered && (st->triggered_option >= 0)) {
assert(st->triggered_option >= 0); // index into the opts-array
assert(st->triggered_option < st->num_of_options_with_scripts);
slog(SLOG_ERROR, "trigger action for %s for device %s with script %s",
odesc->name, st->dev->name, st->opts[st->triggered_option].script);
// prepare the environment for the script to be called
// number of env-vars =
// number of found function-options
// plus the values in the environment-section (2):
// device, action
// plus those 4:
// PATH, PWD, USER, HOME
// plus the sentinel
cfg_t* global_envs = cfg_getsec(cfg_sec_global, C_ENVIRONMENT);
int number_of_envs = st->num_of_options_with_functions + 4 + 2 + 1;
char** env = calloc(number_of_envs, sizeof(char*));
for(int e = 0; e < number_of_envs; e += 1) {
env[e] = calloc(NAME_MAX + 1, sizeof(char));
}
int e = 0;
for(e = 0; e < st->num_of_options_with_functions; e += 1) {
const SANE_Option_Descriptor* fdesc = NULL;
fdesc = sane_get_option_descriptor(st->h,
st->functions[e].number);
assert(fdesc);
// check if the function-option is the same
// as a action-option. If so, use the
// action-option value instead of re-get the same
// option value, because it is (may be) reset
// after the query by the backend
sane_opt_value_t v;
sane_option_value_init(&v);
int o = 0;
for(o = 0; o < st->num_of_options_with_scripts; o += 1) {
if (st->opts[o].number == st->functions[e].number) {
break;
}
}
if (o == st->num_of_options_with_scripts) {
// not found: query the value
v = get_sane_option_value(st->h, st->functions[e].number);
}
else {
}
if ((fdesc->type == SANE_TYPE_BOOL) || (fdesc->type == SANE_TYPE_INT) ||
(fdesc->type == SANE_TYPE_FIXED) || (odesc->type == SANE_TYPE_BUTTON)) {
snprintf(env[e], NAME_MAX, "%s=%lu", st->functions[e].env,
v.num_value);
slog(SLOG_DEBUG, "setting env: %s", env[e]);
}
else if (fdesc->type == SANE_TYPE_STRING) {
snprintf(env[e], NAME_MAX, "%s=%s", st->functions[e].env,
v.str_value.str);
slog(SLOG_DEBUG, "setting env: %s", env[e]);
}
else {
assert(false);
}
sane_option_value_free(&v);
}
const char* ev = "PATH";
if (getenv(ev) != NULL) {
snprintf(env[e], NAME_MAX, "%s=%s", ev, getenv(ev));
slog(SLOG_DEBUG, "setting env: %s", env[e]);
e += 1;
}
else {
snprintf(env[e], NAME_MAX, "%s=%s", ev, "/usr/sbin:/usr/bin:/sbin:/bin");
slog(SLOG_DEBUG, "No PATH, setting env: %s", env[e]);
e += 1;
}
ev = "PWD";
if (getenv(ev) != NULL) {
snprintf(env[e], NAME_MAX, "%s=%s", ev, getenv(ev));
slog(SLOG_DEBUG, "setting env: %s", env[e]);
e += 1;
}
else {
char buf[PATH_MAX];
char* ptr = getcwd(buf, PATH_MAX - 1);
if (!ptr) {
slog(SLOG_ERROR, "can't get pwd");
}
else {
assert(ptr);
snprintf(env[e], NAME_MAX, "%s=%s", ev, ptr);
slog(SLOG_DEBUG, "No PWD, setting env: %s", env[e]);
e += 1;
}
}
ev = "USER";
if (getenv(ev) != NULL) {
snprintf(env[e], NAME_MAX, "%s=%s", ev, getenv(ev));
slog(SLOG_DEBUG, "setting env: %s", env[e]);
e += 1;
}
else {
struct passwd* pwd = NULL;
pwd = getpwuid(geteuid());
assert(pwd);
snprintf(env[e], NAME_MAX, "%s=%s", ev, pwd->pw_name);
slog(SLOG_DEBUG, "No USER, setting env: %s", env[e]);
e += 1;
}
ev = "HOME";
if (getenv(ev) != NULL) {
snprintf(env[e], NAME_MAX, "%s=%s", ev, getenv(ev));
slog(SLOG_DEBUG, "setting env: %s", env[e]);
e += 1;
}
else {
struct passwd* pwd = 0;
pwd = getpwuid(geteuid());
assert(pwd);
snprintf(env[e], NAME_MAX, "%s=%s", ev, pwd->pw_dir);
slog(SLOG_DEBUG, "No HOME, setting env: %s", env[e]);
e += 1;
}
ev = cfg_getstr(global_envs, C_DEVICE);
if (ev != NULL) {
snprintf(env[e], NAME_MAX, "%s=%s", ev, st->dev->name);
slog(SLOG_DEBUG, "setting env: %s", env[e]);
e += 1;
}
ev = cfg_getstr(global_envs, C_ACTION);
if (ev != NULL) {
snprintf(env[e], NAME_MAX, "%s=%s", ev,
st->opts[st->triggered_option].action_name);
slog(SLOG_DEBUG, "setting env: %s", env[e]);
e += 1;
}
env[e] = NULL;
assert(e == number_of_envs-1);
// sendout an dbus-signal with all the values as
// arguments
dbus_send_signal(SCANBD_DBUS_SIGNAL_SCAN_BEGIN, st->dev->name);
//dbus_send_signal_argv_async(SCANBD_DBUS_SIGNAL_TRIGGER, env);
dbus_send_signal_argv(SCANBD_DBUS_SIGNAL_TRIGGER, env);
// the action-script will use the device,
// so we have to release the device
sane_close(st->h);
st->h = NULL;
assert(st->triggered_option >= 0);
assert(st->opts[st->triggered_option].script);
assert(strlen(st->opts[st->triggered_option].script) > 0);
// need to copy the values because we leave the
// critical section
// int triggered_option = st->triggered_option;
char* script = strdup(st->opts[st->triggered_option].script);
assert(script != NULL);
// leave the critical section
if (pthread_mutex_unlock(&st->mutex) < 0) {
// if we can't unlock the mutex, something is heavily wrong!
slog(SLOG_ERROR, "pthread_mutex_unlock: %s", strerror(errno));
pthread_exit(NULL);
}
if (strcmp(script, SCANBD_NULL_STRING) != 0) {
assert(timeout > 0);
usleep(timeout * 1000); //ms
pid_t cpid;
if ((cpid = fork()) < 0) {
slog(SLOG_ERROR, "Can't fork: %s", strerror(errno));
}
else if (cpid > 0) { // parent
slog(SLOG_INFO, "waiting for child: %s", script);
int status;
if (waitpid(cpid, &status, 0) < 0) {
slog(SLOG_ERROR, "waitpid: %s", strerror(errno));
}
if (WIFEXITED(status)) {
slog(SLOG_INFO, "child %s exited with status: %d",
script, WEXITSTATUS(status));
}
if (WIFSIGNALED(status)) {
slog(SLOG_INFO, "child %s signaled with signal: %d",
script, WTERMSIG(status));
}
}
else { // child
slog(SLOG_DEBUG, "exec for %s", script);
if (execle(script, script, NULL, env) < 0) {
slog(SLOG_ERROR, "execlp: %s", strerror(errno));
}
exit(EXIT_FAILURE); // not reached
}
} // script == SCANBD_NULL_STRING
assert(script != NULL);
free(script);
// free (last element is the sentinel!)
assert(env != NULL);
for(int e = 0; e < number_of_envs - 1; e += 1) {
assert(env[e] != NULL);
free(env[e]);
}
free(env);
// enter the critical section
if (pthread_mutex_lock(&st->mutex) < 0) {
// if we can't get the mutex, something is heavily wrong!
slog(SLOG_ERROR, "pthread_mutex_lock: %s", strerror(errno));
pthread_exit(NULL);
}
st->triggered = false;
st->triggered_option = -1; // invalid
// we need to trigger all waiting threads
if (pthread_cond_broadcast(&st->cv) < 0) {
slog(SLOG_ERROR, "pthread_cond_broadcats: this shouln't happen");
}
// leave the critical section
if (pthread_mutex_unlock(&st->mutex) < 0) {
// if we can't release the mutex, something is heavily wrong!
slog(SLOG_ERROR, "pthread_mutex_unlock: %s", strerror(errno));
pthread_exit(NULL);
}
// sleep the timeout to settle devices, necessary?
usleep(timeout * 1000); //ms
// send out the debus signal
dbus_send_signal(SCANBD_DBUS_SIGNAL_SCAN_END, st->dev->name);
// enter the critical section
if (pthread_mutex_lock(&st->mutex) < 0) {
// if we can't get the mutex, something is heavily wrong!
slog(SLOG_ERROR, "pthread_mutex_lock: %s", strerror(errno));
pthread_exit(NULL);
}
slog(SLOG_DEBUG, "reopen device %s", st->dev->name);
if ((status = sane_open(st->dev->name, &st->h)) != SANE_STATUS_GOOD) {
slog(SLOG_ERROR, "Can't open device %s, %s",
st->dev->name, sane_strstatus(status));
if (status == SANE_STATUS_ACCESS_DENIED) {
slog(SLOG_WARN, "abandon polling of %s", st->dev->name);
pthread_exit(NULL);
}
}
} // if triggered
} // foreach option
// release the mutex
// because pthread_cleanup_pop is a macro we can't use it here
// pthread_cleanup_pop(1);
if (pthread_mutex_unlock(&st->mutex) < 0) {
// if we can't unlock the mutex, something is heavily wrong!
slog(SLOG_ERROR, "pthread_mutex_unlock: %s", strerror(errno));
pthread_exit(NULL);
}
// sleep the polling timeout
usleep(timeout * 1000); //ms
// regain the mutex
// because pthread_cleanup_push is a macro we can't use it here
// pthread_cleanup_push(sane_thread_cleanup_mutex, ((void*)&st->mutex));
if (pthread_mutex_lock(&st->mutex) < 0) {
// if we can't get the mutex, something is heavily wrong!
slog(SLOG_ERROR, "pthread_mutex_lock: %s", strerror(errno));
pthread_exit(NULL);
}
}
pthread_cleanup_pop(1); // release the mutex
pthread_exit(NULL);
}
// helper to trigger a specified action from another thread
// (e.g. dbus) via an action number
void sane_trigger_action(int number_of_dev, int action) {
assert(number_of_dev >= 0);
assert(action >= 0);
slog(SLOG_DEBUG, "sane_trigger_action device=%d, action=%d", number_of_dev, action);
if (pthread_mutex_lock(&sane_mutex) < 0) {
slog(SLOG_ERROR, "pthread_mutex_lock: %s", strerror(errno));
return;
}
if (num_devices <= 0) {
slog(SLOG_WARN, "No devices at all");
goto cleanup_sane;
}
if (number_of_dev >= num_devices) {
slog(SLOG_WARN, "No such device number %d", number_of_dev);
goto cleanup_sane;
}
while(sane_poll_threads == NULL) {
// no devices actually polling
slog(SLOG_WARN, "No polling at the moment, waiting ...");
if (pthread_cond_wait(&sane_cv, &sane_mutex) < 0) {
slog(SLOG_ERROR, "pthread_cond_wait: ", strerror(errno));
goto cleanup_sane;
}
}
assert(sane_poll_threads != NULL);
sane_thread_t* st = &sane_poll_threads[number_of_dev];
assert(st != NULL);
// this thread uses the device and the sane_thread_t datastructure
// lock it
if (pthread_mutex_lock(&st->mutex) < 0) {
slog(SLOG_ERROR, "pthread_mutex_lock: %s", strerror(errno));
goto cleanup_sane;
}
if (action >= st->num_of_options_with_scripts) {
slog(SLOG_WARN, "No such action %d for device number %d", action, number_of_dev);
goto cleanup_dev;
}
while(st->triggered == true) {
slog(SLOG_DEBUG, "sane_trigger_action: an action is active, waiting ...");
if (pthread_cond_wait(&st->cv, &st->mutex) < 0) {
slog(SLOG_ERROR, "pthread_cond_wait: %s", strerror(errno));
goto cleanup_dev;
}
}
slog(SLOG_DEBUG, "sane_trigger_action: an action is active, waiting ...");
st->triggered = true;
st->triggered_option = action;
// we need to trigger all waiting threads
if (pthread_cond_broadcast(&st->cv) < 0) {
slog(SLOG_ERROR, "pthread_cond_broadcats: this shouln't happen");
}
cleanup_dev:
if (pthread_mutex_unlock(&st->mutex) < 0) {
slog(SLOG_ERROR, "pthread_mutex_unlock: %s", strerror(errno));
}
cleanup_sane:
if (pthread_mutex_unlock(&sane_mutex) < 0) {
slog(SLOG_ERROR, "pthread_mutex_unlock: %s", strerror(errno));
}
return;
}
void start_sane_threads(void) {
slog(SLOG_DEBUG, "start_sane_threads");
if (pthread_mutex_lock(&sane_mutex) < 0) {
// if we can't get the mutex, something is heavily wrong!
slog(SLOG_ERROR, "pthread_mutex_lock: %s", strerror(errno));
return;
}
if (sane_poll_threads != NULL) {
// if there are active threads kill them
stop_sane_threads();
}
// allocate the thread list
assert(sane_poll_threads == NULL);
sane_poll_threads = (sane_thread_t*) calloc(num_devices, sizeof(sane_thread_t));
if (sane_poll_threads == NULL) {
slog(SLOG_ERROR, "Can't allocate memory for polling threads");
goto cleanup;
}
// starting for each device a seperate thread
for(int i = 0; i < num_devices; i += 1) {
slog(SLOG_DEBUG, "Starting poll thread for %s", sane_device_list[i]->name);
sane_poll_threads[i].tid = 0;
sane_poll_threads[i].dev = sane_device_list[i];
sane_poll_threads[i].h = 0;
sane_poll_threads[i].opts = NULL;
sane_poll_threads[i].functions = NULL;
sane_poll_threads[i].num_of_options = 0;
sane_poll_threads[i].triggered = false;
sane_poll_threads[i].triggered_option = -1;
sane_poll_threads[i].num_of_options_with_scripts = 0;
sane_poll_threads[i].num_of_options_with_functions = 0;
if (pthread_mutex_init(&sane_poll_threads[i].mutex, NULL) < 0) {
slog(SLOG_ERROR, "pthread_mutex_init: should not happen");
}
if (pthread_cond_init(&sane_poll_threads[i].cv, NULL) < 0) {
slog(SLOG_ERROR, "pthread_cond_init: should not happen");
}
if (pthread_create(&sane_poll_threads[i].tid, NULL, sane_poll,
(void*)&sane_poll_threads[i]) < 0) {
slog(SLOG_ERROR, "Can't start sane_poll_thread: %s", strerror(errno));
exit(EXIT_FAILURE);
}
slog(SLOG_DEBUG, "Thread started for device %s", sane_device_list[i]->name);
}
if (pthread_cond_broadcast(&sane_cv)) {
slog(SLOG_ERROR, "pthread_cond_broadcast: %s", strerror(errno));
}
cleanup:
if (pthread_mutex_unlock(&sane_mutex) < 0) {
// if we can't unlock the mutex, something is heavily wrong!
slog(SLOG_ERROR, "pthread_mutex_unlock: %s", strerror(errno));
return;
}
}
// stops all sane polling threads
void stop_sane_threads(void) {
slog(SLOG_DEBUG, "stop_sane_threads");
if (pthread_mutex_lock(&sane_mutex) < 0) {
// if we can't get the mutex, something is heavily wrong!
slog(SLOG_ERROR, "pthread_mutex_lock: %s", strerror(errno));
return;
}
if (sane_poll_threads == NULL) {
// we don't have any active threads
slog(SLOG_DEBUG, "stop_sane_threads: nothing to stop");
goto cleanup;
}
// sending cancel request to all threads
for(int i = 0; i < num_devices; i += 1) {
if (pthread_mutex_lock(&sane_poll_threads[i].mutex) < 0) {
slog(SLOG_ERROR, "pthread_mutex_lock: %s", strerror(errno));
}
while(sane_poll_threads[i].triggered == true) {
slog(SLOG_DEBUG, "stop_sane_threads: an action is active, waiting ...");
if (pthread_cond_wait(&sane_poll_threads[i].cv,
&sane_poll_threads[i].mutex) < 0) {
slog(SLOG_ERROR, "pthread_cond_wait: %s", strerror(errno));
}
}
if (pthread_mutex_unlock(&sane_poll_threads[i].mutex) < 0) {
slog(SLOG_ERROR, "pthread_mutex_lock: %s", strerror(errno));
}
slog(SLOG_DEBUG, "stopping poll thread for device %s", (*(sane_device_list + i))->name);
if (pthread_cancel(sane_poll_threads[i].tid) < 0) {
if (errno == ESRCH) {
slog(SLOG_ERROR, "poll thread for device %s was already cancelled");
}
else {
slog(SLOG_ERROR, "unknown error from pthread_cancel: %s", strerror(errno));
}
}
}
// waiting for all threads to vanish
for(int i = 0; i < num_devices; i += 1) {
slog(SLOG_DEBUG, "waiting for poll thread for device %s",
(*(sane_device_list + i))->name);
// joining all threads to prevent memory leaks
if (pthread_join(sane_poll_threads[i].tid, NULL) < 0) {
slog(SLOG_ERROR, "pthread_join: %s", strerror(errno));
}
sane_poll_threads[i].tid = 0;
// close the associated device of the thread
slog(SLOG_DEBUG, "closing device %s", sane_poll_threads[i].dev->name);
if (sane_poll_threads[i].h != NULL) {
sane_close(sane_poll_threads[i].h);
sane_poll_threads[i].h = NULL;
}
if (sane_poll_threads[i].opts) {
slog(SLOG_DEBUG, "freeing opt ressources for device %s thread",
sane_poll_threads[i].dev->name);
// free the matching options list of that device / threads
for (int k = 0; k < sane_poll_threads[i].num_of_options; k += 1) {
sane_option_value_free(&sane_poll_threads[i].opts[k].from_value);
sane_option_value_free(&sane_poll_threads[i].opts[k].to_value);
sane_option_value_free(&sane_poll_threads[i].opts[k].value);
}
free(sane_poll_threads[i].opts);
sane_poll_threads[i].opts = NULL;
}
if (sane_poll_threads[i].functions) {
slog(SLOG_DEBUG, "freeing funtion ressources for device %s thread",
sane_poll_threads[i].dev->name);
free(sane_poll_threads[i].functions);
sane_poll_threads[i].functions = NULL;
}
if (pthread_cond_destroy(&sane_poll_threads[i].cv) < 0) {
slog(SLOG_ERROR, "pthread_cond_destroy: %s", strerror(errno));
}
if (pthread_mutex_destroy(&sane_poll_threads[i].mutex) < 0) {
slog(SLOG_ERROR, "pthread_mutex_destroy: %s", strerror(errno));
}
}
// free the thread list
free(sane_poll_threads);
sane_poll_threads = NULL;
// no threads active anymore
if (pthread_cond_broadcast(&sane_cv)) {
slog(SLOG_ERROR, "pthread_cond_broadcast: %s", strerror(errno));
}
cleanup:
if (pthread_mutex_unlock(&sane_mutex) < 0) {
// if we can't unlock the mutex, something is heavily wrong!
slog(SLOG_ERROR, "pthread_mutex_unlock: %s", strerror(errno));
return;
}
}