Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief Implementation of I/O functions as defined in kdbio.h
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : *
8 : */
9 :
10 : #include <kdbhelper.h>
11 : #include <kdbinvoke.h>
12 : #include <kdbio.h>
13 : #include <kdbioplugin.h>
14 : #include <kdbioprivate.h>
15 : #include <kdblogger.h>
16 : #include <kdbprivate.h>
17 :
18 : #include <stdio.h>
19 :
20 0 : void elektraIoSetBinding (KDB * kdb, ElektraIoInterface * ioBinding)
21 : {
22 0 : kdb->ioBinding = ioBinding;
23 :
24 0 : KeySet * parameters =
25 0 : ksNew (1, keyNew ("/ioBinding", KEY_BINARY, KEY_SIZE, sizeof (ioBinding), KEY_VALUE, &ioBinding, KEY_END), KS_END);
26 :
27 : // iterate over global plugins
28 0 : for (int positionIndex = 0; positionIndex < NR_GLOBAL_POSITIONS; positionIndex++)
29 : {
30 0 : for (int subPositionIndex = 0; subPositionIndex < NR_GLOBAL_SUBPOSITIONS; subPositionIndex++)
31 : {
32 0 : Plugin * plugin = kdb->globalPlugins[positionIndex][subPositionIndex];
33 0 : if (!plugin)
34 : {
35 0 : continue;
36 : }
37 :
38 0 : elektraDeferredCall (plugin, "setIoBinding", parameters);
39 : }
40 : }
41 :
42 0 : ksDel (parameters);
43 0 : }
44 :
45 0 : ElektraIoInterface * elektraIoGetBinding (KDB * kdb)
46 : {
47 0 : return kdb->ioBinding;
48 : }
49 :
50 : // ################################
51 : // # Binding accessors
52 : // ################################
53 :
54 116 : ElektraIoInterface * elektraIoNewBinding (ElektraIoBindingAddFd * addFd, ElektraIoBindingUpdateFd * updateFd,
55 : ElektraIoBindingRemoveFd * removeFd, ElektraIoBindingAddTimer * addTimer,
56 : ElektraIoBindingUpdateTimer * updateTimer, ElektraIoBindingRemoveTimer * removeTimer,
57 : ElektraIoBindingAddIdle * addIdle, ElektraIoBindingUpdateIdle * updateIdle,
58 : ElektraIoBindingRemoveIdle * removeIdle, ElektraIoBindingCleanup * cleanup)
59 : {
60 116 : if (addFd == NULL)
61 : {
62 : ELEKTRA_LOG_WARNING ("addFd cannot be NULL");
63 : return NULL;
64 : }
65 116 : if (updateFd == NULL)
66 : {
67 : ELEKTRA_LOG_WARNING ("updateFd cannot be NULL");
68 : return NULL;
69 : }
70 116 : if (removeFd == NULL)
71 : {
72 : ELEKTRA_LOG_WARNING ("removeFd cannot be NULL");
73 : return NULL;
74 : }
75 116 : if (addTimer == NULL)
76 : {
77 : ELEKTRA_LOG_WARNING ("addTimer cannot be NULL");
78 : return NULL;
79 : }
80 116 : if (updateTimer == NULL)
81 : {
82 : ELEKTRA_LOG_WARNING ("updateTimer cannot be NULL");
83 : return NULL;
84 : }
85 116 : if (removeTimer == NULL)
86 : {
87 : ELEKTRA_LOG_WARNING ("removeTimer cannot be NULL");
88 : return NULL;
89 : }
90 116 : if (addIdle == NULL)
91 : {
92 : ELEKTRA_LOG_WARNING ("addIdle cannot be NULL");
93 : return NULL;
94 : }
95 116 : if (updateIdle == NULL)
96 : {
97 : ELEKTRA_LOG_WARNING ("updateIdle cannot be NULL");
98 : return NULL;
99 : }
100 116 : if (removeIdle == NULL)
101 : {
102 : ELEKTRA_LOG_WARNING ("removeIdle cannot be NULL");
103 : return NULL;
104 : }
105 116 : if (cleanup == NULL)
106 : {
107 : ELEKTRA_LOG_WARNING ("cleanup cannot be NULL");
108 : return NULL;
109 : }
110 :
111 116 : ElektraIoInterface * binding = elektraMalloc (sizeof (*binding));
112 116 : if (!binding)
113 : {
114 : ELEKTRA_LOG_WARNING ("elektraMalloc failed");
115 : return NULL;
116 : }
117 :
118 116 : binding->data = NULL;
119 116 : binding->addFd = addFd;
120 116 : binding->updateFd = updateFd;
121 116 : binding->removeFd = removeFd;
122 116 : binding->addTimer = addTimer;
123 116 : binding->updateTimer = updateTimer;
124 116 : binding->removeTimer = removeTimer;
125 116 : binding->addIdle = addIdle;
126 116 : binding->updateIdle = updateIdle;
127 116 : binding->removeIdle = removeIdle;
128 116 : binding->cleanup = cleanup;
129 :
130 116 : return binding;
131 : }
132 :
133 58 : int elektraIoBindingAddFd (ElektraIoInterface * binding, ElektraIoFdOperation * fdOp)
134 : {
135 58 : if (binding == NULL)
136 : {
137 : ELEKTRA_LOG_WARNING ("binding cannot be NULL");
138 : return 0;
139 : }
140 58 : if (fdOp == NULL)
141 : {
142 : ELEKTRA_LOG_WARNING ("fdOp cannot be NULL");
143 : return 0;
144 : }
145 58 : if (fdOp->binding != NULL)
146 : {
147 : ELEKTRA_LOG_WARNING ("operation cannot be assigned to multiple bindings");
148 : return 0;
149 : }
150 52 : fdOp->binding = binding;
151 52 : int result = binding->addFd (binding, fdOp);
152 52 : if (!result)
153 : {
154 0 : fdOp->binding = NULL;
155 : }
156 : return result;
157 : }
158 :
159 18 : int elektraIoBindingUpdateFd (ElektraIoFdOperation * fdOp)
160 : {
161 18 : if (fdOp == NULL)
162 : {
163 : ELEKTRA_LOG_WARNING ("fdOp cannot be NULL");
164 : return 0;
165 : }
166 18 : return fdOp->binding->updateFd (fdOp);
167 : }
168 :
169 52 : int elektraIoBindingRemoveFd (ElektraIoFdOperation * fdOp)
170 : {
171 52 : if (fdOp == NULL)
172 : {
173 : ELEKTRA_LOG_WARNING ("fdOp cannot be NULL");
174 : return 0;
175 : }
176 52 : int result = fdOp->binding->removeFd (fdOp);
177 52 : if (result)
178 : {
179 52 : fdOp->binding = NULL;
180 : }
181 : return result;
182 : }
183 :
184 112 : int elektraIoBindingAddTimer (ElektraIoInterface * binding, ElektraIoTimerOperation * timerOp)
185 : {
186 112 : if (binding == NULL)
187 : {
188 : ELEKTRA_LOG_WARNING ("binding cannot be NULL");
189 : return 0;
190 : }
191 112 : if (timerOp == NULL)
192 : {
193 : ELEKTRA_LOG_WARNING ("timerOp cannot be NULL");
194 : return 0;
195 : }
196 112 : if (timerOp->binding != NULL)
197 : {
198 : ELEKTRA_LOG_WARNING ("operation cannot be assigned to multiple bindings");
199 : return 0;
200 : }
201 106 : timerOp->binding = binding;
202 106 : int result = binding->addTimer (binding, timerOp);
203 106 : if (!result)
204 : {
205 0 : timerOp->binding = NULL;
206 : }
207 : return result;
208 : }
209 :
210 18 : int elektraIoBindingUpdateTimer (ElektraIoTimerOperation * timerOp)
211 : {
212 18 : if (timerOp == NULL)
213 : {
214 : ELEKTRA_LOG_WARNING ("timerOp cannot be NULL");
215 : return 0;
216 : }
217 18 : return timerOp->binding->updateTimer (timerOp);
218 : }
219 :
220 106 : int elektraIoBindingRemoveTimer (ElektraIoTimerOperation * timerOp)
221 : {
222 106 : if (timerOp == NULL)
223 : {
224 : ELEKTRA_LOG_WARNING ("timerOp cannot be NULL");
225 : return 0;
226 : }
227 106 : int result = timerOp->binding->removeTimer (timerOp);
228 106 : if (result)
229 : {
230 106 : timerOp->binding = NULL;
231 : }
232 : return result;
233 : }
234 :
235 64 : int elektraIoBindingAddIdle (ElektraIoInterface * binding, ElektraIoIdleOperation * idleOp)
236 : {
237 64 : if (binding == NULL)
238 : {
239 : ELEKTRA_LOG_WARNING ("binding cannot be NULL");
240 : return 0;
241 : }
242 64 : if (idleOp == NULL)
243 : {
244 : ELEKTRA_LOG_WARNING ("idleOp cannot be NULL");
245 : return 0;
246 : }
247 64 : if (idleOp->binding != NULL)
248 : {
249 : ELEKTRA_LOG_WARNING ("operation cannot be assigned to multiple bindings");
250 : return 0;
251 : }
252 58 : idleOp->binding = binding;
253 58 : int result = binding->addIdle (binding, idleOp);
254 58 : if (!result)
255 : {
256 0 : idleOp->binding = NULL;
257 : }
258 : return result;
259 : }
260 :
261 26 : int elektraIoBindingUpdateIdle (ElektraIoIdleOperation * idleOp)
262 : {
263 26 : if (idleOp == NULL)
264 : {
265 : ELEKTRA_LOG_WARNING ("idleOp cannot be NULL");
266 : return 0;
267 : }
268 26 : return idleOp->binding->updateIdle (idleOp);
269 : }
270 :
271 58 : int elektraIoBindingRemoveIdle (ElektraIoIdleOperation * idleOp)
272 : {
273 58 : if (idleOp == NULL)
274 : {
275 : ELEKTRA_LOG_WARNING ("idleOp cannot be NULL");
276 : return 0;
277 : }
278 58 : int result = idleOp->binding->removeIdle (idleOp);
279 58 : if (result)
280 : {
281 58 : idleOp->binding = NULL;
282 : }
283 : return result;
284 : }
285 :
286 110 : int elektraIoBindingCleanup (ElektraIoInterface * binding)
287 : {
288 110 : if (binding == NULL)
289 : {
290 : ELEKTRA_LOG_WARNING ("binding cannot be NULL");
291 : return 0;
292 : }
293 110 : return binding->cleanup (binding);
294 : }
295 :
296 296 : void * elektraIoBindingGetData (ElektraIoInterface * binding)
297 : {
298 296 : if (binding == NULL)
299 : {
300 : ELEKTRA_LOG_WARNING ("binding was NULL");
301 : return NULL;
302 : }
303 :
304 296 : return binding->data;
305 : }
306 :
307 116 : int elektraIoBindingSetData (ElektraIoInterface * binding, void * data)
308 : {
309 116 : if (binding == NULL)
310 : {
311 : ELEKTRA_LOG_WARNING ("operation was NULL");
312 : return 0;
313 : }
314 :
315 116 : binding->data = data;
316 116 : return 1;
317 : }
318 :
319 :
320 : // ################################
321 : // # Constructors for Operations
322 : // ################################
323 :
324 46 : ElektraIoFdOperation * elektraIoNewFdOperation (int fd, int flags, int enabled, ElektraIoFdCallback callback, void * privateData)
325 : {
326 46 : if (callback == NULL)
327 : {
328 : ELEKTRA_LOG_WARNING ("callback cannot be NULL");
329 : return NULL;
330 : }
331 :
332 46 : ElektraIoFdOperation * fdOp = elektraMalloc (sizeof (*fdOp));
333 46 : if (!fdOp)
334 : {
335 : ELEKTRA_LOG_WARNING ("elektraMalloc failed");
336 : return NULL;
337 : }
338 :
339 46 : fdOp->fd = fd;
340 46 : fdOp->flags = flags;
341 46 : fdOp->enabled = enabled;
342 46 : fdOp->callback = callback;
343 46 : fdOp->privateData = privateData;
344 46 : fdOp->binding = NULL;
345 46 : fdOp->bindingData = NULL;
346 :
347 46 : return fdOp;
348 : }
349 :
350 100 : ElektraIoTimerOperation * elektraIoNewTimerOperation (unsigned int interval, int enabled, ElektraIoTimerCallback callback,
351 : void * privateData)
352 : {
353 100 : if (callback == NULL)
354 : {
355 : ELEKTRA_LOG_WARNING ("callback cannot be NULL");
356 : return NULL;
357 : }
358 :
359 100 : ElektraIoTimerOperation * timerOp = elektraMalloc (sizeof (*timerOp));
360 100 : if (!timerOp)
361 : {
362 : ELEKTRA_LOG_WARNING ("elektraMalloc failed");
363 : return NULL;
364 : }
365 :
366 100 : timerOp->interval = interval;
367 100 : timerOp->enabled = enabled;
368 100 : timerOp->callback = callback;
369 100 : timerOp->privateData = privateData;
370 100 : timerOp->binding = NULL;
371 100 : timerOp->bindingData = NULL;
372 :
373 100 : return timerOp;
374 : }
375 :
376 52 : ElektraIoIdleOperation * elektraIoNewIdleOperation (int enabled, ElektraIoIdleCallback callback, void * privateData)
377 : {
378 52 : if (callback == NULL)
379 : {
380 : ELEKTRA_LOG_WARNING ("callback cannot be NULL");
381 : return NULL;
382 : }
383 :
384 52 : ElektraIoIdleOperation * idleOp = elektraMalloc (sizeof (*idleOp));
385 52 : if (!idleOp)
386 : {
387 : ELEKTRA_LOG_WARNING ("elektraMalloc failed");
388 : return NULL;
389 : }
390 :
391 52 : idleOp->enabled = enabled;
392 52 : idleOp->callback = callback;
393 52 : idleOp->privateData = privateData;
394 52 : idleOp->binding = NULL;
395 52 : idleOp->bindingData = NULL;
396 :
397 52 : return idleOp;
398 : }
399 :
400 :
401 : // ################################
402 : // # Setters
403 : // ################################
404 :
405 12 : int elektraIoFdSetEnabled (ElektraIoFdOperation * fdOp, int enabled)
406 : {
407 12 : if (fdOp == NULL)
408 : {
409 : ELEKTRA_LOG_WARNING ("operation was NULL");
410 : return 0;
411 : }
412 :
413 12 : fdOp->enabled = enabled;
414 12 : return 1;
415 : }
416 :
417 6 : int elektraIoFdSetFlags (ElektraIoFdOperation * fdOp, int flags)
418 : {
419 6 : if (fdOp == NULL)
420 : {
421 : ELEKTRA_LOG_WARNING ("operation was NULL");
422 : return 0;
423 : }
424 :
425 6 : fdOp->flags = flags; // TODO check if flags are valid
426 6 : return 1;
427 : }
428 :
429 12 : int elektraIoTimerSetEnabled (ElektraIoTimerOperation * timerOp, int enabled)
430 : {
431 12 : if (timerOp == NULL)
432 : {
433 : ELEKTRA_LOG_WARNING ("operation was NULL");
434 : return 0;
435 : }
436 :
437 12 : timerOp->enabled = enabled;
438 12 : return 1;
439 : }
440 :
441 6 : int elektraIoTimerSetInterval (ElektraIoTimerOperation * timerOp, unsigned int interval)
442 : {
443 6 : if (timerOp == NULL)
444 : {
445 : ELEKTRA_LOG_WARNING ("operation was NULL");
446 : return 0;
447 : }
448 :
449 6 : timerOp->interval = interval;
450 6 : return 1;
451 : }
452 :
453 26 : int elektraIoIdleSetEnabled (ElektraIoIdleOperation * idleOp, int enabled)
454 : {
455 26 : if (idleOp == NULL)
456 : {
457 : ELEKTRA_LOG_WARNING ("operation was NULL");
458 : return 0;
459 : }
460 :
461 26 : idleOp->enabled = enabled;
462 26 : return 1;
463 : }
464 :
465 : // ################################
466 : // # Getters
467 : // ################################
468 :
469 100 : int elektraIoFdGetFd (ElektraIoFdOperation * fdOp)
470 : {
471 100 : if (fdOp == NULL)
472 : {
473 : ELEKTRA_LOG_WARNING ("operation was NULL");
474 : return 0;
475 : }
476 :
477 100 : return fdOp->fd;
478 : }
479 :
480 6 : void * elektraIoFdGetData (ElektraIoFdOperation * fdOp)
481 : {
482 6 : if (fdOp == NULL)
483 : {
484 : ELEKTRA_LOG_WARNING ("operation was NULL");
485 : return NULL;
486 : }
487 :
488 6 : return fdOp->privateData;
489 : }
490 :
491 52 : int elektraIoFdSetBindingData (ElektraIoFdOperation * fdOp, void * data)
492 : {
493 52 : if (fdOp == NULL)
494 : {
495 : ELEKTRA_LOG_WARNING ("operation was NULL");
496 : return 0;
497 : }
498 :
499 52 : fdOp->bindingData = data;
500 52 : return 1;
501 : }
502 :
503 194 : void * elektraIoFdGetBindingData (ElektraIoFdOperation * fdOp)
504 : {
505 194 : if (fdOp == NULL)
506 : {
507 : ELEKTRA_LOG_WARNING ("operation was NULL");
508 : return NULL;
509 : }
510 :
511 194 : return fdOp->bindingData;
512 : }
513 :
514 34 : ElektraIoInterface * elektraIoFdGetBinding (ElektraIoFdOperation * fdOp)
515 : {
516 34 : if (fdOp == NULL)
517 : {
518 : ELEKTRA_LOG_WARNING ("operation was NULL");
519 : return NULL;
520 : }
521 :
522 34 : return fdOp->binding;
523 : }
524 :
525 98 : int elektraIoFdIsEnabled (ElektraIoFdOperation * fdOp)
526 : {
527 98 : if (fdOp == NULL)
528 : {
529 : ELEKTRA_LOG_WARNING ("operation was NULL");
530 : return 0;
531 : }
532 :
533 98 : return fdOp->enabled;
534 : }
535 :
536 90 : int elektraIoFdGetFlags (ElektraIoFdOperation * fdOp)
537 : {
538 90 : if (fdOp == NULL)
539 : {
540 : ELEKTRA_LOG_WARNING ("operation was NULL");
541 : return 0;
542 : }
543 :
544 90 : return fdOp->flags;
545 : }
546 :
547 36 : ElektraIoFdCallback elektraIoFdGetCallback (ElektraIoFdOperation * fdOp)
548 : {
549 36 : if (fdOp == NULL)
550 : {
551 : ELEKTRA_LOG_WARNING ("operation was NULL");
552 : return NULL;
553 : }
554 :
555 36 : return fdOp->callback;
556 : }
557 :
558 106 : int elektraIoTimerSetBindingData (ElektraIoTimerOperation * timerOp, void * data)
559 : {
560 106 : if (timerOp == NULL)
561 : {
562 : ELEKTRA_LOG_WARNING ("operation was NULL");
563 : return 0;
564 : }
565 :
566 106 : timerOp->bindingData = data;
567 106 : return 1;
568 : }
569 :
570 380 : void * elektraIoTimerGetBindingData (ElektraIoTimerOperation * timerOp)
571 : {
572 380 : if (timerOp == NULL)
573 : {
574 : ELEKTRA_LOG_WARNING ("operation was NULL");
575 : return NULL;
576 : }
577 :
578 380 : return timerOp->bindingData;
579 : }
580 :
581 108 : ElektraIoInterface * elektraIoTimerGetBinding (ElektraIoTimerOperation * timerOp)
582 : {
583 108 : if (timerOp == NULL)
584 : {
585 : ELEKTRA_LOG_WARNING ("operation was NULL");
586 : return NULL;
587 : }
588 :
589 108 : return timerOp->binding;
590 : }
591 :
592 0 : void * elektraIoTimerGetData (ElektraIoTimerOperation * timerOp)
593 : {
594 0 : if (timerOp == NULL)
595 : {
596 : ELEKTRA_LOG_WARNING ("operation was NULL");
597 : return NULL;
598 : }
599 :
600 0 : return timerOp->privateData;
601 : }
602 :
603 192 : int elektraIoTimerIsEnabled (ElektraIoTimerOperation * timerOp)
604 : {
605 192 : if (timerOp == NULL)
606 : {
607 : ELEKTRA_LOG_WARNING ("operation was NULL");
608 : return 0;
609 : }
610 :
611 192 : return timerOp->enabled;
612 : }
613 :
614 188 : unsigned int elektraIoTimerGetInterval (ElektraIoTimerOperation * timerOp)
615 : {
616 188 : if (timerOp == NULL)
617 : {
618 : ELEKTRA_LOG_WARNING ("operation was NULL");
619 : return 0;
620 : }
621 :
622 188 : return timerOp->interval;
623 : }
624 :
625 226 : ElektraIoTimerCallback elektraIoTimerGetCallback (ElektraIoTimerOperation * timerOp)
626 : {
627 226 : if (timerOp == NULL)
628 : {
629 : ELEKTRA_LOG_WARNING ("operation was NULL");
630 : return NULL;
631 : }
632 :
633 226 : return timerOp->callback;
634 : }
635 :
636 58 : int elektraIoIdleSetBindingData (ElektraIoIdleOperation * idleOp, void * data)
637 : {
638 58 : if (idleOp == NULL)
639 : {
640 : ELEKTRA_LOG_WARNING ("operation was NULL");
641 : return 0;
642 : }
643 :
644 58 : idleOp->bindingData = data;
645 58 : return 1;
646 : }
647 :
648 228 : void * elektraIoIdleGetBindingData (ElektraIoIdleOperation * idleOp)
649 : {
650 228 : if (idleOp == NULL)
651 : {
652 : ELEKTRA_LOG_WARNING ("operation was NULL");
653 : return NULL;
654 : }
655 :
656 228 : return idleOp->bindingData;
657 : }
658 :
659 40 : ElektraIoInterface * elektraIoIdleGetBinding (ElektraIoIdleOperation * idleOp)
660 : {
661 40 : if (idleOp == NULL)
662 : {
663 : ELEKTRA_LOG_WARNING ("operation was NULL");
664 : return NULL;
665 : }
666 :
667 40 : return idleOp->binding;
668 : }
669 :
670 0 : void * elektraIoIdleGetData (ElektraIoIdleOperation * idleOp)
671 : {
672 0 : if (idleOp == NULL)
673 : {
674 : ELEKTRA_LOG_WARNING ("operation was NULL");
675 : return NULL;
676 : }
677 :
678 0 : return idleOp->privateData;
679 : }
680 :
681 116 : int elektraIoIdleIsEnabled (ElektraIoIdleOperation * idleOp)
682 : {
683 116 : if (idleOp == NULL)
684 : {
685 : ELEKTRA_LOG_WARNING ("operation was NULL");
686 : return 0;
687 : }
688 :
689 116 : return idleOp->enabled;
690 : }
691 :
692 383743 : ElektraIoIdleCallback elektraIoIdleGetCallback (ElektraIoIdleOperation * idleOp)
693 : {
694 383743 : if (idleOp == NULL)
695 : {
696 : ELEKTRA_LOG_WARNING ("operation was NULL");
697 : return NULL;
698 : }
699 :
700 383743 : return idleOp->callback;
701 : }
|