Make multipath ignore devices without mpath prefix
[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 #include <sys/time.h>
16 #include <sys/resource.h>
17 #include <limits.h>
18 #include <linux/oom.h>
19 #include <libudev.h>
20 #ifdef USE_SYSTEMD
21 #include <systemd/sd-daemon.h>
22 #endif
23 #include <semaphore.h>
24 #include <mpath_persist.h>
25 #include <time.h>
26
27 /*
28  * libcheckers
29  */
30 #include <checkers.h>
31
32 #ifdef USE_SYSTEMD
33 static int use_watchdog;
34 #endif
35
36 /*
37  * libmultipath
38  */
39 #include <parser.h>
40 #include <vector.h>
41 #include <memory.h>
42 #include <config.h>
43 #include <util.h>
44 #include <hwtable.h>
45 #include <defaults.h>
46 #include <structs.h>
47 #include <blacklist.h>
48 #include <structs_vec.h>
49 #include <dmparser.h>
50 #include <devmapper.h>
51 #include <sysfs.h>
52 #include <dict.h>
53 #include <discovery.h>
54 #include <debug.h>
55 #include <propsel.h>
56 #include <uevent.h>
57 #include <switchgroup.h>
58 #include <print.h>
59 #include <configure.h>
60 #include <prio.h>
61 #include <wwids.h>
62 #include <pgpolicies.h>
63 #include <uevent.h>
64 #include <log.h>
65
66 #include "main.h"
67 #include "pidfile.h"
68 #include "uxlsnr.h"
69 #include "uxclnt.h"
70 #include "cli.h"
71 #include "cli_handlers.h"
72 #include "lock.h"
73 #include "waiter.h"
74 #include "wwids.h"
75
76 #define FILE_NAME_SIZE 256
77 #define CMDSIZE 160
78
79 #define LOG_MSG(a, b) \
80 do { \
81         if (pp->offline) \
82                 condlog(a, "%s: %s - path offline", pp->mpp->alias, pp->dev); \
83         else if (strlen(b)) \
84                 condlog(a, "%s: %s - %s", pp->mpp->alias, pp->dev, b); \
85 } while(0)
86
87 struct mpath_event_param
88 {
89         char * devname;
90         struct multipath *mpp;
91 };
92
93 unsigned int mpath_mx_alloc_len;
94
95 int logsink;
96 enum daemon_status running_state;
97 pid_t daemon_pid;
98
99 static sem_t exit_sem;
100 /*
101  * global copy of vecs for use in sig handlers
102  */
103 struct vectors * gvecs;
104
105 struct udev * udev;
106
107 static int
108 need_switch_pathgroup (struct multipath * mpp, int refresh)
109 {
110         struct pathgroup * pgp;
111         struct path * pp;
112         unsigned int i, j;
113
114         if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
115                 return 0;
116
117         /*
118          * Refresh path priority values
119          */
120         if (refresh)
121                 vector_foreach_slot (mpp->pg, pgp, i)
122                         vector_foreach_slot (pgp->paths, pp, j)
123                                 pathinfo(pp, conf->hwtable, DI_PRIO);
124
125         mpp->bestpg = select_path_group(mpp);
126
127         if (mpp->bestpg != mpp->nextpg)
128                 return 1;
129
130         return 0;
131 }
132
133 static void
134 switch_pathgroup (struct multipath * mpp)
135 {
136         mpp->stat_switchgroup++;
137         dm_switchgroup(mpp->alias, mpp->bestpg);
138         condlog(2, "%s: switch to path group #%i",
139                  mpp->alias, mpp->bestpg);
140 }
141
142 static int
143 coalesce_maps(struct vectors *vecs, vector nmpv)
144 {
145         struct multipath * ompp;
146         vector ompv = vecs->mpvec;
147         unsigned int i;
148
149         vector_foreach_slot (ompv, ompp, i) {
150                 condlog(3, "%s: coalesce map", ompp->alias);
151                 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
152                         /*
153                          * remove all current maps not allowed by the
154                          * current configuration
155                          */
156                         if (dm_flush_map(ompp->alias)) {
157                                 condlog(0, "%s: unable to flush devmap",
158                                         ompp->alias);
159                                 /*
160                                  * may be just because the device is open
161                                  */
162                                 if (setup_multipath(vecs, ompp) != 0) {
163                                         i--;
164                                         continue;
165                                 }
166                                 if (!vector_alloc_slot(nmpv))
167                                         return 1;
168
169                                 vector_set_slot(nmpv, ompp);
170
171                                 vector_del_slot(ompv, i);
172                                 i--;
173                         }
174                         else {
175                                 dm_lib_release();
176                                 condlog(2, "%s devmap removed", ompp->alias);
177                         }
178                 } else if (conf->reassign_maps) {
179                         condlog(3, "%s: Reassign existing device-mapper"
180                                 " devices", ompp->alias);
181                         dm_reassign(ompp->alias);
182                 }
183         }
184         return 0;
185 }
186
187 void
188 sync_map_state(struct multipath *mpp)
189 {
190         struct pathgroup *pgp;
191         struct path *pp;
192         unsigned int i, j;
193
194         if (!mpp->pg)
195                 return;
196
197         vector_foreach_slot (mpp->pg, pgp, i){
198                 vector_foreach_slot (pgp->paths, pp, j){
199                         if (pp->state == PATH_UNCHECKED || 
200                             pp->state == PATH_WILD ||
201                             pp->state == PATH_DELAYED)
202                                 continue;
203                         if ((pp->dmstate == PSTATE_FAILED ||
204                              pp->dmstate == PSTATE_UNDEF) &&
205                             (pp->state == PATH_UP || pp->state == PATH_GHOST))
206                                 dm_reinstate_path(mpp->alias, pp->dev_t);
207                         else if ((pp->dmstate == PSTATE_ACTIVE ||
208                                   pp->dmstate == PSTATE_UNDEF) &&
209                                  (pp->state == PATH_DOWN ||
210                                   pp->state == PATH_SHAKY))
211                                 dm_fail_path(mpp->alias, pp->dev_t);
212                 }
213         }
214 }
215
216 static void
217 sync_maps_state(vector mpvec)
218 {
219         unsigned int i;
220         struct multipath *mpp;
221
222         vector_foreach_slot (mpvec, mpp, i)
223                 sync_map_state(mpp);
224 }
225
226 static int
227 flush_map(struct multipath * mpp, struct vectors * vecs, int nopaths)
228 {
229         int r;
230
231         if (nopaths)
232                 r = dm_flush_map_nopaths(mpp->alias, mpp->deferred_remove);
233         else
234                 r = dm_flush_map(mpp->alias);
235         /*
236          * clear references to this map before flushing so we can ignore
237          * the spurious uevent we may generate with the dm_flush_map call below
238          */
239         if (r) {
240                 /*
241                  * May not really be an error -- if the map was already flushed
242                  * from the device mapper by dmsetup(8) for instance.
243                  */
244                 if (r == 1)
245                         condlog(0, "%s: can't flush", mpp->alias);
246                 else {
247                         condlog(2, "%s: devmap deferred remove", mpp->alias);
248                         mpp->deferred_remove = DEFERRED_REMOVE_IN_PROGRESS;
249                 }
250                 return r;
251         }
252         else {
253                 dm_lib_release();
254                 condlog(2, "%s: map flushed", mpp->alias);
255         }
256
257         orphan_paths(vecs->pathvec, mpp);
258         remove_map_and_stop_waiter(mpp, vecs, 1);
259
260         return 0;
261 }
262
263 static int
264 uev_add_map (struct uevent * uev, struct vectors * vecs)
265 {
266         char *alias;
267         int major = -1, minor = -1, rc;
268
269         condlog(3, "%s: add map (uevent)", uev->kernel);
270         alias = uevent_get_dm_name(uev);
271         if (!alias) {
272                 condlog(3, "%s: No DM_NAME in uevent", uev->kernel);
273                 major = uevent_get_major(uev);
274                 minor = uevent_get_minor(uev);
275                 alias = dm_mapname(major, minor);
276                 if (!alias) {
277                         condlog(2, "%s: mapname not found for %d:%d",
278                                 uev->kernel, major, minor);
279                         return 1;
280                 }
281         }
282         rc = ev_add_map(uev->kernel, alias, vecs);
283         FREE(alias);
284         return rc;
285 }
286
287 int
288 ev_add_map (char * dev, char * alias, struct vectors * vecs)
289 {
290         char * refwwid;
291         struct multipath * mpp;
292         int map_present;
293         int r = 1;
294
295         map_present = dm_map_present(alias);
296
297         if (map_present && !dm_is_mpath(alias)) {
298                 condlog(4, "%s: not a multipath map", alias);
299                 return 0;
300         }
301
302         mpp = find_mp_by_alias(vecs->mpvec, alias);
303
304         if (mpp) {
305                 /*
306                  * Not really an error -- we generate our own uevent
307                  * if we create a multipath mapped device as a result
308                  * of uev_add_path
309                  */
310                 if (conf->reassign_maps) {
311                         condlog(3, "%s: Reassign existing device-mapper devices",
312                                 alias);
313                         dm_reassign(alias);
314                 }
315                 return 0;
316         }
317         condlog(2, "%s: adding map", alias);
318
319         /*
320          * now we can register the map
321          */
322         if (map_present) {
323                 if ((mpp = add_map_without_path(vecs, alias))) {
324                         sync_map_state(mpp);
325                         condlog(2, "%s: devmap %s registered", alias, dev);
326                         return 0;
327                 } else {
328                         condlog(2, "%s: uev_add_map failed", dev);
329                         return 1;
330                 }
331         }
332         r = get_refwwid(dev, DEV_DEVMAP, vecs->pathvec, &refwwid);
333
334         if (refwwid) {
335                 r = coalesce_paths(vecs, NULL, refwwid, 0);
336                 dm_lib_release();
337         }
338
339         if (!r)
340                 condlog(2, "%s: devmap %s added", alias, dev);
341         else if (r == 2)
342                 condlog(2, "%s: uev_add_map %s blacklisted", alias, dev);
343         else
344                 condlog(0, "%s: uev_add_map %s failed", alias, dev);
345
346         FREE(refwwid);
347         return r;
348 }
349
350 static int
351 uev_remove_map (struct uevent * uev, struct vectors * vecs)
352 {
353         char *alias;
354         int minor;
355         struct multipath *mpp;
356
357         condlog(2, "%s: remove map (uevent)", uev->kernel);
358         alias = uevent_get_dm_name(uev);
359         if (!alias) {
360                 condlog(3, "%s: No DM_NAME in uevent, ignoring", uev->kernel);
361                 return 0;
362         }
363         minor = uevent_get_minor(uev);
364         mpp = find_mp_by_minor(vecs->mpvec, minor);
365
366         if (!mpp) {
367                 condlog(2, "%s: devmap not registered, can't remove",
368                         uev->kernel);
369                 goto out;
370         }
371         if (strcmp(mpp->alias, alias)) {
372                 condlog(2, "%s: minor number mismatch (map %d, event %d)",
373                         mpp->alias, mpp->dmi->minor, minor);
374                 goto out;
375         }
376
377         orphan_paths(vecs->pathvec, mpp);
378         remove_map_and_stop_waiter(mpp, vecs, 1);
379 out:
380         FREE(alias);
381         return 0;
382 }
383
384 int
385 ev_remove_map (char * devname, char * alias, int minor, struct vectors * vecs)
386 {
387         struct multipath * mpp;
388
389         mpp = find_mp_by_minor(vecs->mpvec, minor);
390
391         if (!mpp) {
392                 condlog(2, "%s: devmap not registered, can't remove",
393                         devname);
394                 return 0;
395         }
396         if (strcmp(mpp->alias, alias)) {
397                 condlog(2, "%s: minor number mismatch (map %d, event %d)",
398                         mpp->alias, mpp->dmi->minor, minor);
399                 return 0;
400         }
401         return flush_map(mpp, vecs, 0);
402 }
403
404 static int
405 uev_add_path (struct uevent *uev, struct vectors * vecs)
406 {
407         struct path *pp;
408         int ret = 0, i;
409
410         condlog(2, "%s: add path (uevent)", uev->kernel);
411         if (strstr(uev->kernel, "..") != NULL) {
412                 /*
413                  * Don't allow relative device names in the pathvec
414                  */
415                 condlog(0, "%s: path name is invalid", uev->kernel);
416                 return 1;
417         }
418
419         pp = find_path_by_dev(vecs->pathvec, uev->kernel);
420         if (pp) {
421                 int r;
422
423                 condlog(0, "%s: spurious uevent, path already in pathvec",
424                         uev->kernel);
425                 if (!pp->mpp && !strlen(pp->wwid)) {
426                         condlog(3, "%s: reinitialize path", uev->kernel);
427                         udev_device_unref(pp->udev);
428                         pp->udev = udev_device_ref(uev->udev);
429                         r = pathinfo(pp, conf->hwtable,
430                                      DI_ALL | DI_BLACKLIST);
431                         if (r == PATHINFO_OK)
432                                 ret = ev_add_path(pp, vecs);
433                         else if (r == PATHINFO_SKIPPED) {
434                                 condlog(3, "%s: remove blacklisted path",
435                                         uev->kernel);
436                                 i = find_slot(vecs->pathvec, (void *)pp);
437                                 if (i != -1)
438                                         vector_del_slot(vecs->pathvec, i);
439                                 free_path(pp);
440                         } else {
441                                 condlog(0, "%s: failed to reinitialize path",
442                                         uev->kernel);
443                                 ret = 1;
444                         }
445                 }
446                 return ret;
447         }
448
449         /*
450          * get path vital state
451          */
452         ret = alloc_path_with_pathinfo(conf->hwtable, uev->udev,
453                                        DI_ALL, &pp);
454         if (!pp) {
455                 if (ret == PATHINFO_SKIPPED)
456                         return 0;
457                 condlog(3, "%s: failed to get path info", uev->kernel);
458                 return 1;
459         }
460         if (!strlen(pp->wwid)) {
461                 condlog(3, "%s: Failed to get path wwid", uev->kernel);
462                 free_path(pp);
463                 return 1;
464         }
465         ret = store_path(vecs->pathvec, pp);
466         if (!ret) {
467                 pp->checkint = conf->checkint;
468                 ret = ev_add_path(pp, vecs);
469         } else {
470                 condlog(0, "%s: failed to store path info, "
471                         "dropping event",
472                         uev->kernel);
473                 free_path(pp);
474                 ret = 1;
475         }
476
477         return ret;
478 }
479
480 /*
481  * returns:
482  * 0: added
483  * 1: error
484  */
485 int
486 ev_add_path (struct path * pp, struct vectors * vecs)
487 {
488         struct multipath * mpp;
489         char params[PARAMS_SIZE] = {0};
490         int retries = 3;
491         int start_waiter = 0;
492         int ret;
493
494         /*
495          * need path UID to go any further
496          */
497         if (strlen(pp->wwid) == 0) {
498                 condlog(0, "%s: failed to get path uid", pp->dev);
499                 goto fail; /* leave path added to pathvec */
500         }
501         mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
502 rescan:
503         if (mpp) {
504                 if (mpp->size != pp->size) {
505                         condlog(0, "%s: failed to add new path %s, "
506                                 "device size mismatch",
507                                 mpp->alias, pp->dev);
508                         int i = find_slot(vecs->pathvec, (void *)pp);
509                         if (i != -1)
510                                 vector_del_slot(vecs->pathvec, i);
511                         free_path(pp);
512                         return 1;
513                 }
514
515                 condlog(4,"%s: adopting all paths for path %s",
516                         mpp->alias, pp->dev);
517                 if (adopt_paths(vecs->pathvec, mpp, 1))
518                         goto fail; /* leave path added to pathvec */
519
520                 verify_paths(mpp, vecs);
521                 mpp->flush_on_last_del = FLUSH_UNDEF;
522                 mpp->action = ACT_RELOAD;
523         } else {
524                 if (conf->find_multipaths &&
525                     !should_multipath(pp, vecs->pathvec)) {
526                         orphan_path(pp, "only one path");
527                         return 0;
528                 }
529                 condlog(4,"%s: creating new map", pp->dev);
530                 if ((mpp = add_map_with_path(vecs, pp, 1))) {
531                         mpp->action = ACT_CREATE;
532                         /*
533                          * We don't depend on ACT_CREATE, as domap will
534                          * set it to ACT_NOTHING when complete.
535                          */
536                         start_waiter = 1;
537                 }
538                 else
539                         goto fail; /* leave path added to pathvec */
540         }
541
542         /* persistent reseravtion check*/
543         mpath_pr_event_handle(pp);      
544
545         /*
546          * push the map to the device-mapper
547          */
548         if (setup_map(mpp, params, PARAMS_SIZE)) {
549                 condlog(0, "%s: failed to setup map for addition of new "
550                         "path %s", mpp->alias, pp->dev);
551                 goto fail_map;
552         }
553         /*
554          * reload the map for the multipath mapped device
555          */
556 retry:
557         ret = domap(mpp, params);
558         if (ret <= 0) {
559                 if (ret < 0 && retries-- > 0) {
560                         condlog(0, "%s: retry domap for addition of new "
561                                 "path %s", mpp->alias, pp->dev);
562                         sleep(1);
563                         goto retry;
564                 }
565                 condlog(0, "%s: failed in domap for addition of new "
566                         "path %s", mpp->alias, pp->dev);
567                 /*
568                  * deal with asynchronous uevents :((
569                  */
570                 if (mpp->action == ACT_RELOAD && retries-- > 0) {
571                         condlog(0, "%s: uev_add_path sleep", mpp->alias);
572                         sleep(1);
573                         update_mpp_paths(mpp, vecs->pathvec);
574                         goto rescan;
575                 }
576                 else if (mpp->action == ACT_RELOAD)
577                         condlog(0, "%s: giving up reload", mpp->alias);
578                 else
579                         goto fail_map;
580         }
581         dm_lib_release();
582
583         /*
584          * update our state from kernel regardless of create or reload
585          */
586         if (setup_multipath(vecs, mpp))
587                 goto fail; /* if setup_multipath fails, it removes the map */
588
589         sync_map_state(mpp);
590
591         if ((mpp->action == ACT_CREATE ||
592              (mpp->action == ACT_NOTHING && start_waiter && !mpp->waiter)) &&
593             start_waiter_thread(mpp, vecs))
594                         goto fail_map;
595
596         if (retries >= 0) {
597                 condlog(2, "%s [%s]: path added to devmap %s",
598                         pp->dev, pp->dev_t, mpp->alias);
599                 return 0;
600         }
601         else
602                 goto fail;
603
604 fail_map:
605         remove_map(mpp, vecs, 1);
606 fail:
607         orphan_path(pp, "failed to add path");
608         return 1;
609 }
610
611 static int
612 uev_remove_path (struct uevent *uev, struct vectors * vecs)
613 {
614         struct path *pp;
615
616         condlog(2, "%s: remove path (uevent)", uev->kernel);
617         pp = find_path_by_dev(vecs->pathvec, uev->kernel);
618
619         if (!pp) {
620                 /* Not an error; path might have been purged earlier */
621                 condlog(0, "%s: path already removed", uev->kernel);
622                 return 0;
623         }
624
625         return ev_remove_path(pp, vecs);
626 }
627
628 int
629 ev_remove_path (struct path *pp, struct vectors * vecs)
630 {
631         struct multipath * mpp;
632         int i, retval = 0;
633         char params[PARAMS_SIZE] = {0};
634
635         /*
636          * avoid referring to the map of an orphaned path
637          */
638         if ((mpp = pp->mpp)) {
639                 /*
640                  * transform the mp->pg vector of vectors of paths
641                  * into a mp->params string to feed the device-mapper
642                  */
643                 if (update_mpp_paths(mpp, vecs->pathvec)) {
644                         condlog(0, "%s: failed to update paths",
645                                 mpp->alias);
646                         goto fail;
647                 }
648                 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
649                         vector_del_slot(mpp->paths, i);
650
651                 /*
652                  * remove the map IFF removing the last path
653                  */
654                 if (VECTOR_SIZE(mpp->paths) == 0) {
655                         char alias[WWID_SIZE];
656
657                         /*
658                          * flush_map will fail if the device is open
659                          */
660                         strncpy(alias, mpp->alias, WWID_SIZE);
661                         if (mpp->flush_on_last_del == FLUSH_ENABLED) {
662                                 condlog(2, "%s Last path deleted, disabling queueing", mpp->alias);
663                                 mpp->retry_tick = 0;
664                                 mpp->no_path_retry = NO_PATH_RETRY_FAIL;
665                                 mpp->flush_on_last_del = FLUSH_IN_PROGRESS;
666                                 dm_queue_if_no_path(mpp->alias, 0);
667                         }
668                         if (!flush_map(mpp, vecs, 1)) {
669                                 condlog(2, "%s: removed map after"
670                                         " removing all paths",
671                                         alias);
672                                 retval = 0;
673                                 goto out;
674                         }
675                         /*
676                          * Not an error, continue
677                          */
678                 }
679
680                 if (setup_map(mpp, params, PARAMS_SIZE)) {
681                         condlog(0, "%s: failed to setup map for"
682                                 " removal of path %s", mpp->alias, pp->dev);
683                         goto fail;
684                 }
685                 /*
686                  * reload the map
687                  */
688                 mpp->action = ACT_RELOAD;
689                 if (domap(mpp, params) <= 0) {
690                         condlog(0, "%s: failed in domap for "
691                                 "removal of path %s",
692                                 mpp->alias, pp->dev);
693                         retval = 1;
694                 } else {
695                         /*
696                          * update our state from kernel
697                          */
698                         if (setup_multipath(vecs, mpp))
699                                 return 1;
700                         sync_map_state(mpp);
701
702                         condlog(2, "%s [%s]: path removed from map %s",
703                                 pp->dev, pp->dev_t, mpp->alias);
704                 }
705         }
706
707 out:
708         if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
709                 vector_del_slot(vecs->pathvec, i);
710
711         free_path(pp);
712
713         return retval;
714
715 fail:
716         remove_map_and_stop_waiter(mpp, vecs, 1);
717         return 1;
718 }
719
720 static int
721 uev_update_path (struct uevent *uev, struct vectors * vecs)
722 {
723         int ro, retval = 0;
724
725         ro = uevent_get_disk_ro(uev);
726
727         if (ro >= 0) {
728                 struct path * pp;
729
730                 condlog(2, "%s: update path write_protect to '%d' (uevent)",
731                         uev->kernel, ro);
732                 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
733                 if (!pp) {
734                         condlog(0, "%s: spurious uevent, path not found",
735                                 uev->kernel);
736                         return 1;
737                 }
738                 if (pp->mpp) {
739                         retval = reload_map(vecs, pp->mpp, 0);
740
741                         condlog(2, "%s: map %s reloaded (retval %d)",
742                                 uev->kernel, pp->mpp->alias, retval);
743                 }
744
745         }
746
747         return retval;
748 }
749
750 static int
751 map_discovery (struct vectors * vecs)
752 {
753         struct multipath * mpp;
754         unsigned int i;
755
756         if (dm_get_maps(vecs->mpvec))
757                 return 1;
758
759         vector_foreach_slot (vecs->mpvec, mpp, i)
760                 if (setup_multipath(vecs, mpp))
761                         return 1;
762
763         return 0;
764 }
765
766 int
767 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
768 {
769         struct vectors * vecs;
770         int r;
771
772         *reply = NULL;
773         *len = 0;
774         vecs = (struct vectors *)trigger_data;
775
776         pthread_cleanup_push(cleanup_lock, &vecs->lock);
777         lock(vecs->lock);
778         pthread_testcancel();
779
780         r = parse_cmd(str, reply, len, vecs);
781
782         if (r > 0) {
783                 *reply = STRDUP("fail\n");
784                 *len = strlen(*reply) + 1;
785                 r = 1;
786         }
787         else if (!r && *len == 0) {
788                 *reply = STRDUP("ok\n");
789                 *len = strlen(*reply) + 1;
790                 r = 0;
791         }
792         /* else if (r < 0) leave *reply alone */
793
794         lock_cleanup_pop(vecs->lock);
795         return r;
796 }
797
798 static int
799 uev_discard(char * devpath)
800 {
801         char *tmp;
802         char a[11], b[11];
803
804         /*
805          * keep only block devices, discard partitions
806          */
807         tmp = strstr(devpath, "/block/");
808         if (tmp == NULL){
809                 condlog(4, "no /block/ in '%s'", devpath);
810                 return 1;
811         }
812         if (sscanf(tmp, "/block/%10s", a) != 1 ||
813             sscanf(tmp, "/block/%10[^/]/%10s", a, b) == 2) {
814                 condlog(4, "discard event on %s", devpath);
815                 return 1;
816         }
817         return 0;
818 }
819
820 int
821 uev_trigger (struct uevent * uev, void * trigger_data)
822 {
823         int r = 0;
824         struct vectors * vecs;
825
826         vecs = (struct vectors *)trigger_data;
827
828         if (uev_discard(uev->devpath))
829                 return 0;
830
831         pthread_cleanup_push(cleanup_lock, &vecs->lock);
832         lock(vecs->lock);
833         pthread_testcancel();
834
835         /*
836          * device map event
837          * Add events are ignored here as the tables
838          * are not fully initialised then.
839          */
840         if (!strncmp(uev->kernel, "dm-", 3)) {
841                 if (!strncmp(uev->action, "change", 6)) {
842                         r = uev_add_map(uev, vecs);
843                         goto out;
844                 }
845                 if (!strncmp(uev->action, "remove", 6)) {
846                         r = uev_remove_map(uev, vecs);
847                         goto out;
848                 }
849                 goto out;
850         }
851
852         /*
853          * path add/remove event
854          */
855         if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
856                            uev->kernel) > 0)
857                 goto out;
858
859         if (!strncmp(uev->action, "add", 3)) {
860                 r = uev_add_path(uev, vecs);
861                 goto out;
862         }
863         if (!strncmp(uev->action, "remove", 6)) {
864                 r = uev_remove_path(uev, vecs);
865                 goto out;
866         }
867         if (!strncmp(uev->action, "change", 6)) {
868                 r = uev_update_path(uev, vecs);
869                 goto out;
870         }
871
872 out:
873         lock_cleanup_pop(vecs->lock);
874         return r;
875 }
876
877 static void *
878 ueventloop (void * ap)
879 {
880         struct udev *udev = ap;
881
882         if (uevent_listen(udev))
883                 condlog(0, "error starting uevent listener");
884
885         return NULL;
886 }
887
888 static void *
889 uevqloop (void * ap)
890 {
891         if (uevent_dispatch(&uev_trigger, ap))
892                 condlog(0, "error starting uevent dispatcher");
893
894         return NULL;
895 }
896 static void *
897 uxlsnrloop (void * ap)
898 {
899         if (cli_init())
900                 return NULL;
901
902         set_handler_callback(LIST+PATHS, cli_list_paths);
903         set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
904         set_handler_callback(LIST+PATH, cli_list_path);
905         set_handler_callback(LIST+MAPS, cli_list_maps);
906         set_handler_callback(LIST+STATUS, cli_list_status);
907         set_handler_callback(LIST+DAEMON, cli_list_daemon);
908         set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
909         set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
910         set_handler_callback(LIST+MAPS+FMT, cli_list_maps_fmt);
911         set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
912         set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
913         set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
914         set_handler_callback(LIST+CONFIG, cli_list_config);
915         set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
916         set_handler_callback(LIST+DEVICES, cli_list_devices);
917         set_handler_callback(LIST+WILDCARDS, cli_list_wildcards);
918         set_handler_callback(ADD+PATH, cli_add_path);
919         set_handler_callback(DEL+PATH, cli_del_path);
920         set_handler_callback(ADD+MAP, cli_add_map);
921         set_handler_callback(DEL+MAP, cli_del_map);
922         set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
923         set_handler_callback(RECONFIGURE, cli_reconfigure);
924         set_handler_callback(SUSPEND+MAP, cli_suspend);
925         set_handler_callback(RESUME+MAP, cli_resume);
926         set_handler_callback(RESIZE+MAP, cli_resize);
927         set_handler_callback(RELOAD+MAP, cli_reload);
928         set_handler_callback(RESET+MAP, cli_reassign);
929         set_handler_callback(REINSTATE+PATH, cli_reinstate);
930         set_handler_callback(FAIL+PATH, cli_fail);
931         set_handler_callback(DISABLEQ+MAP, cli_disable_queueing);
932         set_handler_callback(RESTOREQ+MAP, cli_restore_queueing);
933         set_handler_callback(DISABLEQ+MAPS, cli_disable_all_queueing);
934         set_handler_callback(RESTOREQ+MAPS, cli_restore_all_queueing);
935         set_handler_callback(QUIT, cli_quit);
936         set_handler_callback(SHUTDOWN, cli_shutdown);
937         set_handler_callback(GETPRSTATUS+MAP, cli_getprstatus);
938         set_handler_callback(SETPRSTATUS+MAP, cli_setprstatus);
939         set_handler_callback(UNSETPRSTATUS+MAP, cli_unsetprstatus);
940         set_handler_callback(FORCEQ+DAEMON, cli_force_no_daemon_q);
941         set_handler_callback(RESTOREQ+DAEMON, cli_restore_no_daemon_q);
942
943         umask(077);
944         uxsock_listen(&uxsock_trigger, ap);
945
946         return NULL;
947 }
948
949 void
950 exit_daemon (void)
951 {
952         sem_post(&exit_sem);
953 }
954
955 const char *
956 daemon_status(void)
957 {
958         switch (running_state) {
959         case DAEMON_INIT:
960                 return "init";
961         case DAEMON_START:
962                 return "startup";
963         case DAEMON_CONFIGURE:
964                 return "configure";
965         case DAEMON_RUNNING:
966                 return "running";
967         case DAEMON_SHUTDOWN:
968                 return "shutdown";
969         }
970         return NULL;
971 }
972
973 static void
974 fail_path (struct path * pp, int del_active)
975 {
976         if (!pp->mpp)
977                 return;
978
979         condlog(2, "checker failed path %s in map %s",
980                  pp->dev_t, pp->mpp->alias);
981
982         dm_fail_path(pp->mpp->alias, pp->dev_t);
983         if (del_active)
984                 update_queue_mode_del_path(pp->mpp);
985 }
986
987 /*
988  * caller must have locked the path list before calling that function
989  */
990 static int
991 reinstate_path (struct path * pp, int add_active)
992 {
993         int ret = 0;
994
995         if (!pp->mpp)
996                 return 0;
997
998         if (dm_reinstate_path(pp->mpp->alias, pp->dev_t)) {
999                 condlog(0, "%s: reinstate failed", pp->dev_t);
1000                 ret = 1;
1001         } else {
1002                 condlog(2, "%s: reinstated", pp->dev_t);
1003                 if (add_active)
1004                         update_queue_mode_add_path(pp->mpp);
1005         }
1006         return ret;
1007 }
1008
1009 static void
1010 enable_group(struct path * pp)
1011 {
1012         struct pathgroup * pgp;
1013
1014         /*
1015          * if path is added through uev_add_path, pgindex can be unset.
1016          * next update_strings() will set it, upon map reload event.
1017          *
1018          * we can safely return here, because upon map reload, all
1019          * PG will be enabled.
1020          */
1021         if (!pp->mpp->pg || !pp->pgindex)
1022                 return;
1023
1024         pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1025
1026         if (pgp->status == PGSTATE_DISABLED) {
1027                 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1028                 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1029         }
1030 }
1031
1032 static void
1033 mpvec_garbage_collector (struct vectors * vecs)
1034 {
1035         struct multipath * mpp;
1036         unsigned int i;
1037
1038         if (!vecs->mpvec)
1039                 return;
1040
1041         vector_foreach_slot (vecs->mpvec, mpp, i) {
1042                 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1043                         condlog(2, "%s: remove dead map", mpp->alias);
1044                         remove_map_and_stop_waiter(mpp, vecs, 1);
1045                         i--;
1046                 }
1047         }
1048 }
1049
1050 /* This is called after a path has started working again. It the multipath
1051  * device for this path uses the followover failback type, and this is the
1052  * best pathgroup, and this is the first path in the pathgroup to come back
1053  * up, then switch to this pathgroup */
1054 static int
1055 followover_should_failback(struct path * pp)
1056 {
1057         struct pathgroup * pgp;
1058         struct path *pp1;
1059         int i;
1060
1061         if (pp->mpp->pgfailback != -FAILBACK_FOLLOWOVER ||
1062             !pp->mpp->pg || !pp->pgindex ||
1063             pp->pgindex != pp->mpp->bestpg)
1064                 return 0;
1065
1066         pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1067         vector_foreach_slot(pgp->paths, pp1, i) {
1068                 if (pp1 == pp)
1069                         continue;
1070                 if (pp1->chkrstate != PATH_DOWN && pp1->chkrstate != PATH_SHAKY)
1071                         return 0;
1072         }
1073         return 1;
1074 }
1075
1076 static void
1077 defered_failback_tick (vector mpvec)
1078 {
1079         struct multipath * mpp;
1080         unsigned int i;
1081
1082         vector_foreach_slot (mpvec, mpp, i) {
1083                 /*
1084                  * defered failback getting sooner
1085                  */
1086                 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1087                         mpp->failback_tick--;
1088
1089                         if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1090                                 switch_pathgroup(mpp);
1091                 }
1092         }
1093 }
1094
1095 static void
1096 retry_count_tick(vector mpvec)
1097 {
1098         struct multipath *mpp;
1099         unsigned int i;
1100
1101         vector_foreach_slot (mpvec, mpp, i) {
1102                 if (mpp->retry_tick) {
1103                         mpp->stat_total_queueing_time++;
1104                         condlog(4, "%s: Retrying.. No active path", mpp->alias);
1105                         if(--mpp->retry_tick == 0) {
1106                                 dm_queue_if_no_path(mpp->alias, 0);
1107                                 condlog(2, "%s: Disable queueing", mpp->alias);
1108                         }
1109                 }
1110         }
1111 }
1112
1113 int update_prio(struct path *pp, int refresh_all)
1114 {
1115         int oldpriority;
1116         struct path *pp1;
1117         struct pathgroup * pgp;
1118         int i, j, changed = 0;
1119
1120         if (refresh_all) {
1121                 vector_foreach_slot (pp->mpp->pg, pgp, i) {
1122                         vector_foreach_slot (pgp->paths, pp1, j) {
1123                                 oldpriority = pp1->priority;
1124                                 pathinfo(pp1, conf->hwtable, DI_PRIO);
1125                                 if (pp1->priority != oldpriority)
1126                                         changed = 1;
1127                         }
1128                 }
1129                 return changed;
1130         }
1131         oldpriority = pp->priority;
1132         pathinfo(pp, conf->hwtable, DI_PRIO);
1133
1134         if (pp->priority == oldpriority)
1135                 return 0;
1136         return 1;
1137 }
1138
1139 int update_path_groups(struct multipath *mpp, struct vectors *vecs, int refresh)
1140 {
1141         if (reload_map(vecs, mpp, refresh))
1142                 return 1;
1143
1144         dm_lib_release();
1145         if (setup_multipath(vecs, mpp) != 0)
1146                 return 1;
1147         sync_map_state(mpp);
1148
1149         return 0;
1150 }
1151
1152 /*
1153  * Returns '1' if the path has been checked, '0' otherwise
1154  */
1155 int
1156 check_path (struct vectors * vecs, struct path * pp)
1157 {
1158         int newstate;
1159         int new_path_up = 0;
1160         int chkr_new_path_up = 0;
1161         int add_active;
1162         int oldchkrstate = pp->chkrstate;
1163
1164         if (pp->initialized && !pp->mpp)
1165                 return 0;
1166
1167         if (pp->tick && --pp->tick)
1168                 return 0; /* don't check this path yet */
1169
1170         /*
1171          * provision a next check soonest,
1172          * in case we exit abnormaly from here
1173          */
1174         pp->tick = conf->checkint;
1175
1176         newstate = path_offline(pp);
1177         /*
1178          * Wait for uevent for removed paths;
1179          * some LLDDs like zfcp keep paths unavailable
1180          * without sending uevents.
1181          */
1182         if (newstate == PATH_REMOVED)
1183                 newstate = PATH_DOWN;
1184
1185         if (newstate == PATH_UP)
1186                 newstate = get_state(pp, 1);
1187         else
1188                 checker_clear_message(&pp->checker);
1189
1190         if (newstate == PATH_WILD || newstate == PATH_UNCHECKED) {
1191                 condlog(2, "%s: unusable path", pp->dev);
1192                 pathinfo(pp, conf->hwtable, 0);
1193                 return 1;
1194         }
1195         if (!pp->mpp) {
1196                 if (!strlen(pp->wwid) &&
1197                     (newstate == PATH_UP || newstate == PATH_GHOST)) {
1198                         condlog(2, "%s: add missing path", pp->dev);
1199                         if (pathinfo(pp, conf->hwtable, DI_ALL) == 0) {
1200                                 ev_add_path(pp, vecs);
1201                                 pp->tick = 1;
1202                         }
1203                 }
1204                 return 0;
1205         }
1206         /*
1207          * Async IO in flight. Keep the previous path state
1208          * and reschedule as soon as possible
1209          */
1210         if (newstate == PATH_PENDING) {
1211                 pp->tick = 1;
1212                 return 0;
1213         }
1214         /*
1215          * Synchronize with kernel state
1216          */
1217         if (update_multipath_strings(pp->mpp, vecs->pathvec)) {
1218                 condlog(1, "%s: Could not synchronize with kernel state",
1219                         pp->dev);
1220                 pp->dmstate = PSTATE_UNDEF;
1221         }
1222         /* if update_multipath_strings orphaned the path, quit early */
1223         if (!pp->mpp)
1224                 return 0;
1225
1226         if ((newstate == PATH_UP || newstate == PATH_GHOST) &&
1227              pp->wait_checks > 0) {
1228                 if (pp->mpp && pp->mpp->nr_active > 0) {
1229                         pp->state = PATH_DELAYED;
1230                         pp->wait_checks--;
1231                         return 1;
1232                 } else
1233                         pp->wait_checks = 0;
1234         }
1235
1236         pp->chkrstate = newstate;
1237         if (newstate != pp->state) {
1238                 int oldstate = pp->state;
1239                 pp->state = newstate;
1240
1241                 if (strlen(checker_message(&pp->checker)))
1242                         LOG_MSG(1, checker_message(&pp->checker));
1243
1244                 /*
1245                  * upon state change, reset the checkint
1246                  * to the shortest delay
1247                  */
1248                 pp->checkint = conf->checkint;
1249
1250                 if (newstate == PATH_DOWN || newstate == PATH_SHAKY) {
1251                         /*
1252                          * proactively fail path in the DM
1253                          */
1254                         if (oldstate == PATH_UP ||
1255                             oldstate == PATH_GHOST) {
1256                                 fail_path(pp, 1);
1257                                 if (pp->mpp->delay_wait_checks > 0 &&
1258                                     pp->watch_checks > 0) {
1259                                         pp->wait_checks = pp->mpp->delay_wait_checks;
1260                                         pp->watch_checks = 0;
1261                                 }
1262                         }else
1263                                 fail_path(pp, 0);
1264
1265                         /*
1266                          * cancel scheduled failback
1267                          */
1268                         pp->mpp->failback_tick = 0;
1269
1270                         pp->mpp->stat_path_failures++;
1271                         return 1;
1272                 }
1273
1274                 if(newstate == PATH_UP || newstate == PATH_GHOST){
1275                         if ( pp->mpp && pp->mpp->prflag ){
1276                                 /*
1277                                  * Check Persistent Reservation.
1278                                  */
1279                         condlog(2, "%s: checking persistent reservation "
1280                                 "registration", pp->dev);
1281                         mpath_pr_event_handle(pp);
1282                         }
1283                 }
1284
1285                 /*
1286                  * reinstate this path
1287                  */
1288                 if (oldstate != PATH_UP &&
1289                     oldstate != PATH_GHOST) {
1290                         if (pp->mpp->delay_watch_checks > 0)
1291                                 pp->watch_checks = pp->mpp->delay_watch_checks;
1292                         add_active = 1;
1293                 } else {
1294                         if (pp->watch_checks > 0)
1295                                 pp->watch_checks--;
1296                         add_active = 0;
1297                 }
1298                 if (reinstate_path(pp, add_active)) {
1299                         condlog(3, "%s: reload map", pp->dev);
1300                         ev_add_path(pp, vecs);
1301                         pp->tick = 1;
1302                         return 0;
1303                 }
1304                 new_path_up = 1;
1305
1306                 if (oldchkrstate != PATH_UP && oldchkrstate != PATH_GHOST)
1307                         chkr_new_path_up = 1;
1308
1309                 /*
1310                  * if at least one path is up in a group, and
1311                  * the group is disabled, re-enable it
1312                  */
1313                 if (newstate == PATH_UP)
1314                         enable_group(pp);
1315         }
1316         else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1317                 if (pp->dmstate == PSTATE_FAILED ||
1318                     pp->dmstate == PSTATE_UNDEF) {
1319                         /* Clear IO errors */
1320                         if (reinstate_path(pp, 0)) {
1321                                 condlog(3, "%s: reload map", pp->dev);
1322                                 ev_add_path(pp, vecs);
1323                                 pp->tick = 1;
1324                                 return 0;
1325                         }
1326                 } else {
1327                         LOG_MSG(4, checker_message(&pp->checker));
1328                         if (pp->checkint != conf->max_checkint) {
1329                                 /*
1330                                  * double the next check delay.
1331                                  * max at conf->max_checkint
1332                                  */
1333                                 if (pp->checkint < (conf->max_checkint / 2))
1334                                         pp->checkint = 2 * pp->checkint;
1335                                 else
1336                                         pp->checkint = conf->max_checkint;
1337
1338                                 condlog(4, "%s: delay next check %is",
1339                                         pp->dev_t, pp->checkint);
1340                         }
1341                         if (pp->watch_checks > 0)
1342                                 pp->watch_checks--;
1343                         pp->tick = pp->checkint;
1344                 }
1345         }
1346         else if (newstate == PATH_DOWN &&
1347                  strlen(checker_message(&pp->checker))) {
1348                 if (conf->log_checker_err == LOG_CHKR_ERR_ONCE)
1349                         LOG_MSG(3, checker_message(&pp->checker));
1350                 else
1351                         LOG_MSG(2, checker_message(&pp->checker));
1352         }
1353
1354         pp->state = newstate;
1355
1356         /*
1357          * path prio refreshing
1358          */
1359         condlog(4, "path prio refresh");
1360
1361         if (update_prio(pp, new_path_up) &&
1362             (pp->mpp->pgpolicyfn == (pgpolicyfn *)group_by_prio) &&
1363              pp->mpp->pgfailback == -FAILBACK_IMMEDIATE)
1364                 update_path_groups(pp->mpp, vecs, !new_path_up);
1365         else if (need_switch_pathgroup(pp->mpp, 0)) {
1366                 if (pp->mpp->pgfailback > 0 &&
1367                     (new_path_up || pp->mpp->failback_tick <= 0))
1368                         pp->mpp->failback_tick =
1369                                 pp->mpp->pgfailback + 1;
1370                 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE ||
1371                          (chkr_new_path_up && followover_should_failback(pp)))
1372                         switch_pathgroup(pp->mpp);
1373         }
1374         return 1;
1375 }
1376
1377 static void *
1378 checkerloop (void *ap)
1379 {
1380         struct vectors *vecs;
1381         struct path *pp;
1382         int count = 0;
1383         unsigned int i;
1384
1385         mlockall(MCL_CURRENT | MCL_FUTURE);
1386         vecs = (struct vectors *)ap;
1387         condlog(2, "path checkers start up");
1388
1389         /*
1390          * init the path check interval
1391          */
1392         vector_foreach_slot (vecs->pathvec, pp, i) {
1393                 pp->checkint = conf->checkint;
1394         }
1395
1396         while (1) {
1397                 struct timeval diff_time, start_time, end_time;
1398                 int num_paths = 0;
1399
1400                 if (gettimeofday(&start_time, NULL) != 0)
1401                         start_time.tv_sec = 0;
1402                 pthread_cleanup_push(cleanup_lock, &vecs->lock);
1403                 lock(vecs->lock);
1404                 pthread_testcancel();
1405                 condlog(4, "tick");
1406 #ifdef USE_SYSTEMD
1407                 if (use_watchdog)
1408                         sd_notify(0, "WATCHDOG=1");
1409 #endif
1410                 if (vecs->pathvec) {
1411                         vector_foreach_slot (vecs->pathvec, pp, i) {
1412                                 num_paths += check_path(vecs, pp);
1413                         }
1414                 }
1415                 if (vecs->mpvec) {
1416                         defered_failback_tick(vecs->mpvec);
1417                         retry_count_tick(vecs->mpvec);
1418                 }
1419                 if (count)
1420                         count--;
1421                 else {
1422                         condlog(4, "map garbage collection");
1423                         mpvec_garbage_collector(vecs);
1424                         count = MAPGCINT;
1425                 }
1426
1427                 lock_cleanup_pop(vecs->lock);
1428                 if (start_time.tv_sec &&
1429                     gettimeofday(&end_time, NULL) == 0 &&
1430                     num_paths) {
1431                         timersub(&end_time, &start_time, &diff_time);
1432                         condlog(3, "checked %d path%s in %lu.%06lu secs",
1433                                 num_paths, num_paths > 1 ? "s" : "",
1434                                 diff_time.tv_sec, diff_time.tv_usec);
1435                 }
1436                 sleep(1);
1437         }
1438         return NULL;
1439 }
1440
1441 int
1442 configure (struct vectors * vecs, int start_waiters)
1443 {
1444         struct multipath * mpp;
1445         struct path * pp;
1446         vector mpvec;
1447         int i, ret;
1448
1449         if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1450                 return 1;
1451
1452         if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1453                 return 1;
1454
1455         if (!(mpvec = vector_alloc()))
1456                 return 1;
1457
1458         /*
1459          * probe for current path (from sysfs) and map (from dm) sets
1460          */
1461         ret = path_discovery(vecs->pathvec, conf, DI_ALL);
1462         if (ret < 0)
1463                 return 1;
1464
1465         vector_foreach_slot (vecs->pathvec, pp, i){
1466                 if (filter_path(conf, pp) > 0){
1467                         vector_del_slot(vecs->pathvec, i);
1468                         free_path(pp);
1469                         i--;
1470                 }
1471                 else
1472                         pp->checkint = conf->checkint;
1473         }
1474         if (map_discovery(vecs))
1475                 return 1;
1476
1477         /*
1478          * create new set of maps & push changed ones into dm
1479          */
1480         if (coalesce_paths(vecs, mpvec, NULL, 1))
1481                 return 1;
1482
1483         /*
1484          * may need to remove some maps which are no longer relevant
1485          * e.g., due to blacklist changes in conf file
1486          */
1487         if (coalesce_maps(vecs, mpvec))
1488                 return 1;
1489
1490         dm_lib_release();
1491
1492         sync_maps_state(mpvec);
1493         vector_foreach_slot(mpvec, mpp, i){
1494                 remember_wwid(mpp->wwid);
1495                 update_map_pr(mpp);
1496         }
1497
1498         /*
1499          * purge dm of old maps
1500          */
1501         remove_maps(vecs);
1502
1503         /*
1504          * save new set of maps formed by considering current path state
1505          */
1506         vector_free(vecs->mpvec);
1507         vecs->mpvec = mpvec;
1508
1509         /*
1510          * start dm event waiter threads for these new maps
1511          */
1512         vector_foreach_slot(vecs->mpvec, mpp, i) {
1513                 if (setup_multipath(vecs, mpp))
1514                         return 1;
1515                 if (start_waiters)
1516                         if (start_waiter_thread(mpp, vecs))
1517                                 return 1;
1518         }
1519         return 0;
1520 }
1521
1522 int
1523 reconfigure (struct vectors * vecs)
1524 {
1525         struct config * old = conf;
1526         int retval = 1;
1527
1528         running_state = DAEMON_CONFIGURE;
1529
1530         /*
1531          * free old map and path vectors ... they use old conf state
1532          */
1533         if (VECTOR_SIZE(vecs->mpvec))
1534                 remove_maps_and_stop_waiters(vecs);
1535
1536         if (VECTOR_SIZE(vecs->pathvec))
1537                 free_pathvec(vecs->pathvec, FREE_PATHS);
1538
1539         vecs->pathvec = NULL;
1540         conf = NULL;
1541
1542         /* Re-read any timezone changes */
1543         tzset();
1544
1545         if (!load_config(DEFAULT_CONFIGFILE, udev)) {
1546                 dm_drv_version(conf->version, TGT_MPATH);
1547                 conf->verbosity = old->verbosity;
1548                 conf->daemon = 1;
1549                 configure(vecs, 1);
1550                 free_config(old);
1551                 retval = 0;
1552         }
1553
1554         running_state = DAEMON_RUNNING;
1555
1556         return retval;
1557 }
1558
1559 static struct vectors *
1560 init_vecs (void)
1561 {
1562         struct vectors * vecs;
1563
1564         vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1565
1566         if (!vecs)
1567                 return NULL;
1568
1569         vecs->lock.mutex =
1570                 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1571
1572         if (!vecs->lock.mutex)
1573                 goto out;
1574
1575         pthread_mutex_init(vecs->lock.mutex, NULL);
1576         vecs->lock.depth = 0;
1577
1578         return vecs;
1579
1580 out:
1581         FREE(vecs);
1582         condlog(0, "failed to init paths");
1583         return NULL;
1584 }
1585
1586 static void *
1587 signal_set(int signo, void (*func) (int))
1588 {
1589         int r;
1590         struct sigaction sig;
1591         struct sigaction osig;
1592
1593         sig.sa_handler = func;
1594         sigemptyset(&sig.sa_mask);
1595         sig.sa_flags = 0;
1596
1597         r = sigaction(signo, &sig, &osig);
1598
1599         if (r < 0)
1600                 return (SIG_ERR);
1601         else
1602                 return (osig.sa_handler);
1603 }
1604
1605 void
1606 handle_signals(void)
1607 {
1608         if (reconfig_sig && running_state == DAEMON_RUNNING) {
1609                 condlog(2, "reconfigure (signal)");
1610                 pthread_cleanup_push(cleanup_lock,
1611                                 &gvecs->lock);
1612                 lock(gvecs->lock);
1613                 pthread_testcancel();
1614                 reconfigure(gvecs);
1615                 lock_cleanup_pop(gvecs->lock);
1616         }
1617         if (log_reset_sig) {
1618                 condlog(2, "reset log (signal)");
1619                 pthread_mutex_lock(&logq_lock);
1620                 log_reset("multipathd");
1621                 pthread_mutex_unlock(&logq_lock);
1622         }
1623         reconfig_sig = 0;
1624         log_reset_sig = 0;
1625 }
1626
1627 static void
1628 sighup (int sig)
1629 {
1630         reconfig_sig = 1;
1631 }
1632
1633 static void
1634 sigend (int sig)
1635 {
1636         exit_daemon();
1637 }
1638
1639 static void
1640 sigusr1 (int sig)
1641 {
1642         log_reset_sig = 1;
1643 }
1644
1645 static void
1646 sigusr2 (int sig)
1647 {
1648         condlog(3, "SIGUSR2 received");
1649 }
1650
1651 static void
1652 signal_init(void)
1653 {
1654         sigset_t set;
1655
1656         sigemptyset(&set);
1657         sigaddset(&set, SIGHUP);
1658         sigaddset(&set, SIGUSR1);
1659         sigaddset(&set, SIGUSR2);
1660         pthread_sigmask(SIG_BLOCK, &set, NULL);
1661
1662         signal_set(SIGHUP, sighup);
1663         signal_set(SIGUSR1, sigusr1);
1664         signal_set(SIGUSR2, sigusr2);
1665         signal_set(SIGINT, sigend);
1666         signal_set(SIGTERM, sigend);
1667         signal(SIGPIPE, SIG_IGN);
1668 }
1669
1670 static void
1671 setscheduler (void)
1672 {
1673         int res;
1674         static struct sched_param sched_param = {
1675                 .sched_priority = 99
1676         };
1677
1678         res = sched_setscheduler (0, SCHED_RR, &sched_param);
1679
1680         if (res == -1)
1681                 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1682         return;
1683 }
1684
1685 static void
1686 set_oom_adj (void)
1687 {
1688 #ifdef OOM_SCORE_ADJ_MIN
1689         int retry = 1;
1690         char *file = "/proc/self/oom_score_adj";
1691         int score = OOM_SCORE_ADJ_MIN;
1692 #else
1693         int retry = 0;
1694         char *file = "/proc/self/oom_adj";
1695         int score = OOM_ADJUST_MIN;
1696 #endif
1697         FILE *fp;
1698         struct stat st;
1699         char *envp;
1700
1701         envp = getenv("OOMScoreAdjust");
1702         if (envp) {
1703                 condlog(3, "Using systemd provided OOMScoreAdjust");
1704                 return;
1705         }
1706         do {
1707                 if (stat(file, &st) == 0){
1708                         fp = fopen(file, "w");
1709                         if (!fp) {
1710                                 condlog(0, "couldn't fopen %s : %s", file,
1711                                         strerror(errno));
1712                                 return;
1713                         }
1714                         fprintf(fp, "%i", score);
1715                         fclose(fp);
1716                         return;
1717                 }
1718                 if (errno != ENOENT) {
1719                         condlog(0, "couldn't stat %s : %s", file,
1720                                 strerror(errno));
1721                         return;
1722                 }
1723 #ifdef OOM_ADJUST_MIN
1724                 file = "/proc/self/oom_adj";
1725                 score = OOM_ADJUST_MIN;
1726 #else
1727                 retry = 0;
1728 #endif
1729         } while (retry--);
1730         condlog(0, "couldn't adjust oom score");
1731 }
1732
1733 static int
1734 child (void * param)
1735 {
1736         pthread_t check_thr, uevent_thr, uxlsnr_thr, uevq_thr;
1737         pthread_attr_t log_attr, misc_attr, uevent_attr;
1738         struct vectors * vecs;
1739         struct multipath * mpp;
1740         int i;
1741 #ifdef USE_SYSTEMD
1742         unsigned long checkint;
1743 #endif
1744         int rc, pid_rc;
1745         char *envp;
1746
1747         mlockall(MCL_CURRENT | MCL_FUTURE);
1748         sem_init(&exit_sem, 0, 0);
1749         signal_init();
1750
1751         udev = udev_new();
1752
1753         setup_thread_attr(&misc_attr, 64 * 1024, 1);
1754         setup_thread_attr(&uevent_attr, DEFAULT_UEVENT_STACKSIZE * 1024, 1);
1755         setup_thread_attr(&waiter_attr, 32 * 1024, 1);
1756
1757         if (logsink == 1) {
1758                 setup_thread_attr(&log_attr, 64 * 1024, 0);
1759                 log_thread_start(&log_attr);
1760                 pthread_attr_destroy(&log_attr);
1761         }
1762
1763         running_state = DAEMON_START;
1764
1765 #ifdef USE_SYSTEMD
1766         sd_notify(0, "STATUS=startup");
1767 #endif
1768         condlog(2, "--------start up--------");
1769         condlog(2, "read " DEFAULT_CONFIGFILE);
1770
1771         if (load_config(DEFAULT_CONFIGFILE, udev))
1772                 goto failed;
1773
1774         dm_drv_version(conf->version, TGT_MPATH);
1775         if (init_checkers()) {
1776                 condlog(0, "failed to initialize checkers");
1777                 goto failed;
1778         }
1779         if (init_prio()) {
1780                 condlog(0, "failed to initialize prioritizers");
1781                 goto failed;
1782         }
1783
1784         setlogmask(LOG_UPTO(conf->verbosity + 3));
1785
1786         envp = getenv("LimitNOFILE");
1787
1788         if (envp) {
1789                 condlog(2,"Using systemd provided open fds limit of %s", envp);
1790         } else if (conf->max_fds) {
1791                 struct rlimit fd_limit;
1792
1793                 if (getrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
1794                         condlog(0, "can't get open fds limit: %s",
1795                                 strerror(errno));
1796                         fd_limit.rlim_cur = 0;
1797                         fd_limit.rlim_max = 0;
1798                 }
1799                 if (fd_limit.rlim_cur < conf->max_fds) {
1800                         fd_limit.rlim_cur = conf->max_fds;
1801                         if (fd_limit.rlim_max < conf->max_fds)
1802                                 fd_limit.rlim_max = conf->max_fds;
1803                         if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
1804                                 condlog(0, "can't set open fds limit to "
1805                                         "%lu/%lu : %s",
1806                                         fd_limit.rlim_cur, fd_limit.rlim_max,
1807                                         strerror(errno));
1808                         } else {
1809                                 condlog(3, "set open fds limit to %lu/%lu",
1810                                         fd_limit.rlim_cur, fd_limit.rlim_max);
1811                         }
1812                 }
1813
1814         }
1815
1816         vecs = gvecs = init_vecs();
1817         if (!vecs)
1818                 goto failed;
1819
1820         setscheduler();
1821         set_oom_adj();
1822
1823         conf->daemon = 1;
1824         dm_udev_set_sync_support(0);
1825 #ifdef USE_SYSTEMD
1826         envp = getenv("WATCHDOG_USEC");
1827         if (envp && sscanf(envp, "%lu", &checkint) == 1) {
1828                 /* Value is in microseconds */
1829                 conf->max_checkint = checkint / 1000000;
1830                 /* Rescale checkint */
1831                 if (conf->checkint > conf->max_checkint)
1832                         conf->checkint = conf->max_checkint;
1833                 else
1834                         conf->checkint = conf->max_checkint / 4;
1835                 condlog(3, "enabling watchdog, interval %d max %d",
1836                         conf->checkint, conf->max_checkint);
1837                 use_watchdog = conf->checkint;
1838         }
1839 #endif
1840         /*
1841          * Start uevent listener early to catch events
1842          */
1843         if ((rc = pthread_create(&uevent_thr, &uevent_attr, ueventloop, udev))) {
1844                 condlog(0, "failed to create uevent thread: %d", rc);
1845                 goto failed;
1846         }
1847         pthread_attr_destroy(&uevent_attr);
1848         if ((rc = pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs))) {
1849                 condlog(0, "failed to create cli listener: %d", rc);
1850                 goto failed;
1851         }
1852         /*
1853          * fetch and configure both paths and multipaths
1854          */
1855 #ifdef USE_SYSTEMD
1856         sd_notify(0, "STATUS=configure");
1857 #endif
1858         running_state = DAEMON_CONFIGURE;
1859
1860         lock(vecs->lock);
1861         if (configure(vecs, 1)) {
1862                 unlock(vecs->lock);
1863                 condlog(0, "failure during configuration");
1864                 goto failed;
1865         }
1866         unlock(vecs->lock);
1867
1868         /*
1869          * start threads
1870          */
1871         if ((rc = pthread_create(&check_thr, &misc_attr, checkerloop, vecs))) {
1872                 condlog(0,"failed to create checker loop thread: %d", rc);
1873                 goto failed;
1874         }
1875         if ((rc = pthread_create(&uevq_thr, &misc_attr, uevqloop, vecs))) {
1876                 condlog(0, "failed to create uevent dispatcher: %d", rc);
1877                 goto failed;
1878         }
1879         pthread_attr_destroy(&misc_attr);
1880
1881         /* Startup complete, create logfile */
1882         pid_rc = pidfile_create(DEFAULT_PIDFILE, daemon_pid);
1883         /* Ignore errors, we can live without */
1884
1885         running_state = DAEMON_RUNNING;
1886 #ifdef USE_SYSTEMD
1887         sd_notify(0, "READY=1\nSTATUS=running");
1888 #endif
1889
1890         /*
1891          * exit path
1892          */
1893         while(sem_wait(&exit_sem) != 0); /* Do nothing */
1894
1895 #ifdef USE_SYSTEMD
1896         sd_notify(0, "STATUS=shutdown");
1897 #endif
1898         running_state = DAEMON_SHUTDOWN;
1899         lock(vecs->lock);
1900         if (conf->queue_without_daemon == QUE_NO_DAEMON_OFF)
1901                 vector_foreach_slot(vecs->mpvec, mpp, i)
1902                         dm_queue_if_no_path(mpp->alias, 0);
1903         remove_maps_and_stop_waiters(vecs);
1904         unlock(vecs->lock);
1905
1906         pthread_cancel(check_thr);
1907         pthread_cancel(uevent_thr);
1908         pthread_cancel(uxlsnr_thr);
1909         pthread_cancel(uevq_thr);
1910
1911         lock(vecs->lock);
1912         free_pathvec(vecs->pathvec, FREE_PATHS);
1913         vecs->pathvec = NULL;
1914         unlock(vecs->lock);
1915         /* Now all the waitevent threads will start rushing in. */
1916         while (vecs->lock.depth > 0) {
1917                 sleep (1); /* This is weak. */
1918                 condlog(3, "Have %d wait event checkers threads to de-alloc,"
1919                         " waiting...", vecs->lock.depth);
1920         }
1921         pthread_mutex_destroy(vecs->lock.mutex);
1922         FREE(vecs->lock.mutex);
1923         vecs->lock.depth = 0;
1924         vecs->lock.mutex = NULL;
1925         FREE(vecs);
1926         vecs = NULL;
1927
1928         cleanup_checkers();
1929         cleanup_prio();
1930
1931         dm_lib_release();
1932         dm_lib_exit();
1933
1934         /* We're done here */
1935         if (!pid_rc) {
1936                 condlog(3, "unlink pidfile");
1937                 unlink(DEFAULT_PIDFILE);
1938         }
1939
1940         condlog(2, "--------shut down-------");
1941
1942         if (logsink == 1)
1943                 log_thread_stop();
1944
1945         /*
1946          * Freeing config must be done after condlog() and dm_lib_exit(),
1947          * because logging functions like dlog() and dm_write_log()
1948          * reference the config.
1949          */
1950         free_config(conf);
1951         conf = NULL;
1952         udev_unref(udev);
1953         udev = NULL;
1954 #ifdef _DEBUG_
1955         dbg_free_final(NULL);
1956 #endif
1957
1958 #ifdef USE_SYSTEMD
1959         sd_notify(0, "ERRNO=0");
1960 #endif
1961         exit(0);
1962
1963 failed:
1964 #ifdef USE_SYSTEMD
1965         sd_notify(0, "ERRNO=1");
1966 #endif
1967         exit(1);
1968 }
1969
1970 static int
1971 daemonize(void)
1972 {
1973         int pid;
1974         int dev_null_fd;
1975
1976         if( (pid = fork()) < 0){
1977                 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1978                 return -1;
1979         }
1980         else if (pid != 0)
1981                 return pid;
1982
1983         setsid();
1984
1985         if ( (pid = fork()) < 0)
1986                 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1987         else if (pid != 0)
1988                 _exit(0);
1989
1990         if (chdir("/") < 0)
1991                 fprintf(stderr, "cannot chdir to '/', continuing\n");
1992
1993         dev_null_fd = open("/dev/null", O_RDWR);
1994         if (dev_null_fd < 0){
1995                 fprintf(stderr, "cannot open /dev/null for input & output : %s\n",
1996                         strerror(errno));
1997                 _exit(0);
1998         }
1999
2000         close(STDIN_FILENO);
2001         if (dup(dev_null_fd) < 0) {
2002                 fprintf(stderr, "cannot dup /dev/null to stdin : %s\n",
2003                         strerror(errno));
2004                 _exit(0);
2005         }
2006         close(STDOUT_FILENO);
2007         if (dup(dev_null_fd) < 0) {
2008                 fprintf(stderr, "cannot dup /dev/null to stdout : %s\n",
2009                         strerror(errno));
2010                 _exit(0);
2011         }
2012         close(STDERR_FILENO);
2013         if (dup(dev_null_fd) < 0) {
2014                 fprintf(stderr, "cannot dup /dev/null to stderr : %s\n",
2015                         strerror(errno));
2016                 _exit(0);
2017         }
2018         close(dev_null_fd);
2019         daemon_pid = getpid();
2020         return 0;
2021 }
2022
2023 int
2024 main (int argc, char *argv[])
2025 {
2026         extern char *optarg;
2027         extern int optind;
2028         int arg;
2029         int err;
2030         int foreground = 0;
2031
2032         logsink = 1;
2033         running_state = DAEMON_INIT;
2034         dm_init();
2035
2036         if (getuid() != 0) {
2037                 fprintf(stderr, "need to be root\n");
2038                 exit(1);
2039         }
2040
2041         /* make sure we don't lock any path */
2042         if (chdir("/") < 0)
2043                 fprintf(stderr, "can't chdir to root directory : %s\n",
2044                         strerror(errno));
2045         umask(umask(077) | 022);
2046
2047         conf = alloc_config();
2048
2049         if (!conf)
2050                 exit(1);
2051
2052         while ((arg = getopt(argc, argv, ":dsv:k::B")) != EOF ) {
2053         switch(arg) {
2054                 case 'd':
2055                         foreground = 1;
2056                         if (logsink > 0)
2057                                 logsink = 0;
2058                         //debug=1; /* ### comment me out ### */
2059                         break;
2060                 case 'v':
2061                         if (sizeof(optarg) > sizeof(char *) ||
2062                             !isdigit(optarg[0]))
2063                                 exit(1);
2064
2065                         conf->verbosity = atoi(optarg);
2066                         break;
2067                 case 's':
2068                         logsink = -1;
2069                         break;
2070                 case 'k':
2071                         if (load_config(DEFAULT_CONFIGFILE, udev_new()))
2072                                 exit(1);
2073                         uxclnt(optarg, conf->uxsock_timeout);
2074                         exit(0);
2075                 case 'B':
2076                         conf->bindings_read_only = 1;
2077                         break;
2078                 default:
2079                         ;
2080                 }
2081         }
2082         if (optind < argc) {
2083                 char cmd[CMDSIZE];
2084                 char * s = cmd;
2085                 char * c = s;
2086
2087                 if (load_config(DEFAULT_CONFIGFILE, udev_new()))
2088                         exit(1);
2089                 while (optind < argc) {
2090                         if (strchr(argv[optind], ' '))
2091                                 c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
2092                         else
2093                                 c += snprintf(c, s + CMDSIZE - c, "%s ", argv[optind]);
2094                         optind++;
2095                 }
2096                 c += snprintf(c, s + CMDSIZE - c, "\n");
2097                 uxclnt(s, conf->uxsock_timeout);
2098                 exit(0);
2099         }
2100
2101         if (foreground) {
2102                 if (!isatty(fileno(stdout)))
2103                         setbuf(stdout, NULL);
2104                 err = 0;
2105                 daemon_pid = getpid();
2106         } else
2107                 err = daemonize();
2108
2109         if (err < 0)
2110                 /* error */
2111                 exit(1);
2112         else if (err > 0)
2113                 /* parent dies */
2114                 exit(0);
2115         else
2116                 /* child lives */
2117                 return (child(NULL));
2118 }
2119
2120 void *  mpath_pr_event_handler_fn (void * pathp )
2121 {
2122         struct multipath * mpp;
2123         int i,j, ret, isFound;
2124         struct path * pp = (struct path *)pathp;
2125         unsigned char *keyp;
2126         uint64_t prkey;
2127         struct prout_param_descriptor *param;
2128         struct prin_resp *resp;
2129
2130         mpp = pp->mpp;
2131
2132         resp = mpath_alloc_prin_response(MPATH_PRIN_RKEY_SA);
2133         if (!resp){
2134                 condlog(0,"%s Alloc failed for prin response", pp->dev);
2135                 return NULL;
2136         }
2137
2138         ret = prin_do_scsi_ioctl(pp->dev, MPATH_PRIN_RKEY_SA, resp, 0);
2139         if (ret != MPATH_PR_SUCCESS )
2140         {
2141                 condlog(0,"%s : pr in read keys service action failed. Error=%d", pp->dev, ret);
2142                 goto out;
2143         }
2144
2145         condlog(3, " event pr=%d addlen=%d",resp->prin_descriptor.prin_readkeys.prgeneration,
2146                         resp->prin_descriptor.prin_readkeys.additional_length );
2147
2148         if (resp->prin_descriptor.prin_readkeys.additional_length == 0 )
2149         {
2150                 condlog(1, "%s: No key found. Device may not be registered.", pp->dev);
2151                 ret = MPATH_PR_SUCCESS;
2152                 goto out;
2153         }
2154         prkey = 0;
2155         keyp = (unsigned char *)mpp->reservation_key;
2156         for (j = 0; j < 8; ++j) {
2157                 if (j > 0)
2158                         prkey <<= 8;
2159                 prkey |= *keyp;
2160                 ++keyp;
2161         }
2162         condlog(2, "Multipath  reservation_key: 0x%" PRIx64 " ", prkey);
2163
2164         isFound =0;
2165         for (i = 0; i < resp->prin_descriptor.prin_readkeys.additional_length/8; i++ )
2166         {
2167                 condlog(2, "PR IN READKEYS[%d]  reservation key:",i);
2168                 dumpHex((char *)&resp->prin_descriptor.prin_readkeys.key_list[i*8], 8 , -1);
2169                 if (!memcmp(mpp->reservation_key, &resp->prin_descriptor.prin_readkeys.key_list[i*8], 8))
2170                 {
2171                         condlog(2, "%s: pr key found in prin readkeys response", mpp->alias);
2172                         isFound =1;
2173                         break;
2174                 }
2175         }
2176         if (!isFound)
2177         {
2178                 condlog(0, "%s: Either device not registered or ", pp->dev);
2179                 condlog(0, "host is not authorised for registration. Skip path");
2180                 ret = MPATH_PR_OTHER;
2181                 goto out;
2182         }
2183
2184         param= malloc(sizeof(struct prout_param_descriptor));
2185         memset(param, 0 , sizeof(struct prout_param_descriptor));
2186
2187         for (j = 7; j >= 0; --j) {
2188                 param->sa_key[j] = (prkey & 0xff);
2189                 prkey >>= 8;
2190         }
2191         param->num_transportid = 0;
2192
2193         condlog(3, "device %s:%s", pp->dev, pp->mpp->wwid);
2194
2195         ret = prout_do_scsi_ioctl(pp->dev, MPATH_PROUT_REG_IGN_SA, 0, 0, param, 0);
2196         if (ret != MPATH_PR_SUCCESS )
2197         {
2198                 condlog(0,"%s: Reservation registration failed. Error: %d", pp->dev, ret);
2199         }
2200         mpp->prflag = 1;
2201
2202         free(param);
2203 out:
2204         free(resp);
2205         return NULL;
2206 }
2207
2208 int mpath_pr_event_handle(struct path *pp)
2209 {
2210         pthread_t thread;
2211         int rc;
2212         pthread_attr_t attr;
2213         struct multipath * mpp;
2214
2215         mpp = pp->mpp;
2216
2217         if (!mpp->reservation_key)
2218                 return -1;
2219
2220         pthread_attr_init(&attr);
2221         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
2222
2223         rc = pthread_create(&thread, NULL , mpath_pr_event_handler_fn, pp);
2224         if (rc) {
2225                 condlog(0, "%s: ERROR; return code from pthread_create() is %d", pp->dev, rc);
2226                 return -1;
2227         }
2228         pthread_attr_destroy(&attr);
2229         rc = pthread_join(thread, NULL);
2230         return 0;
2231 }
2232