[multipathd] superfluous unlock() in uevent discard code path
[platform/upstream/multipath-tools.git] / multipathd / main.c
1 /*
2  * Copyright (c) 2004, 2005 Christophe Varoqui
3  * Copyright (c) 2005 Kiyoshi Ueda, NEC
4  * Copyright (c) 2005 Benjamin Marzinski, Redhat
5  * Copyright (c) 2005 Edward Goggin, EMC
6  */
7 #include <unistd.h>
8 #include <sys/stat.h>
9 #include <libdevmapper.h>
10 #include <wait.h>
11 #include <sys/mman.h>
12 #include <sys/types.h>
13 #include <fcntl.h>
14 #include <errno.h>
15
16 /*
17  * libsysfs
18  */
19 #include <sysfs/libsysfs.h>
20 #include <sysfs/dlist.h>
21
22 /*
23  * libcheckers
24  */
25 #include <checkers.h>
26 #include <path_state.h>
27
28 /*
29  * libmultipath
30  */
31 #include <parser.h>
32 #include <vector.h>
33 #include <memory.h>
34 #include <config.h>
35 #include <callout.h>
36 #include <util.h>
37 #include <blacklist.h>
38 #include <hwtable.h>
39 #include <defaults.h>
40 #include <structs.h>
41 #include <dmparser.h>
42 #include <devmapper.h>
43 #include <dict.h>
44 #include <discovery.h>
45 #include <debug.h>
46 #include <propsel.h>
47 #include <uevent.h>
48 #include <switchgroup.h>
49 #include <path_state.h>
50 #include <print.h>
51
52 #include "main.h"
53 #include "pidfile.h"
54 #include "uxlsnr.h"
55 #include "uxclnt.h"
56 #include "cli.h"
57 #include "cli_handlers.h"
58
59 #define FILE_NAME_SIZE 256
60 #define CMDSIZE 160
61
62 #define LOG_MSG(a,b) \
63         if (strlen(b)) { \
64                 condlog(a, "%s: %s", pp->dev_t, b); \
65                 memset(b, 0, MAX_CHECKER_MSG_SIZE); \
66         }
67
68 #ifdef LCKDBG
69 #define lock(a) \
70         fprintf(stderr, "%s:%s(%i) lock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
71         pthread_mutex_lock(a)
72 #define unlock(a) \
73         fprintf(stderr, "%s:%s(%i) unlock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
74         pthread_mutex_unlock(a)
75 #define lock_cleanup_pop(a) \
76         fprintf(stderr, "%s:%s(%i) unlock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
77         pthread_cleanup_pop(1);
78 #else
79 #define lock(a) pthread_mutex_lock(a)
80 #define unlock(a) pthread_mutex_unlock(a)
81 #define lock_cleanup_pop(a) pthread_cleanup_pop(1);
82 #endif
83
84 pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
85 pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
86
87 /*
88  * structs
89  */
90 struct event_thread {
91         struct dm_task *dmt;
92         pthread_t thread;
93         int event_nr;
94         char mapname[WWID_SIZE];
95         struct vectors *vecs;
96 };
97
98 static struct event_thread *
99 alloc_waiter (void)
100 {
101
102         struct event_thread * wp;
103
104         wp = (struct event_thread *)MALLOC(sizeof(struct event_thread));
105
106         return wp;
107 }
108
109 static void
110 free_waiter (void * data)
111 {
112         struct event_thread * wp = (struct event_thread *)data;
113
114         if (wp->dmt)
115                 dm_task_destroy(wp->dmt);
116         FREE(wp);
117 }
118
119 static void
120 stop_waiter_thread (struct multipath * mpp, struct vectors * vecs)
121 {
122         struct event_thread * wp = (struct event_thread *)mpp->waiter;
123         pthread_t thread;
124         
125         if (!wp) {
126                 condlog(3, "%s: no waiter thread", mpp->alias);
127                 return;
128         }
129         thread = wp->thread;
130
131         if (!wp) {
132                 condlog(3, "%s: thread not started", mpp->alias);
133                 return;
134         }
135         condlog(2, "%s: stop event checker thread", wp->mapname);
136         pthread_kill(thread, SIGHUP);
137 }
138
139 static void
140 cleanup_lock (void * data)
141 {
142         pthread_mutex_unlock((pthread_mutex_t *)data);
143 }
144
145 static void
146 adopt_paths (struct vectors * vecs, struct multipath * mpp)
147 {
148         int i;
149         struct path * pp;
150
151         if (!mpp)
152                 return;
153
154         vector_foreach_slot (vecs->pathvec, pp, i) {
155                 if (!strncmp(mpp->wwid, pp->wwid, WWID_SIZE)) {
156                         condlog(4, "%s ownership set", pp->dev_t);
157                         pp->mpp = mpp;
158                 }
159         }
160 }
161
162 static void
163 orphan_path (struct path * pp)
164 {
165         pp->mpp = NULL;
166         pp->checkfn = NULL;
167         pp->dmstate = PSTATE_UNDEF;
168         pp->checker_context = NULL;
169         pp->getuid = NULL;
170         pp->getprio = NULL;
171
172         if (pp->fd >= 0)
173                 close(pp->fd);
174
175         pp->fd = -1;
176 }
177
178 static void
179 orphan_paths (struct vectors * vecs, struct multipath * mpp)
180 {
181         int i;
182         struct path * pp;
183
184         vector_foreach_slot (vecs->pathvec, pp, i) {
185                 if (pp->mpp == mpp) {
186                         condlog(4, "%s is orphaned", pp->dev_t);
187                         orphan_path(pp);
188                 }
189         }
190 }
191
192 static int
193 update_multipath_table (struct multipath *mpp, vector pathvec)
194 {
195         if (!mpp)
196                 return 1;
197
198         if (dm_get_map(mpp->alias, &mpp->size, mpp->params))
199                 return 1;
200
201         if (disassemble_map(pathvec, mpp->params, mpp))
202                 return 1;
203
204         return 0;
205 }
206
207 static int
208 update_multipath_status (struct multipath *mpp)
209 {
210         if (!mpp)
211                 return 1;
212
213         if(dm_get_status(mpp->alias, mpp->status))
214                 return 1;
215
216         if (disassemble_status(mpp->status, mpp))
217                 return 1;
218
219         return 0;
220 }
221
222 static int
223 update_multipath_strings (struct multipath *mpp, vector pathvec)
224 {
225         if (mpp->selector) {
226                 FREE(mpp->selector);
227                 mpp->selector = NULL;
228         }
229
230         if (mpp->features) {
231                 FREE(mpp->features);
232                 mpp->features = NULL;
233         }
234
235         if (mpp->hwhandler) {
236                 FREE(mpp->hwhandler);
237                 mpp->hwhandler = NULL;
238         }
239
240         free_pgvec(mpp->pg, KEEP_PATHS);
241         mpp->pg = NULL;
242
243         if (update_multipath_table(mpp, pathvec))
244                 return 1;
245
246         if (update_multipath_status(mpp))
247                 return 1;
248
249         return 0;
250 }
251
252 static void
253 set_multipath_wwid (struct multipath * mpp)
254 {
255         if (mpp->wwid)
256                 return;
257
258         dm_get_uuid(mpp->alias, mpp->wwid);
259 }
260
261 static int
262 pathcount (struct multipath *mpp, int state)
263 {
264         struct pathgroup *pgp;
265         struct path *pp;
266         int i, j;
267         int count = 0;
268
269         vector_foreach_slot (mpp->pg, pgp, i)
270                 vector_foreach_slot (pgp->paths, pp, j)
271                         if (pp->state == state)
272                                 count++;
273         return count;
274 }
275
276 /*
277  * mpp->no_path_retry:
278  *   -2 (QUEUE) : queue_if_no_path enabled, never turned off
279  *   -1 (FAIL)  : fail_if_no_path
280  *    0 (UNDEF) : nothing
281  *   >0         : queue_if_no_path enabled, turned off after polling n times
282  */
283 static void
284 update_queue_mode_del_path(struct multipath *mpp)
285 {
286         if (--mpp->nr_active == 0 && mpp->no_path_retry > 0) {
287                 /*
288                  * Enter retry mode.
289                  * meaning of +1: retry_tick may be decremented in
290                  *                checkerloop before starting retry.
291                  */
292                 mpp->retry_tick = mpp->no_path_retry * conf->checkint + 1;
293                 condlog(1, "%s: Entering recovery mode: max_retries=%d",
294                         mpp->alias, mpp->no_path_retry);
295         }
296         condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
297 }
298
299 static void
300 update_queue_mode_add_path(struct multipath *mpp)
301 {
302         if (mpp->nr_active++ == 0 && mpp->no_path_retry > 0) {
303                 /* come back to normal mode from retry mode */
304                 mpp->retry_tick = 0;
305                 dm_queue_if_no_path(mpp->alias, 1);
306                 condlog(2, "%s: queue_if_no_path enabled", mpp->alias);
307                 condlog(1, "%s: Recovered to normal mode", mpp->alias);
308         }
309         condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
310 }
311
312 static void
313 set_no_path_retry(struct multipath *mpp)
314 {
315         mpp->retry_tick = 0;
316         mpp->nr_active = pathcount(mpp, PATH_UP);
317         select_no_path_retry(mpp);
318
319         switch (mpp->no_path_retry) {
320         case NO_PATH_RETRY_UNDEF:
321                 break;
322         case NO_PATH_RETRY_FAIL:
323                 dm_queue_if_no_path(mpp->alias, 0);
324                 break;
325         case NO_PATH_RETRY_QUEUE:
326                 dm_queue_if_no_path(mpp->alias, 1);
327                 break;
328         default:
329                 dm_queue_if_no_path(mpp->alias, 1);
330                 if (mpp->nr_active == 0) {
331                         /* Enter retry mode */
332                         mpp->retry_tick = mpp->no_path_retry * conf->checkint;
333                         condlog(1, "%s: Entering recovery mode: max_retries=%d",
334                                 mpp->alias, mpp->no_path_retry);
335                 }
336                 break;
337         }
338 }
339
340 static struct hwentry *
341 extract_hwe_from_path(struct multipath * mpp)
342 {
343         struct path * pp;
344         struct pathgroup * pgp;
345
346         pgp = VECTOR_SLOT(mpp->pg, 0);
347         pp = VECTOR_SLOT(pgp->paths, 0);
348
349         return pp->hwe;
350 }
351
352 static void
353 remove_map (struct multipath * mpp, struct vectors * vecs)
354 {
355         int i;
356
357         stop_waiter_thread(mpp, vecs);
358
359         /*
360          * clear references to this map
361          */
362         orphan_paths(vecs, mpp);
363
364         /*
365          * purge the multipath vector
366          */
367         i = find_slot(vecs->mpvec, (void *)mpp);
368         vector_del_slot(vecs->mpvec, i);
369
370         /*
371          * final free
372          */
373         free_multipath(mpp, KEEP_PATHS);
374         mpp = NULL;
375 }
376
377 static void
378 remove_maps (struct vectors * vecs)
379 {
380         int i;
381         struct multipath * mpp;
382
383         vector_foreach_slot (vecs->mpvec, mpp, i) {
384                 remove_map(mpp, vecs);
385                 i--;
386         }
387
388         vector_free(vecs->mpvec);
389         vecs->mpvec = NULL;
390 }
391
392 static int
393 setup_multipath (struct vectors * vecs, struct multipath * mpp)
394 {
395         if (dm_get_info(mpp->alias, &mpp->dmi))
396                 goto out;
397
398         set_multipath_wwid(mpp);
399         mpp->mpe = find_mpe(mpp->wwid);
400         condlog(4, "discovered map %s", mpp->alias);
401
402         if (update_multipath_strings(mpp, vecs->pathvec))
403                 goto out;
404
405         adopt_paths(vecs, mpp);
406         mpp->hwe = extract_hwe_from_path(mpp);
407         select_pgfailback(mpp);
408         set_no_path_retry(mpp);
409
410         return 0;
411 out:
412         condlog(0, "%s: failed to setup multipath", mpp->alias);
413         remove_map(mpp, vecs);
414         return 1;
415 }
416
417 static int
418 need_switch_pathgroup (struct multipath * mpp, int refresh)
419 {
420         struct pathgroup * pgp;
421         struct path * pp;
422         int i, j;
423
424         if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
425                 return 0;
426
427         /*
428          * Refresh path priority values
429          */
430         if (refresh)
431                 vector_foreach_slot (mpp->pg, pgp, i)
432                         vector_foreach_slot (pgp->paths, pp, j)
433                                 pathinfo(pp, conf->hwtable, DI_PRIO);
434
435         mpp->bestpg = select_path_group(mpp);
436
437         if (mpp->bestpg != mpp->nextpg)
438                 return 1;
439
440         return 0;
441 }
442
443 static void
444 switch_pathgroup (struct multipath * mpp)
445 {
446         dm_switchgroup(mpp->alias, mpp->bestpg);
447         condlog(2, "%s: switch to path group #%i",
448                  mpp->alias, mpp->bestpg);
449 }
450
451 static int
452 update_multipath (struct vectors *vecs, char *mapname)
453 {
454         struct multipath *mpp;
455         struct pathgroup  *pgp;
456         struct path *pp;
457         int i, j;
458         int r = 1;
459
460         mpp = find_mp_by_alias(vecs->mpvec, mapname);
461
462         if (!mpp)
463                 goto out;
464
465         free_pgvec(mpp->pg, KEEP_PATHS);
466         mpp->pg = NULL;
467
468         if (setup_multipath(vecs, mpp))
469                 goto out; /* mpp freed in setup_multipath */
470
471         /*
472          * compare checkers states with DM states
473          */
474         vector_foreach_slot (mpp->pg, pgp, i) {
475                 vector_foreach_slot (pgp->paths, pp, j) {
476                         if (pp->dmstate != PSTATE_FAILED)
477                                 continue;
478
479                         if (pp->state != PATH_DOWN) {
480                                 condlog(2, "%s: mark as failed", pp->dev_t);
481                                 pp->state = PATH_DOWN;
482                                 update_queue_mode_del_path(mpp);
483
484                                 /*
485                                  * if opportune,
486                                  * schedule the next check earlier
487                                  */
488                                 if (pp->tick > conf->checkint)
489                                         pp->tick = conf->checkint;
490                         }
491                 }
492         }
493         r = 0;
494 out:
495         if (r)
496                 condlog(0, "failed to update multipath");
497
498         return r;
499 }
500
501 static sigset_t unblock_sighup(void)
502 {
503         sigset_t set, old;
504
505         sigemptyset(&set);
506         sigaddset(&set, SIGHUP);
507         pthread_sigmask(SIG_UNBLOCK, &set, &old);
508         return old;
509 }
510
511 /*
512  * returns the reschedule delay
513  * negative means *stop*
514  */
515 static int
516 waiteventloop (struct event_thread * waiter)
517 {
518         sigset_t set;
519         int event_nr;
520         int r;
521
522         if (!waiter->event_nr)
523                 waiter->event_nr = dm_geteventnr(waiter->mapname);
524
525         if (!(waiter->dmt = dm_task_create(DM_DEVICE_WAITEVENT)))
526                 return 1;
527
528         if (!dm_task_set_name(waiter->dmt, waiter->mapname)) {
529                 dm_task_destroy(waiter->dmt);
530                 return 1;
531         }
532
533         if (waiter->event_nr && !dm_task_set_event_nr(waiter->dmt,
534                                                       waiter->event_nr)) {
535                 dm_task_destroy(waiter->dmt);
536                 return 1;
537         }
538
539         dm_task_no_open_count(waiter->dmt);
540         
541         /* accept wait interruption */
542         set = unblock_sighup();
543
544         /* interruption spits messages */
545         dm_shut_log();
546
547         /* wait */
548         r = dm_task_run(waiter->dmt);
549
550         /* wait is over : event or interrupt */
551         pthread_sigmask(SIG_SETMASK, &set, NULL);
552         //dm_restore_log();
553
554         if (!r) /* wait interrupted by signal */
555                 return -1;
556
557         dm_task_destroy(waiter->dmt);
558         waiter->dmt = NULL;
559         waiter->event_nr++;
560
561         /*
562          * upon event ...
563          */
564         while (1) {
565                 condlog(3, "%s: devmap event #%i",
566                                 waiter->mapname, waiter->event_nr);
567
568                 /*
569                  * event might be :
570                  *
571                  * 1) a table reload, which means our mpp structure is
572                  *    obsolete : refresh it through update_multipath()
573                  * 2) a path failed by DM : mark as such through
574                  *    update_multipath()
575                  * 3) map has gone away : stop the thread.
576                  * 4) a path reinstate : nothing to do
577                  * 5) a switch group : nothing to do
578                  */
579                 pthread_cleanup_push(cleanup_lock, waiter->vecs->lock);
580                 lock(waiter->vecs->lock);
581                 r = update_multipath(waiter->vecs, waiter->mapname);
582                 lock_cleanup_pop(waiter->vecs->lock);
583
584                 if (r)
585                         return -1; /* stop the thread */
586
587                 event_nr = dm_geteventnr(waiter->mapname);
588
589                 if (waiter->event_nr == event_nr)
590                         return 1; /* upon problem reschedule 1s later */
591
592                 waiter->event_nr = event_nr;
593         }
594         return -1; /* never reach there */
595 }
596
597 static void *
598 waitevent (void * et)
599 {
600         int r;
601         struct event_thread *waiter;
602
603         mlockall(MCL_CURRENT | MCL_FUTURE);
604
605         waiter = (struct event_thread *)et;
606         pthread_cleanup_push(free_waiter, et);
607
608         while (1) {
609                 r = waiteventloop(waiter);
610
611                 if (r < 0)
612                         break;
613
614                 sleep(r);
615         }
616
617         pthread_cleanup_pop(1);
618         return NULL;
619 }
620
621 static int
622 start_waiter_thread (struct multipath * mpp, struct vectors * vecs)
623 {
624         pthread_attr_t attr;
625         struct event_thread * wp;
626
627         if (!mpp)
628                 return 0;
629
630         if (pthread_attr_init(&attr))
631                 goto out;
632
633         pthread_attr_setstacksize(&attr, 32 * 1024);
634         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
635
636         wp = alloc_waiter();
637
638         if (!wp)
639                 goto out;
640
641         mpp->waiter = (void *)wp;
642         strncpy(wp->mapname, mpp->alias, WWID_SIZE);
643         wp->vecs = vecs;
644
645         if (pthread_create(&wp->thread, &attr, waitevent, wp)) {
646                 condlog(0, "%s: cannot create event checker", wp->mapname);
647                 goto out1;
648         }
649         condlog(2, "%s: event checker started", wp->mapname);
650
651         return 0;
652 out1:
653         free_waiter(wp);
654         mpp->waiter = NULL;
655 out:
656         condlog(0, "failed to start waiter thread");
657         return 1;
658 }
659
660 int
661 uev_add_map (char * devname, struct vectors * vecs)
662 {
663         int major, minor;
664         char dev_t[BLK_DEV_SIZE];
665         char * alias;
666         struct multipath * mpp;
667
668         if (sscanf(devname, "dm-%d", &minor) == 1 &&
669             !sysfs_get_dev(sysfs_path, devname, dev_t, BLK_DEV_SIZE) &&
670             sscanf(dev_t, "%d:%d", &major, &minor) == 2)
671                 alias = dm_mapname(major, minor);
672         else
673                 alias = STRDUP(devname);
674                 
675         if (!alias)
676                 return 1;
677         
678         if (!dm_type(alias, DEFAULT_TARGET)) {
679                 condlog(4, "%s: not a multipath map", alias);
680                 FREE(alias);
681                 return 0;
682         }
683
684         mpp = find_mp_by_alias(vecs->mpvec, alias);
685
686         if (mpp) {
687                 /*
688                  * this should not happen,
689                  * we missed a remove map event (not sent ?)
690                  */
691                 condlog(2, "%s: already registered", alias);
692                 remove_map(mpp, vecs);
693         }
694
695         /*
696          * now we can allocate
697          */
698         mpp = alloc_multipath();
699
700         if (!mpp)
701                 return 1;
702
703         mpp->alias = alias;
704
705         if (setup_multipath(vecs, mpp))
706                 return 1; /* mpp freed in setup_multipath */
707
708         if (!vector_alloc_slot(vecs->mpvec))
709                 goto out;
710
711         vector_set_slot(vecs->mpvec, mpp);
712         adopt_paths(vecs, mpp);
713
714         if (start_waiter_thread(mpp, vecs))
715                 goto out;
716
717         return 0;
718 out:
719         condlog(2, "%s: add devmap failed", mpp->alias);
720         remove_map(mpp, vecs);
721         return 1;
722 }
723
724 int
725 uev_remove_map (char * devname, struct vectors * vecs)
726 {
727         int minor;
728         struct multipath * mpp;
729
730         if (sscanf(devname, "dm-%d", &minor) == 1)
731                 mpp = find_mp_by_minor(vecs->mpvec, minor);
732         else
733                 mpp = find_mp_by_alias(vecs->mpvec, devname);
734
735         if (!mpp) {
736                 condlog(3, "%s: devmap not registered, can't remove",
737                         devname);
738                 return 0;
739         }
740
741         condlog(2, "remove %s devmap", mpp->alias);
742         remove_map(mpp, vecs);
743
744         return 0;
745 }
746
747 int
748 uev_add_path (char * devname, struct vectors * vecs)
749 {
750         struct path * pp;
751
752         pp = find_path_by_dev(vecs->pathvec, devname);
753
754         if (pp) {
755                 condlog(3, "%s: already in pathvec");
756                 return 1;
757         }
758         pp = store_pathinfo(vecs->pathvec, conf->hwtable,
759                        devname, DI_SYSFS | DI_WWID);
760
761         if (!pp) {
762                 condlog(0, "%s: failed to store path info", devname);
763                 return 1;
764         }
765
766         condlog(2, "%s: path checker registered", devname);
767         pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
768
769         if (pp->mpp) {
770                 condlog(4, "%s: ownership set to %s",
771                                 pp->dev_t, pp->mpp->alias);
772         } else {
773                 condlog(4, "%s: orphaned", pp->dev_t);
774                 orphan_path(pp);
775         }
776
777         return 0;
778 }
779
780 int
781 uev_remove_path (char * devname, struct vectors * vecs)
782 {
783         int i;
784         struct path * pp;
785
786         pp = find_path_by_dev(vecs->pathvec, devname);
787
788         if (!pp) {
789                 condlog(3, "%s: not in pathvec");
790                 return 1;
791         }
792
793         if (pp->mpp && pp->state == PATH_UP)
794                 update_queue_mode_del_path(pp->mpp);
795
796         condlog(2, "remove %s path checker", devname);
797         i = find_slot(vecs->pathvec, (void *)pp);
798         vector_del_slot(vecs->pathvec, i);
799         free_path(pp);
800
801         return 0;
802 }
803
804 int
805 show_paths (char ** r, int * len, struct vectors * vecs)
806 {
807         int i;
808         struct path * pp;
809         char * c;
810         char * reply;
811         struct path_layout pl;
812
813         get_path_layout(&pl, vecs->pathvec);
814         reply = MALLOC(MAX_REPLY_LEN);
815
816         if (!reply)
817                 return 1;
818
819         c = reply;
820
821         if (VECTOR_SIZE(vecs->pathvec) > 0)
822                 c += snprint_path_header(c, reply + MAX_REPLY_LEN - c,
823                                          PRINT_PATH_CHECKER, &pl);
824
825         vector_foreach_slot(vecs->pathvec, pp, i)
826                 c += snprint_path(c, reply + MAX_REPLY_LEN - c,
827                                   PRINT_PATH_CHECKER, pp, &pl);
828
829         *r = reply;
830         *len = (int)(c - reply + 1);
831         return 0;
832 }
833
834 int
835 show_maps (char ** r, int *len, struct vectors * vecs)
836 {
837         int i;
838         struct multipath * mpp;
839         char * c;
840         char * reply;
841         struct map_layout ml;
842
843         get_map_layout(&ml, vecs->mpvec);
844         reply = MALLOC(MAX_REPLY_LEN);
845
846         if (!reply)
847                 return 1;
848
849         c = reply;
850         if (VECTOR_SIZE(vecs->mpvec) > 0)
851                 c += snprint_map_header(c, reply + MAX_REPLY_LEN - c,
852                                         PRINT_MAP_FAILBACK, &ml);
853
854         vector_foreach_slot(vecs->mpvec, mpp, i)
855                 c += snprint_map(c, reply + MAX_REPLY_LEN - c,
856                                  PRINT_MAP_FAILBACK, mpp, &ml);
857
858         *r = reply;
859         *len = (int)(c - reply + 1);
860         return 0;
861 }
862
863 int
864 dump_pathvec (char ** r, int * len, struct vectors * vecs)
865 {
866         int i;
867         struct path * pp;
868         char * reply;
869         char * p;
870
871         *len = VECTOR_SIZE(vecs->pathvec) * sizeof(struct path);
872         reply = (char *)MALLOC(*len);
873         *r = reply;
874
875         if (!reply)
876                 return 1;
877
878         p = reply;
879
880         vector_foreach_slot (vecs->pathvec, pp, i) {
881                 memcpy((void *)p, pp, sizeof(struct path));
882                 p += sizeof(struct path);
883         }
884
885         /* return negative to hint caller not to add "ok" to the dump */
886         return -1;
887 }
888
889 static int
890 map_discovery (struct vectors * vecs)
891 {
892         int i;
893         struct multipath * mpp;
894
895         if (dm_get_maps(vecs->mpvec, "multipath"))
896                 return 1;
897
898         vector_foreach_slot (vecs->mpvec, mpp, i) {
899                 if (setup_multipath(vecs, mpp))
900                         return 1;
901                 start_waiter_thread(mpp, vecs);
902         }
903
904         return 0;
905 }
906
907 int
908 reconfigure (struct vectors * vecs)
909 {
910         struct config * old = conf;
911         struct multipath * mpp;
912         struct path * pp;
913         int i;
914
915         conf = NULL;
916
917         if (load_config(DEFAULT_CONFIGFILE)) {
918                 conf = old;
919                 condlog(2, "reconfigure failed, continue with old config");
920                 return 1;
921         }
922         conf->verbosity = old->verbosity;
923         free_config(old);
924
925         vector_foreach_slot (vecs->mpvec, mpp, i) {
926                 mpp->mpe = find_mpe(mpp->wwid);
927                 mpp->hwe = extract_hwe_from_path(mpp);
928                 adopt_paths(vecs, mpp);
929                 set_no_path_retry(mpp);
930         }
931         vector_foreach_slot (vecs->pathvec, pp, i) {
932                 select_checkfn(pp);
933                 select_getuid(pp);
934                 select_getprio(pp);
935         }
936         condlog(2, "reconfigured");
937         return 0;
938 }
939
940 int
941 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
942 {
943         struct vectors * vecs;
944         int r;
945         
946         *reply = NULL;
947         *len = 0;
948         vecs = (struct vectors *)trigger_data;
949
950         pthread_cleanup_push(cleanup_lock, vecs->lock);
951         lock(vecs->lock);
952
953         r = parse_cmd(str, reply, len, vecs);
954
955         if (r > 0) {
956                 *reply = STRDUP("fail\n");
957                 *len = strlen(*reply) + 1;
958                 r = 1;
959         }
960         else if (!r && *len == 0) {
961                 *reply = STRDUP("ok\n");
962                 *len = strlen(*reply) + 1;
963                 r = 0;
964         }
965         /* else if (r < 0) leave *reply alone */
966
967         lock_cleanup_pop(vecs->lock);
968         return r;
969 }
970
971 static int
972 uev_discard(char * devpath)
973 {
974         char a[10], b[10];
975
976         /*
977          * keep only block devices, discard partitions
978          */
979         if (sscanf(devpath, "/block/%10s", a) != 1 ||
980             sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
981                 condlog(4, "discard event on %s", devpath);
982                 return 1;
983         }
984         return 0;
985 }
986
987 int 
988 uev_trigger (struct uevent * uev, void * trigger_data)
989 {
990         int r = 0;
991         char devname[32];
992         struct vectors * vecs;
993
994         vecs = (struct vectors *)trigger_data;
995
996         if (uev_discard(uev->devpath))
997                 return 1;
998
999         basename(uev->devpath, devname);
1000         lock(vecs->lock);
1001
1002         /*
1003          * device map add/remove event
1004          */
1005         if (!strncmp(devname, "dm-", 3)) {
1006                 if (!strncmp(uev->action, "add", 3)) {
1007                         r = uev_add_map(devname, vecs);
1008                         goto out;
1009                 }
1010 #if 0
1011                 if (!strncmp(uev->action, "remove", 6)) {
1012                         r = uev_remove_map(devname, vecs);
1013                         goto out;
1014                 }
1015 #endif
1016                 goto out;
1017         }
1018         
1019         /*
1020          * path add/remove event
1021          */
1022         if (blacklist(conf->blist, devname))
1023                 goto out;
1024
1025         if (!strncmp(uev->action, "add", 3)) {
1026                 r = uev_add_path(devname, vecs);
1027                 goto out;
1028         }
1029         if (!strncmp(uev->action, "remove", 6)) {
1030                 r = uev_remove_path(devname, vecs);
1031                 goto out;
1032         }
1033
1034 out:
1035         unlock(vecs->lock);
1036         return r;
1037 }
1038
1039 static void *
1040 ueventloop (void * ap)
1041 {
1042         if (uevent_listen(&uev_trigger, ap))
1043                 fprintf(stderr, "error starting uevent listener");
1044                 
1045         return NULL;
1046 }
1047
1048 static void *
1049 uxlsnrloop (void * ap)
1050 {
1051         if (load_keys())
1052                 return NULL;
1053         
1054         if (alloc_handlers())
1055                 return NULL;
1056
1057         add_handler(LIST+PATHS, cli_list_paths);
1058         add_handler(LIST+MAPS, cli_list_maps);
1059         add_handler(ADD+PATH, cli_add_path);
1060         add_handler(DEL+PATH, cli_del_path);
1061         add_handler(ADD+MAP, cli_add_map);
1062         add_handler(DEL+MAP, cli_del_map);
1063         add_handler(SWITCH+MAP+GROUP, cli_switch_group);
1064         add_handler(DUMP+PATHVEC, cli_dump_pathvec);
1065         add_handler(RECONFIGURE, cli_reconfigure);
1066         add_handler(SUSPEND+MAP, cli_suspend);
1067         add_handler(RESUME+MAP, cli_resume);
1068         add_handler(REINSTATE+PATH, cli_reinstate);
1069         add_handler(FAIL+PATH, cli_fail);
1070
1071         uxsock_listen(&uxsock_trigger, ap);
1072
1073         return NULL;
1074 }
1075
1076 static int
1077 exit_daemon (int status)
1078 {
1079         if (status != 0)
1080                 fprintf(stderr, "bad exit status. see daemon.log\n");
1081
1082         condlog(3, "unlink pidfile");
1083         unlink(DEFAULT_PIDFILE);
1084
1085         lock(&exit_mutex);
1086         pthread_cond_signal(&exit_cond);
1087         unlock(&exit_mutex);
1088
1089         return status;
1090 }
1091
1092 static void
1093 fail_path (struct path * pp)
1094 {
1095         if (!pp->mpp)
1096                 return;
1097
1098         condlog(2, "checker failed path %s in map %s",
1099                  pp->dev_t, pp->mpp->alias);
1100
1101         dm_fail_path(pp->mpp->alias, pp->dev_t);
1102         update_queue_mode_del_path(pp->mpp);
1103 }
1104
1105 /*
1106  * caller must have locked the path list before calling that function
1107  */
1108 static void
1109 reinstate_path (struct path * pp)
1110 {
1111         if (!pp->mpp)
1112                 return;
1113
1114         if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
1115                 condlog(0, "%s: reinstate failed", pp->dev_t);
1116         else {
1117                 condlog(2, "%s: reinstated", pp->dev_t);
1118                 update_queue_mode_add_path(pp->mpp);
1119         }
1120 }
1121
1122 static void
1123 enable_group(struct path * pp)
1124 {
1125         struct pathgroup * pgp;
1126
1127         /*
1128          * if path is added through uev_add_path, pgindex can be unset.
1129          * next update_strings() will set it, upon map reload event.
1130          *
1131          * we can safely return here, because upon map reload, all
1132          * PG will be enabled.
1133          */
1134         if (!pp->mpp->pg || !pp->pgindex)
1135                 return;
1136
1137         pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1138         
1139         if (pgp->status == PGSTATE_DISABLED) {
1140                 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1141                 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1142         }
1143 }
1144
1145 static void
1146 mpvec_garbage_collector (struct vectors * vecs)
1147 {
1148         struct multipath * mpp;
1149         int i;
1150
1151         vector_foreach_slot (vecs->mpvec, mpp, i) {
1152                 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1153                         condlog(2, "%s: remove dead map", mpp->alias);
1154                         remove_map(mpp, vecs);
1155                         i--;
1156                 }
1157         }
1158 }
1159
1160 static void
1161 defered_failback_tick (vector mpvec)
1162 {
1163         struct multipath * mpp;
1164         int i;
1165
1166         vector_foreach_slot (mpvec, mpp, i) {
1167                 /*
1168                  * defered failback getting sooner
1169                  */
1170                 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1171                         mpp->failback_tick--;
1172
1173                         if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1174                                 switch_pathgroup(mpp);
1175                 }
1176         }
1177 }
1178
1179 static void
1180 retry_count_tick(vector mpvec)
1181 {
1182         struct multipath *mpp;
1183         int i;
1184
1185         vector_foreach_slot (mpvec, mpp, i) {
1186                 if (mpp->retry_tick) {
1187                         condlog(4, "%s: Retrying.. No active path", mpp->alias);
1188                         if(--mpp->retry_tick == 0) {
1189                                 dm_queue_if_no_path(mpp->alias, 0);
1190                                 condlog(2, "%s: Disable queueing", mpp->alias);
1191                         }
1192                 }
1193         }
1194 }
1195
1196 static void *
1197 checkerloop (void *ap)
1198 {
1199         struct vectors *vecs;
1200         struct path *pp;
1201         int i, count = 0;
1202         int newstate;
1203         char checker_msg[MAX_CHECKER_MSG_SIZE];
1204
1205         mlockall(MCL_CURRENT | MCL_FUTURE);
1206
1207         memset(checker_msg, 0, MAX_CHECKER_MSG_SIZE);
1208         vecs = (struct vectors *)ap;
1209
1210         condlog(2, "path checkers start up");
1211
1212         /*
1213          * init the path check interval
1214          */
1215         vector_foreach_slot (vecs->pathvec, pp, i) {
1216                 pp->checkint = conf->checkint;
1217         }
1218
1219         while (1) {
1220                 pthread_cleanup_push(cleanup_lock, vecs->lock);
1221                 lock(vecs->lock);
1222                 condlog(4, "tick");
1223
1224                 vector_foreach_slot (vecs->pathvec, pp, i) {
1225                         if (!pp->mpp)
1226                                 continue;
1227
1228                         if (pp->tick && --pp->tick)
1229                                 continue; /* don't check this path yet */
1230
1231                         /*
1232                          * provision a next check soonest,
1233                          * in case we exit abnormaly from here
1234                          */
1235                         pp->tick = conf->checkint;
1236                         
1237                         if (!pp->checkfn) {
1238                                 pathinfo(pp, conf->hwtable, DI_SYSFS);
1239                                 select_checkfn(pp);
1240                         }
1241
1242                         if (!pp->checkfn) {
1243                                 condlog(0, "%s: checkfn is void", pp->dev);
1244                                 continue;
1245                         }
1246                         newstate = pp->checkfn(pp->fd, checker_msg,
1247                                                &pp->checker_context);
1248                         
1249                         if (newstate < 0) {
1250                                 condlog(2, "%s: unusable path", pp->dev);
1251                                 pathinfo(pp, conf->hwtable, 0);
1252                                 continue;
1253                         }
1254
1255                         if (newstate != pp->state) {
1256                                 pp->state = newstate;
1257                                 LOG_MSG(1, checker_msg);
1258
1259                                 /*
1260                                  * upon state change, reset the checkint
1261                                  * to the shortest delay
1262                                  */
1263                                 pp->checkint = conf->checkint;
1264
1265                                 if (newstate == PATH_DOWN ||
1266                                     newstate == PATH_SHAKY ||
1267                                     update_multipath_strings(pp->mpp,
1268                                                              vecs->pathvec)) {
1269                                         /*
1270                                          * proactively fail path in the DM
1271                                          */
1272                                         fail_path(pp);
1273
1274                                         /*
1275                                          * cancel scheduled failback
1276                                          */
1277                                         pp->mpp->failback_tick = 0;
1278
1279                                         continue;
1280                                 }
1281
1282                                 /*
1283                                  * reinstate this path
1284                                  */
1285                                 reinstate_path(pp);
1286
1287                                 /*
1288                                  * schedule [defered] failback
1289                                  */
1290                                 if (pp->mpp->pgfailback > 0)
1291                                         pp->mpp->failback_tick =
1292                                                 pp->mpp->pgfailback + 1;
1293                                 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
1294                                     need_switch_pathgroup(pp->mpp, 1))
1295                                         switch_pathgroup(pp->mpp);
1296
1297                                 /*
1298                                  * if at least one path is up in a group, and
1299                                  * the group is disabled, re-enable it
1300                                  */
1301                                 if (newstate == PATH_UP)
1302                                         enable_group(pp);
1303                         }
1304                         else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1305                                 LOG_MSG(4, checker_msg);
1306                                 /*
1307                                  * double the next check delay.
1308                                  * max at conf->max_checkint
1309                                  */
1310                                 if (pp->checkint < (conf->max_checkint / 2))
1311                                         pp->checkint = 2 * pp->checkint;
1312                                 else
1313                                         pp->checkint = conf->max_checkint;
1314
1315                                 pp->tick = pp->checkint;
1316                                 condlog(4, "%s: delay next check %is",
1317                                                 pp->dev_t, pp->tick);
1318
1319                         }
1320                         pp->state = newstate;
1321
1322                         /*
1323                          * path prio refreshing
1324                          */
1325                         condlog(4, "path prio refresh");
1326                         pathinfo(pp, conf->hwtable, DI_PRIO);
1327
1328                         if (need_switch_pathgroup(pp->mpp, 0)) {
1329                                 if (pp->mpp->pgfailback > 0)
1330                                         pp->mpp->failback_tick =
1331                                                 pp->mpp->pgfailback + 1;
1332                                 else if (pp->mpp->pgfailback ==
1333                                                 -FAILBACK_IMMEDIATE)
1334                                         switch_pathgroup(pp->mpp);
1335                         }
1336                 }
1337                 defered_failback_tick(vecs->mpvec);
1338                 retry_count_tick(vecs->mpvec);
1339
1340                 if (count)
1341                         count--;
1342                 else {
1343                         condlog(4, "map garbage collection");
1344                         mpvec_garbage_collector(vecs);
1345                         count = MAPGCINT;
1346                 }
1347                 
1348                 lock_cleanup_pop(vecs->lock);
1349                 sleep(1);
1350         }
1351         return NULL;
1352 }
1353
1354 static struct vectors *
1355 init_paths (void)
1356 {
1357         struct vectors * vecs;
1358
1359         vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1360
1361         if (!vecs)
1362                 return NULL;
1363
1364         vecs->lock = 
1365                 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1366
1367         if (!vecs->lock)
1368                 goto out;
1369
1370         vecs->pathvec = vector_alloc();
1371
1372         if (!vecs->pathvec)
1373                 goto out1;
1374                 
1375         vecs->mpvec = vector_alloc();
1376
1377         if (!vecs->mpvec)
1378                 goto out2;
1379         
1380         pthread_mutex_init(vecs->lock, NULL);
1381
1382         return vecs;
1383
1384 out2:
1385         vector_free(vecs->pathvec);
1386 out1:
1387         FREE(vecs->lock);
1388 out:
1389         FREE(vecs);
1390         condlog(0, "failed to init paths");
1391         return NULL;
1392 }
1393
1394 static void *
1395 signal_set(int signo, void (*func) (int))
1396 {
1397         int r;
1398         struct sigaction sig;
1399         struct sigaction osig;
1400
1401         sig.sa_handler = func;
1402         sigemptyset(&sig.sa_mask);
1403         sig.sa_flags = 0;
1404
1405         r = sigaction(signo, &sig, &osig);
1406
1407         if (r < 0)
1408                 return (SIG_ERR);
1409         else
1410                 return (osig.sa_handler);
1411 }
1412
1413 static void
1414 sighup (int sig)
1415 {
1416         condlog(3, "SIGHUP received");
1417
1418 #ifdef _DEBUG_
1419         dbg_free_final(NULL);
1420 #endif
1421 }
1422
1423 static void
1424 sigend (int sig)
1425 {
1426         exit_daemon(0);
1427 }
1428
1429 static void
1430 signal_init(void)
1431 {
1432         signal_set(SIGHUP, sighup);
1433         signal_set(SIGINT, sigend);
1434         signal_set(SIGTERM, sigend);
1435         signal_set(SIGKILL, sigend);
1436 }
1437
1438 static void
1439 setscheduler (void)
1440 {
1441         int res;
1442         static struct sched_param sched_param = {
1443                 sched_priority: 99
1444         };
1445
1446         res = sched_setscheduler (0, SCHED_RR, &sched_param);
1447
1448         if (res == -1)
1449                 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1450         return;
1451 }
1452
1453 static void
1454 set_oom_adj (int val)
1455 {
1456         FILE *fp;
1457
1458         fp = fopen("/proc/self/oom_adj", "w");
1459
1460         if (!fp)
1461                 return;
1462
1463         fprintf(fp, "%i", val);
1464         fclose(fp);
1465 }
1466         
1467 static int
1468 child (void * param)
1469 {
1470         pthread_t check_thr, uevent_thr, uxlsnr_thr;
1471         pthread_attr_t attr;
1472         struct vectors * vecs;
1473
1474         mlockall(MCL_CURRENT | MCL_FUTURE);
1475
1476         if (logsink)
1477                 log_thread_start();
1478
1479         condlog(2, "--------start up--------");
1480         condlog(2, "read " DEFAULT_CONFIGFILE);
1481
1482         if (load_config(DEFAULT_CONFIGFILE))
1483                 exit(1);
1484
1485         setlogmask(LOG_UPTO(conf->verbosity + 3));
1486
1487         /*
1488          * fill the voids left in the config file
1489          */
1490         if (!conf->checkint) {
1491                 conf->checkint = CHECKINT;
1492                 conf->max_checkint = MAX_CHECKINT;
1493         }
1494
1495         if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1496                 if (logsink)
1497                         log_thread_stop();
1498
1499                 exit(1);
1500         }
1501         signal_init();
1502         setscheduler();
1503         set_oom_adj(-17);
1504         vecs = init_paths();
1505
1506         if (!vecs)
1507                 exit(1);
1508
1509         if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
1510                 condlog(0, "can not find sysfs mount point");
1511                 exit(1);
1512         }
1513
1514         /*
1515          * fetch paths and multipaths lists
1516          * no paths and/or no multipaths are valid scenarii
1517          * vectors maintenance will be driven by events
1518          */
1519         path_discovery(vecs->pathvec, conf, DI_SYSFS | DI_WWID | DI_CHECKER);
1520         map_discovery(vecs);
1521
1522         /*
1523          * start threads
1524          */
1525         pthread_attr_init(&attr);
1526         pthread_attr_setstacksize(&attr, 64 * 1024);
1527         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1528         
1529         pthread_create(&check_thr, &attr, checkerloop, vecs);
1530         pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1531         pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1532
1533         pthread_cond_wait(&exit_cond, &exit_mutex);
1534
1535         /*
1536          * exit path
1537          */
1538         lock(vecs->lock);
1539         remove_maps(vecs);
1540         free_pathvec(vecs->pathvec, FREE_PATHS);
1541
1542         pthread_cancel(check_thr);
1543         pthread_cancel(uevent_thr);
1544         pthread_cancel(uxlsnr_thr);
1545
1546         free_keys(keys);
1547         keys = NULL;
1548         free_handlers(handlers);
1549         handlers = NULL;
1550         free_polls();
1551
1552         unlock(vecs->lock);
1553         pthread_mutex_destroy(vecs->lock);
1554         FREE(vecs->lock);
1555         vecs->lock = NULL;
1556         FREE(vecs);
1557         vecs = NULL;
1558         free_config(conf);
1559         conf = NULL;
1560
1561         condlog(2, "--------shut down-------");
1562         
1563         if (logsink)
1564                 log_thread_stop();
1565
1566 #ifdef _DEBUG_
1567         dbg_free_final(NULL);
1568 #endif
1569
1570         exit(0);
1571 }
1572
1573 static int
1574 daemonize(void)
1575 {
1576         int pid;
1577         int in_fd, out_fd;
1578
1579         if( (pid = fork()) < 0){
1580                 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1581                 return -1;
1582         }
1583         else if (pid != 0)
1584                 return pid;
1585
1586         setsid();
1587
1588         if ( (pid = fork()) < 0)
1589                 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1590         else if (pid != 0)
1591                 _exit(0);
1592
1593         in_fd = open("/dev/null", O_RDONLY);
1594         if (in_fd < 0){
1595                 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1596                         strerror(errno));
1597                 _exit(0);
1598         }
1599         out_fd = open("/dev/console", O_WRONLY);
1600         if (out_fd < 0){
1601                 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1602                         strerror(errno));
1603                 _exit(0);
1604         }
1605
1606         close(STDIN_FILENO);
1607         dup(in_fd);
1608         close(STDOUT_FILENO);
1609         dup(out_fd);
1610         close(STDERR_FILENO);
1611         dup(out_fd);
1612
1613         close(in_fd);
1614         close(out_fd);
1615         chdir("/");
1616         umask(0);
1617         return 0;
1618 }
1619
1620 int
1621 main (int argc, char *argv[])
1622 {
1623         extern char *optarg;
1624         extern int optind;
1625         int arg;
1626         int err;
1627         
1628         logsink = 1;
1629
1630         if (getuid() != 0) {
1631                 fprintf(stderr, "need to be root\n");
1632                 exit(1);
1633         }
1634
1635         /* make sure we don't lock any path */
1636         chdir("/");
1637         umask(umask(077) | 022);
1638
1639         conf = alloc_config();
1640
1641         if (!conf)
1642                 exit(1);
1643
1644         while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1645         switch(arg) {
1646                 case 'd':
1647                         logsink = 0;
1648                         //debug=1; /* ### comment me out ### */
1649                         break;
1650                 case 'v':
1651                         if (sizeof(optarg) > sizeof(char *) ||
1652                             !isdigit(optarg[0]))
1653                                 exit(1);
1654
1655                         conf->verbosity = atoi(optarg);
1656                         break;
1657                 case 'k':
1658                         uxclnt(optarg);
1659                         exit(0);
1660                 default:
1661                         ;
1662                 }
1663         }
1664
1665         if (!logsink)
1666                 err = 0;
1667         else
1668                 err = daemonize();
1669         
1670         if (err < 0)
1671                 /* error */
1672                 exit(1);
1673         else if (err > 0)
1674                 /* parent dies */
1675                 exit(0);
1676         else
1677                 /* child lives */
1678                 return (child(NULL));
1679 }