do not allow relative path names to be added to the pathvec
[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 #include <sys/time.h>
16 #include <sys/resource.h>
17 #include <limits.h>
18
19 /*
20  * libcheckers
21  */
22 #include <checkers.h>
23
24 /*
25  * libmultipath
26  */
27 #include <parser.h>
28 #include <vector.h>
29 #include <memory.h>
30 #include <config.h>
31 #include <util.h>
32 #include <hwtable.h>
33 #include <defaults.h>
34 #include <structs.h>
35 #include <callout.h>
36 #include <blacklist.h>
37 #include <structs_vec.h>
38 #include <dmparser.h>
39 #include <devmapper.h>
40 #include <sysfs.h>
41 #include <dict.h>
42 #include <discovery.h>
43 #include <debug.h>
44 #include <propsel.h>
45 #include <uevent.h>
46 #include <switchgroup.h>
47 #include <print.h>
48 #include <configure.h>
49 #include <prio.h>
50
51 #include "main.h"
52 #include "pidfile.h"
53 #include "uxlsnr.h"
54 #include "uxclnt.h"
55 #include "cli.h"
56 #include "cli_handlers.h"
57 #include "lock.h"
58 #include "waiter.h"
59
60 #define FILE_NAME_SIZE 256
61 #define CMDSIZE 160
62
63 #define LOG_MSG(a,b) \
64         if (strlen(b)) condlog(a, "%s: %s", pp->dev, b);
65
66 pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
67 pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
68
69 int logsink;
70
71 /*
72  * global copy of vecs for use in sig handlers
73  */
74 struct vectors * gvecs;
75
76 static int
77 need_switch_pathgroup (struct multipath * mpp, int refresh)
78 {
79         struct pathgroup * pgp;
80         struct path * pp;
81         unsigned int i, j;
82
83         if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
84                 return 0;
85
86         /*
87          * Refresh path priority values
88          */
89         if (refresh)
90                 vector_foreach_slot (mpp->pg, pgp, i)
91                         vector_foreach_slot (pgp->paths, pp, j)
92                                 pathinfo(pp, conf->hwtable, DI_PRIO);
93
94         mpp->bestpg = select_path_group(mpp);
95
96         if (mpp->bestpg != mpp->nextpg)
97                 return 1;
98
99         return 0;
100 }
101
102 static void
103 switch_pathgroup (struct multipath * mpp)
104 {
105         mpp->stat_switchgroup++;
106         dm_switchgroup(mpp->alias, mpp->bestpg);
107         condlog(2, "%s: switch to path group #%i",
108                  mpp->alias, mpp->bestpg);
109 }
110
111 static int
112 coalesce_maps(struct vectors *vecs, vector nmpv)
113 {
114         struct multipath * ompp;
115         vector ompv = vecs->mpvec;
116         unsigned int i;
117         int j;
118
119         vector_foreach_slot (ompv, ompp, i) {
120                 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
121                         /*
122                          * remove all current maps not allowed by the
123                          * current configuration
124                          */
125                         if (dm_flush_map(ompp->alias)) {
126                                 condlog(0, "%s: unable to flush devmap",
127                                         ompp->alias);
128                                 /*
129                                  * may be just because the device is open
130                                  */
131                                 if (!vector_alloc_slot(nmpv))
132                                         return 1;
133
134                                 vector_set_slot(nmpv, ompp);
135                                 setup_multipath(vecs, ompp);
136
137                                 if ((j = find_slot(ompv, (void *)ompp)) != -1)
138                                         vector_del_slot(ompv, j);
139
140                                 continue;
141                         }
142                         else {
143                                 dm_lib_release();
144                                 condlog(2, "%s devmap removed", ompp->alias);
145                         }
146                 }
147         }
148         return 0;
149 }
150
151 void
152 sync_map_state(struct multipath *mpp)
153 {
154         struct pathgroup *pgp;
155         struct path *pp;
156         unsigned int i, j;
157
158         if (!mpp->pg)
159                 return;
160
161         vector_foreach_slot (mpp->pg, pgp, i){
162                 vector_foreach_slot (pgp->paths, pp, j){
163                         if (pp->state == PATH_UNCHECKED || 
164                             pp->state == PATH_WILD)
165                                 continue;
166                         if ((pp->dmstate == PSTATE_FAILED ||
167                              pp->dmstate == PSTATE_UNDEF) &&
168                             (pp->state == PATH_UP || pp->state == PATH_GHOST))
169                                 dm_reinstate_path(mpp->alias, pp->dev_t);
170                         else if ((pp->dmstate == PSTATE_ACTIVE ||
171                                   pp->dmstate == PSTATE_UNDEF) &&
172                                  (pp->state == PATH_DOWN ||
173                                   pp->state == PATH_SHAKY))
174                                 dm_fail_path(mpp->alias, pp->dev_t);
175                 }
176         }
177 }
178
179 static void
180 sync_maps_state(vector mpvec)
181 {
182         unsigned int i;
183         struct multipath *mpp;
184
185         vector_foreach_slot (mpvec, mpp, i)
186                 sync_map_state(mpp);
187 }
188
189 static int
190 flush_map(struct multipath * mpp, struct vectors * vecs)
191 {
192         /*
193          * clear references to this map before flushing so we can ignore
194          * the spurious uevent we may generate with the dm_flush_map call below
195          */
196         if (dm_flush_map(mpp->alias)) {
197                 /*
198                  * May not really be an error -- if the map was already flushed
199                  * from the device mapper by dmsetup(8) for instance.
200                  */
201                 condlog(0, "%s: can't flush", mpp->alias);
202                 return 1;
203         }
204         else {
205                 dm_lib_release();
206                 condlog(2, "%s: devmap removed", mpp->alias);
207         }
208
209         orphan_paths(vecs->pathvec, mpp);
210         remove_map_and_stop_waiter(mpp, vecs, 1);
211
212         return 0;
213 }
214
215 static int
216 uev_add_map (struct sysfs_device * dev, struct vectors * vecs)
217 {
218         condlog(2, "%s: add map (uevent)", dev->kernel);
219         return ev_add_map(dev, vecs);
220 }
221
222 int
223 ev_add_map (struct sysfs_device * dev, struct vectors * vecs)
224 {
225         char * alias;
226         char *dev_t;
227         int major, minor;
228         char * refwwid;
229         struct multipath * mpp;
230         int map_present;
231         int r = 1;
232
233         dev_t = sysfs_attr_get_value(dev->devpath, "dev");
234
235         if (!dev_t || sscanf(dev_t, "%d:%d", &major, &minor) != 2)
236                 return 1;
237
238         alias = dm_mapname(major, minor);
239
240         if (!alias)
241                 return 1;
242
243         map_present = dm_map_present(alias);
244
245         if (map_present && dm_type(alias, TGT_MPATH) <= 0) {
246                 condlog(4, "%s: not a multipath map", alias);
247                 FREE(alias);
248                 return 0;
249         }
250
251         mpp = find_mp_by_alias(vecs->mpvec, alias);
252
253         if (mpp) {
254                 /*
255                  * Not really an error -- we generate our own uevent
256                  * if we create a multipath mapped device as a result
257                  * of uev_add_path
258                  */
259                 condlog(0, "%s: devmap already registered",
260                         dev->kernel);
261                 FREE(alias);
262                 return 0;
263         }
264
265         /*
266          * now we can register the map
267          */
268         if (map_present && (mpp = add_map_without_path(vecs, minor, alias))) {
269                 sync_map_state(mpp);
270                 condlog(2, "%s: devmap %s added", alias, dev->kernel);
271                 return 0;
272         }
273         refwwid = get_refwwid(dev->kernel, DEV_DEVMAP, vecs->pathvec);
274
275         if (refwwid) {
276                 r = coalesce_paths(vecs, NULL, refwwid, 0);
277                 dm_lib_release();
278         }
279
280         if (!r)
281                 condlog(2, "%s: devmap %s added", alias, dev->kernel);
282         else
283                 condlog(0, "%s: uev_add_map %s failed", alias, dev->kernel);
284
285         FREE(refwwid);
286         FREE(alias);
287         return r;
288 }
289
290 static int
291 uev_remove_map (struct sysfs_device * dev, struct vectors * vecs)
292 {
293         condlog(2, "%s: remove map (uevent)", dev->kernel);
294         return ev_remove_map(dev->kernel, vecs);
295 }
296
297 int
298 ev_remove_map (char * devname, struct vectors * vecs)
299 {
300         struct multipath * mpp;
301
302         mpp = find_mp_by_str(vecs->mpvec, devname);
303
304         if (!mpp) {
305                 condlog(2, "%s: devmap not registered, can't remove",
306                         devname);
307                 return 0;
308         }
309         flush_map(mpp, vecs);
310
311         return 0;
312 }
313
314 static int
315 uev_umount_map (struct sysfs_device * dev, struct vectors * vecs)
316 {
317         struct multipath * mpp;
318
319         condlog(2, "%s: umount map (uevent)", dev->kernel);
320
321         mpp = find_mp_by_str(vecs->mpvec, dev->kernel);
322
323         if (!mpp)
324                 return 0;
325
326         update_mpp_paths(mpp, vecs->pathvec);
327         verify_paths(mpp, vecs, NULL);
328
329         if (!VECTOR_SIZE(mpp->paths))
330                 flush_map(mpp, vecs);
331
332         return 0;
333 }
334
335 static int
336 uev_add_path (struct sysfs_device * dev, struct vectors * vecs)
337 {
338         condlog(2, "%s: add path (uevent)", dev->kernel);
339         return (ev_add_path(dev->kernel, vecs) != 1)? 0 : 1;
340 }
341
342
343 /*
344  * returns:
345  * 0: added
346  * 1: error
347  * 2: blacklisted
348  */
349 int
350 ev_add_path (char * devname, struct vectors * vecs)
351 {
352         struct multipath * mpp;
353         struct path * pp;
354         char empty_buff[WWID_SIZE] = {0};
355
356         if (strstr(devname, "..") != NULL) {
357                 /*
358                  * Don't allow relative device names in the pathvec
359                  */
360                 condlog(0, "%s: path name is invalid", devname);
361                 return 1;
362         }
363
364         pp = find_path_by_dev(vecs->pathvec, devname);
365
366         if (pp) {
367                 condlog(0, "%s: spurious uevent, path already in pathvec",
368                         devname);
369                 if (pp->mpp)
370                         return 0;
371         }
372         else {
373                 /*
374                  * get path vital state
375                  */
376                 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
377                       devname, DI_ALL))) {
378                         condlog(0, "%s: failed to store path info", devname);
379                         return 1;
380                 }
381                 pp->checkint = conf->checkint;
382         }
383
384         /*
385          * need path UID to go any further
386          */
387         if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
388                 condlog(0, "%s: failed to get path uid", devname);
389                 return 1; /* leave path added to pathvec */
390         }
391         if (filter_path(conf, pp) > 0){
392                 int i = find_slot(vecs->pathvec, (void *)pp);
393                 if (i != -1)
394                         vector_del_slot(vecs->pathvec, i);
395                 free_path(pp);
396                 return 2;
397         }
398         mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
399 rescan:
400         if (mpp) {
401                 condlog(4,"%s: adopting all paths for path %s",
402                         mpp->alias, pp->dev);
403                 if (adopt_paths(vecs->pathvec, mpp))
404                         return 1; /* leave path added to pathvec */
405
406                 verify_paths(mpp, vecs, NULL);
407                 mpp->flush_on_last_del = FLUSH_UNDEF;
408                 mpp->action = ACT_RELOAD;
409         }
410         else {
411                 condlog(4,"%s: creating new map", pp->dev);
412                 if ((mpp = add_map_with_path(vecs, pp, 1)))
413                         mpp->action = ACT_CREATE;
414                 else
415                         return 1; /* leave path added to pathvec */
416         }
417
418         /*
419          * push the map to the device-mapper
420          */
421         if (setup_map(mpp)) {
422                 condlog(0, "%s: failed to setup map for addition of new "
423                         "path %s", mpp->alias, devname);
424                 goto out;
425         }
426         /*
427          * reload the map for the multipath mapped device
428          */
429         if (domap(mpp) <= 0) {
430                 condlog(0, "%s: failed in domap for addition of new "
431                         "path %s", mpp->alias, devname);
432                 /*
433                  * deal with asynchronous uevents :((
434                  */
435                 if (mpp->action == ACT_RELOAD) {
436                         condlog(0, "%s: uev_add_path sleep", mpp->alias);
437                         sleep(1);
438                         update_mpp_paths(mpp, vecs->pathvec);
439                         goto rescan;
440                 }
441                 else
442                         goto out;
443         }
444         dm_lib_release();
445
446         /*
447          * update our state from kernel regardless of create or reload
448          */
449         if (setup_multipath(vecs, mpp))
450                 goto out;
451
452         sync_map_state(mpp);
453
454         if (mpp->action == ACT_CREATE &&
455             start_waiter_thread(mpp, vecs))
456                         goto out;
457
458         condlog(2, "%s path added to devmap %s", devname, mpp->alias);
459         return 0;
460
461 out:
462         remove_map(mpp, vecs, 1);
463         return 1;
464 }
465
466 static int
467 uev_remove_path (struct sysfs_device * dev, struct vectors * vecs)
468 {
469         int retval;
470
471         condlog(2, "%s: remove path (uevent)", dev->kernel);
472         retval = ev_remove_path(dev->kernel, vecs);
473         if (!retval)
474                 sysfs_device_put(dev);
475
476         return retval;
477 }
478
479 int
480 ev_remove_path (char * devname, struct vectors * vecs)
481 {
482         struct multipath * mpp;
483         struct path * pp;
484         int i, retval = 0;
485
486         pp = find_path_by_dev(vecs->pathvec, devname);
487
488         if (!pp) {
489                 /* Not an error; path might have been purged earlier */
490                 condlog(0, "%s: path already removed", devname);
491                 return 0;
492         }
493
494         /*
495          * avoid referring to the map of an orphaned path
496          */
497         if ((mpp = pp->mpp)) {
498                 /*
499                  * transform the mp->pg vector of vectors of paths
500                  * into a mp->params string to feed the device-mapper
501                  */
502                 if (update_mpp_paths(mpp, vecs->pathvec)) {
503                         condlog(0, "%s: failed to update paths",
504                                 mpp->alias);
505                         goto fail;
506                 }
507                 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
508                         vector_del_slot(mpp->paths, i);
509
510                 /*
511                  * remove the map IFF removing the last path
512                  */
513                 if (VECTOR_SIZE(mpp->paths) == 0) {
514                         char alias[WWID_SIZE];
515
516                         /*
517                          * flush_map will fail if the device is open
518                          */
519                         strncpy(alias, mpp->alias, WWID_SIZE);
520                         if (mpp->flush_on_last_del == FLUSH_ENABLED) {
521                                 condlog(2, "%s Last path deleted, disabling queueing", mpp->alias);
522                                 mpp->retry_tick = 0;
523                                 mpp->no_path_retry = NO_PATH_RETRY_FAIL;
524                                 mpp->flush_on_last_del = FLUSH_IN_PROGRESS;
525                                 dm_queue_if_no_path(mpp->alias, 0);
526                         }
527                         if (!flush_map(mpp, vecs)) {
528                                 condlog(2, "%s: removed map after"
529                                         " removing all paths",
530                                         alias);
531                                 retval = 0;
532                                 goto out;
533                         }
534                         /*
535                          * Not an error, continue
536                          */
537                 }
538
539                 if (setup_map(mpp)) {
540                         condlog(0, "%s: failed to setup map for"
541                                 " removal of path %s", mpp->alias,
542                                 devname);
543                         goto fail;
544                 }
545                 /*
546                  * reload the map
547                  */
548                 mpp->action = ACT_RELOAD;
549                 if (domap(mpp) <= 0) {
550                         condlog(0, "%s: failed in domap for "
551                                 "removal of path %s",
552                                 mpp->alias, devname);
553                         retval = 1;
554                 } else {
555                         /*
556                          * update our state from kernel
557                          */
558                         if (setup_multipath(vecs, mpp)) {
559                                 goto fail;
560                         }
561                         sync_map_state(mpp);
562
563                         condlog(2, "%s: path removed from map %s",
564                                 devname, mpp->alias);
565                 }
566         }
567
568 out:
569         if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
570                 vector_del_slot(vecs->pathvec, i);
571
572         free_path(pp);
573
574         return retval;
575
576 fail:
577         remove_map_and_stop_waiter(mpp, vecs, 1);
578         return 1;
579 }
580
581 static int
582 map_discovery (struct vectors * vecs)
583 {
584         struct multipath * mpp;
585         unsigned int i;
586
587         if (dm_get_maps(vecs->mpvec))
588                 return 1;
589
590         vector_foreach_slot (vecs->mpvec, mpp, i)
591                 if (setup_multipath(vecs, mpp))
592                         return 1;
593
594         return 0;
595 }
596
597 int
598 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
599 {
600         struct vectors * vecs;
601         int r;
602
603         *reply = NULL;
604         *len = 0;
605         vecs = (struct vectors *)trigger_data;
606
607         pthread_cleanup_push(cleanup_lock, &vecs->lock);
608         lock(vecs->lock);
609
610         r = parse_cmd(str, reply, len, vecs);
611
612         if (r > 0) {
613                 *reply = STRDUP("fail\n");
614                 *len = strlen(*reply) + 1;
615                 r = 1;
616         }
617         else if (!r && *len == 0) {
618                 *reply = STRDUP("ok\n");
619                 *len = strlen(*reply) + 1;
620                 r = 0;
621         }
622         /* else if (r < 0) leave *reply alone */
623
624         lock_cleanup_pop(vecs->lock);
625         return r;
626 }
627
628 static int
629 uev_discard(char * devpath)
630 {
631         char *tmp;
632         char a[11], b[11];
633
634         /*
635          * keep only block devices, discard partitions
636          */
637         tmp = strstr(devpath, "/block/");
638         if (tmp == NULL){
639                 condlog(4, "no /block/ in '%s'", devpath);
640                 return 1;
641         }
642         if (sscanf(tmp, "/block/%10s", a) != 1 ||
643             sscanf(tmp, "/block/%10[^/]/%10s", a, b) == 2) {
644                 condlog(4, "discard event on %s", devpath);
645                 return 1;
646         }
647         return 0;
648 }
649
650 int
651 uev_trigger (struct uevent * uev, void * trigger_data)
652 {
653         int r = 0;
654         struct sysfs_device *sysdev;
655         struct vectors * vecs;
656
657         vecs = (struct vectors *)trigger_data;
658
659         if (uev_discard(uev->devpath))
660                 return 0;
661
662         sysdev = sysfs_device_get(uev->devpath);
663         if(!sysdev)
664                 return 0;
665
666         lock(vecs->lock);
667
668         /*
669          * device map event
670          * Add events are ignored here as the tables
671          * are not fully initialised then.
672          */
673         if (!strncmp(sysdev->kernel, "dm-", 3)) {
674                 if (!strncmp(uev->action, "change", 6)) {
675                         r = uev_add_map(sysdev, vecs);
676                         goto out;
677                 }
678                 if (!strncmp(uev->action, "remove", 6)) {
679                         r = uev_remove_map(sysdev, vecs);
680                         goto out;
681                 }
682                 if (!strncmp(uev->action, "umount", 6)) {
683                         r = uev_umount_map(sysdev, vecs);
684                         goto out;
685                 }
686                 goto out;
687         }
688
689         /*
690          * path add/remove event
691          */
692         if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
693                            sysdev->kernel) > 0)
694                 goto out;
695
696         if (!strncmp(uev->action, "add", 3)) {
697                 r = uev_add_path(sysdev, vecs);
698                 goto out;
699         }
700         if (!strncmp(uev->action, "remove", 6)) {
701                 r = uev_remove_path(sysdev, vecs);
702                 goto out;
703         }
704
705 out:
706         unlock(vecs->lock);
707         return r;
708 }
709
710 static void *
711 ueventloop (void * ap)
712 {
713         block_signal(SIGUSR1, NULL);
714         block_signal(SIGHUP, NULL);
715
716         if (uevent_listen(&uev_trigger, ap))
717                 fprintf(stderr, "error starting uevent listener");
718
719         return NULL;
720 }
721
722 static void *
723 uxlsnrloop (void * ap)
724 {
725         block_signal(SIGUSR1, NULL);
726         block_signal(SIGHUP, NULL);
727
728         if (cli_init())
729                 return NULL;
730
731         set_handler_callback(LIST+PATHS, cli_list_paths);
732         set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
733         set_handler_callback(LIST+MAPS, cli_list_maps);
734         set_handler_callback(LIST+STATUS, cli_list_status);
735         set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
736         set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
737         set_handler_callback(LIST+MAPS+FMT, cli_list_maps_fmt);
738         set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
739         set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
740         set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
741         set_handler_callback(LIST+CONFIG, cli_list_config);
742         set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
743         set_handler_callback(LIST+DEVICES, cli_list_devices);
744         set_handler_callback(LIST+WILDCARDS, cli_list_wildcards);
745         set_handler_callback(ADD+PATH, cli_add_path);
746         set_handler_callback(DEL+PATH, cli_del_path);
747         set_handler_callback(ADD+MAP, cli_add_map);
748         set_handler_callback(DEL+MAP, cli_del_map);
749         set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
750         set_handler_callback(RECONFIGURE, cli_reconfigure);
751         set_handler_callback(SUSPEND+MAP, cli_suspend);
752         set_handler_callback(RESUME+MAP, cli_resume);
753         set_handler_callback(RESIZE+MAP, cli_resize);
754         set_handler_callback(REINSTATE+PATH, cli_reinstate);
755         set_handler_callback(FAIL+PATH, cli_fail);
756         set_handler_callback(DISABLEQ+MAP, cli_disable_queueing);
757         set_handler_callback(RESTOREQ+MAP, cli_restore_queueing);
758         set_handler_callback(DISABLEQ+MAPS, cli_disable_all_queueing);
759         set_handler_callback(RESTOREQ+MAPS, cli_restore_all_queueing);
760         set_handler_callback(QUIT, cli_quit);
761
762         uxsock_listen(&uxsock_trigger, ap);
763
764         return NULL;
765 }
766
767 static int
768 exit_daemon (int status)
769 {
770         if (status != 0)
771                 fprintf(stderr, "bad exit status. see daemon.log\n");
772
773         condlog(3, "unlink pidfile");
774         unlink(DEFAULT_PIDFILE);
775
776         pthread_mutex_lock(&exit_mutex);
777         pthread_cond_signal(&exit_cond);
778         pthread_mutex_unlock(&exit_mutex);
779
780         return status;
781 }
782
783 static void
784 fail_path (struct path * pp, int del_active)
785 {
786         if (!pp->mpp)
787                 return;
788
789         condlog(2, "checker failed path %s in map %s",
790                  pp->dev_t, pp->mpp->alias);
791
792         dm_fail_path(pp->mpp->alias, pp->dev_t);
793         if (del_active)
794                 update_queue_mode_del_path(pp->mpp);
795 }
796
797 /*
798  * caller must have locked the path list before calling that function
799  */
800 static void
801 reinstate_path (struct path * pp, int add_active)
802 {
803         if (!pp->mpp)
804                 return;
805
806         if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
807                 condlog(0, "%s: reinstate failed", pp->dev_t);
808         else {
809                 condlog(2, "%s: reinstated", pp->dev_t);
810                 if (add_active)
811                         update_queue_mode_add_path(pp->mpp);
812         }
813 }
814
815 static void
816 enable_group(struct path * pp)
817 {
818         struct pathgroup * pgp;
819
820         /*
821          * if path is added through uev_add_path, pgindex can be unset.
822          * next update_strings() will set it, upon map reload event.
823          *
824          * we can safely return here, because upon map reload, all
825          * PG will be enabled.
826          */
827         if (!pp->mpp->pg || !pp->pgindex)
828                 return;
829
830         pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
831
832         if (pgp->status == PGSTATE_DISABLED) {
833                 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
834                 dm_enablegroup(pp->mpp->alias, pp->pgindex);
835         }
836 }
837
838 static void
839 mpvec_garbage_collector (struct vectors * vecs)
840 {
841         struct multipath * mpp;
842         unsigned int i;
843
844         if (!vecs->mpvec)
845                 return;
846
847         vector_foreach_slot (vecs->mpvec, mpp, i) {
848                 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
849                         condlog(2, "%s: remove dead map", mpp->alias);
850                         remove_map_and_stop_waiter(mpp, vecs, 1);
851                         i--;
852                 }
853         }
854 }
855
856 static void
857 defered_failback_tick (vector mpvec)
858 {
859         struct multipath * mpp;
860         unsigned int i;
861
862         vector_foreach_slot (mpvec, mpp, i) {
863                 /*
864                  * defered failback getting sooner
865                  */
866                 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
867                         mpp->failback_tick--;
868
869                         if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
870                                 switch_pathgroup(mpp);
871                 }
872         }
873 }
874
875 static void
876 retry_count_tick(vector mpvec)
877 {
878         struct multipath *mpp;
879         unsigned int i;
880
881         vector_foreach_slot (mpvec, mpp, i) {
882                 if (mpp->retry_tick) {
883                         mpp->stat_total_queueing_time++;
884                         condlog(4, "%s: Retrying.. No active path", mpp->alias);
885                         if(--mpp->retry_tick == 0) {
886                                 dm_queue_if_no_path(mpp->alias, 0);
887                                 condlog(2, "%s: Disable queueing", mpp->alias);
888                         }
889                 }
890         }
891 }
892
893 void
894 check_path (struct vectors * vecs, struct path * pp)
895 {
896         int newstate;
897
898         if (!pp->mpp)
899                 return;
900
901         if (pp->tick && --pp->tick)
902                 return; /* don't check this path yet */
903
904         /*
905          * provision a next check soonest,
906          * in case we exit abnormaly from here
907          */
908         pp->tick = conf->checkint;
909
910         if (!checker_selected(&pp->checker)) {
911                 pathinfo(pp, conf->hwtable, DI_SYSFS);
912                 select_checker(pp);
913         }
914         if (!checker_selected(&pp->checker)) {
915                 condlog(0, "%s: checker is not set", pp->dev);
916                 return;
917         }
918         /*
919          * Set checker in async mode.
920          * Honored only by checker implementing the said mode.
921          */
922         checker_set_async(&pp->checker);
923
924         if (path_offline(pp))
925                 newstate = PATH_DOWN;
926         else
927                 newstate = checker_check(&pp->checker);
928
929         if (newstate < 0) {
930                 condlog(2, "%s: unusable path", pp->dev);
931                 pathinfo(pp, conf->hwtable, 0);
932                 return;
933         }
934         /*
935          * Async IO in flight. Keep the previous path state
936          * and reschedule as soon as possible
937          */
938         if (newstate == PATH_PENDING) {
939                 pp->tick = 1;
940                 return;
941         }
942         if (newstate != pp->state) {
943                 int oldstate = pp->state;
944                 pp->state = newstate;
945                 LOG_MSG(1, checker_message(&pp->checker));
946
947                 /*
948                  * upon state change, reset the checkint
949                  * to the shortest delay
950                  */
951                 pp->checkint = conf->checkint;
952
953                 if (newstate == PATH_DOWN || newstate == PATH_SHAKY ||
954                     update_multipath_strings(pp->mpp, vecs->pathvec)) {
955                         /*
956                          * proactively fail path in the DM
957                          */
958                         if (oldstate == PATH_UP ||
959                             oldstate == PATH_GHOST)
960                                 fail_path(pp, 1);
961                         else
962                                 fail_path(pp, 0);
963
964                         /*
965                          * cancel scheduled failback
966                          */
967                         pp->mpp->failback_tick = 0;
968
969                         pp->mpp->stat_path_failures++;
970                         return;
971                 }
972
973                 /*
974                  * reinstate this path
975                  */
976                 if (oldstate != PATH_UP &&
977                     oldstate != PATH_GHOST)
978                         reinstate_path(pp, 1);
979                 else
980                         reinstate_path(pp, 0);
981
982                 /*
983                  * schedule [defered] failback
984                  */
985                 if (pp->mpp->pgfailback > 0)
986                         pp->mpp->failback_tick =
987                                 pp->mpp->pgfailback + 1;
988                 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
989                     need_switch_pathgroup(pp->mpp, 1))
990                         switch_pathgroup(pp->mpp);
991
992                 /*
993                  * if at least one path is up in a group, and
994                  * the group is disabled, re-enable it
995                  */
996                 if (newstate == PATH_UP)
997                         enable_group(pp);
998         }
999         else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1000                 LOG_MSG(4, checker_message(&pp->checker));
1001                 /*
1002                  * double the next check delay.
1003                  * max at conf->max_checkint
1004                  */
1005                 if (pp->checkint < (conf->max_checkint / 2))
1006                         pp->checkint = 2 * pp->checkint;
1007                 else
1008                         pp->checkint = conf->max_checkint;
1009
1010                 pp->tick = pp->checkint;
1011                 condlog(4, "%s: delay next check %is",
1012                                 pp->dev_t, pp->tick);
1013         }
1014         else if (newstate == PATH_DOWN)
1015                 LOG_MSG(2, checker_message(&pp->checker));
1016
1017         pp->state = newstate;
1018
1019         /*
1020          * path prio refreshing
1021          */
1022         condlog(4, "path prio refresh");
1023         pathinfo(pp, conf->hwtable, DI_PRIO);
1024
1025         /*
1026          * pathgroup failback policy
1027          */
1028         if (need_switch_pathgroup(pp->mpp, 0)) {
1029                 if (pp->mpp->pgfailback > 0 &&
1030                     pp->mpp->failback_tick <= 0)
1031                         pp->mpp->failback_tick =
1032                                 pp->mpp->pgfailback + 1;
1033                 else if (pp->mpp->pgfailback ==
1034                                 -FAILBACK_IMMEDIATE)
1035                         switch_pathgroup(pp->mpp);
1036         }
1037 }
1038
1039 static void *
1040 checkerloop (void *ap)
1041 {
1042         struct vectors *vecs;
1043         struct path *pp;
1044         int count = 0;
1045         unsigned int i;
1046         sigset_t old;
1047
1048         mlockall(MCL_CURRENT | MCL_FUTURE);
1049         vecs = (struct vectors *)ap;
1050         condlog(2, "path checkers start up");
1051
1052         /*
1053          * init the path check interval
1054          */
1055         vector_foreach_slot (vecs->pathvec, pp, i) {
1056                 pp->checkint = conf->checkint;
1057         }
1058
1059         while (1) {
1060                 block_signal(SIGHUP, &old);
1061                 pthread_cleanup_push(cleanup_lock, &vecs->lock);
1062                 lock(vecs->lock);
1063                 condlog(4, "tick");
1064
1065                 if (vecs->pathvec) {
1066                         vector_foreach_slot (vecs->pathvec, pp, i) {
1067                                 check_path(vecs, pp);
1068                         }
1069                 }
1070                 if (vecs->mpvec) {
1071                         defered_failback_tick(vecs->mpvec);
1072                         retry_count_tick(vecs->mpvec);
1073                 }
1074                 if (count)
1075                         count--;
1076                 else {
1077                         condlog(4, "map garbage collection");
1078                         mpvec_garbage_collector(vecs);
1079                         count = MAPGCINT;
1080                 }
1081
1082                 lock_cleanup_pop(vecs->lock);
1083                 pthread_sigmask(SIG_SETMASK, &old, NULL);
1084                 sleep(1);
1085         }
1086         return NULL;
1087 }
1088
1089 int
1090 configure (struct vectors * vecs, int start_waiters)
1091 {
1092         struct multipath * mpp;
1093         struct path * pp;
1094         vector mpvec;
1095         int i;
1096
1097         if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1098                 return 1;
1099
1100         if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1101                 return 1;
1102
1103         if (!(mpvec = vector_alloc()))
1104                 return 1;
1105
1106         /*
1107          * probe for current path (from sysfs) and map (from dm) sets
1108          */
1109         path_discovery(vecs->pathvec, conf, DI_ALL);
1110
1111         vector_foreach_slot (vecs->pathvec, pp, i){
1112                 if (filter_path(conf, pp) > 0){
1113                         vector_del_slot(vecs->pathvec, i);
1114                         free_path(pp);
1115                         i--;
1116                 }
1117                 else
1118                         pp->checkint = conf->checkint;
1119         }
1120         if (map_discovery(vecs))
1121                 return 1;
1122
1123         /*
1124          * create new set of maps & push changed ones into dm
1125          */
1126         if (coalesce_paths(vecs, mpvec, NULL, 0))
1127                 return 1;
1128
1129         /*
1130          * may need to remove some maps which are no longer relevant
1131          * e.g., due to blacklist changes in conf file
1132          */
1133         if (coalesce_maps(vecs, mpvec))
1134                 return 1;
1135
1136         dm_lib_release();
1137
1138         sync_maps_state(mpvec);
1139
1140         /*
1141          * purge dm of old maps
1142          */
1143         remove_maps(vecs);
1144
1145         /*
1146          * save new set of maps formed by considering current path state
1147          */
1148         vector_free(vecs->mpvec);
1149         vecs->mpvec = mpvec;
1150
1151         /*
1152          * start dm event waiter threads for these new maps
1153          */
1154         vector_foreach_slot(vecs->mpvec, mpp, i) {
1155                 if (setup_multipath(vecs, mpp))
1156                         return 1;
1157                 if (start_waiters)
1158                         if (start_waiter_thread(mpp, vecs))
1159                                 return 1;
1160         }
1161         return 0;
1162 }
1163
1164 int
1165 reconfigure (struct vectors * vecs)
1166 {
1167         struct config * old = conf;
1168
1169         /*
1170          * free old map and path vectors ... they use old conf state
1171          */
1172         if (VECTOR_SIZE(vecs->mpvec))
1173                 remove_maps_and_stop_waiters(vecs);
1174
1175         if (VECTOR_SIZE(vecs->pathvec))
1176                 free_pathvec(vecs->pathvec, FREE_PATHS);
1177
1178         vecs->pathvec = NULL;
1179         conf = NULL;
1180
1181         if (load_config(DEFAULT_CONFIGFILE))
1182                 return 1;
1183
1184         conf->verbosity = old->verbosity;
1185
1186         if (!conf->checkint) {
1187                 conf->checkint = DEFAULT_CHECKINT;
1188                 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1189         }
1190         configure(vecs, 1);
1191         free_config(old);
1192         return 0;
1193 }
1194
1195 static struct vectors *
1196 init_vecs (void)
1197 {
1198         struct vectors * vecs;
1199
1200         vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1201
1202         if (!vecs)
1203                 return NULL;
1204
1205         vecs->lock.mutex =
1206                 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1207
1208         if (!vecs->lock.mutex)
1209                 goto out;
1210
1211         pthread_mutex_init(vecs->lock.mutex, NULL);
1212         vecs->lock.depth = 0;
1213
1214         return vecs;
1215
1216 out:
1217         FREE(vecs);
1218         condlog(0, "failed to init paths");
1219         return NULL;
1220 }
1221
1222 static void *
1223 signal_set(int signo, void (*func) (int))
1224 {
1225         int r;
1226         struct sigaction sig;
1227         struct sigaction osig;
1228
1229         sig.sa_handler = func;
1230         sigemptyset(&sig.sa_mask);
1231         sig.sa_flags = 0;
1232
1233         r = sigaction(signo, &sig, &osig);
1234
1235         if (r < 0)
1236                 return (SIG_ERR);
1237         else
1238                 return (osig.sa_handler);
1239 }
1240
1241 static void
1242 sighup (int sig)
1243 {
1244         condlog(2, "reconfigure (SIGHUP)");
1245
1246         lock(gvecs->lock);
1247         reconfigure(gvecs);
1248         unlock(gvecs->lock);
1249
1250 #ifdef _DEBUG_
1251         dbg_free_final(NULL);
1252 #endif
1253 }
1254
1255 static void
1256 sigend (int sig)
1257 {
1258         exit_daemon(0);
1259 }
1260
1261 static void
1262 sigusr1 (int sig)
1263 {
1264         condlog(3, "SIGUSR1 received");
1265 }
1266
1267 static void
1268 signal_init(void)
1269 {
1270         signal_set(SIGHUP, sighup);
1271         signal_set(SIGUSR1, sigusr1);
1272         signal_set(SIGINT, sigend);
1273         signal_set(SIGTERM, sigend);
1274         signal(SIGPIPE, SIG_IGN);
1275 }
1276
1277 static void
1278 setscheduler (void)
1279 {
1280         int res;
1281         static struct sched_param sched_param = {
1282                 .sched_priority = 99
1283         };
1284
1285         res = sched_setscheduler (0, SCHED_RR, &sched_param);
1286
1287         if (res == -1)
1288                 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1289         return;
1290 }
1291
1292 static void
1293 set_oom_adj (int val)
1294 {
1295         FILE *fp;
1296
1297         fp = fopen("/proc/self/oom_adj", "w");
1298
1299         if (!fp)
1300                 return;
1301
1302         fprintf(fp, "%i", val);
1303         fclose(fp);
1304 }
1305
1306 void
1307 setup_thread_attr(pthread_attr_t *attr, size_t stacksize, int detached)
1308 {
1309         if (pthread_attr_init(attr)) {
1310                 fprintf(stderr, "can't initialize thread attr: %s\n",
1311                         strerror(errno));
1312                 exit(1);
1313         }
1314         if (stacksize < PTHREAD_STACK_MIN)
1315                 stacksize = PTHREAD_STACK_MIN;
1316
1317         if (pthread_attr_setstacksize(attr, stacksize)) {
1318                 fprintf(stderr, "can't set thread stack size to %lu: %s\n",
1319                         (unsigned long)stacksize, strerror(errno));
1320                 exit(1);
1321         }
1322         if (detached && pthread_attr_setdetachstate(attr,
1323                                                     PTHREAD_CREATE_DETACHED)) {
1324                 fprintf(stderr, "can't set thread to detached: %s\n",
1325                         strerror(errno));
1326                 exit(1);
1327         }
1328 }
1329
1330 static int
1331 child (void * param)
1332 {
1333         pthread_t check_thr, uevent_thr, uxlsnr_thr;
1334         pthread_attr_t log_attr, misc_attr;
1335         struct vectors * vecs;
1336
1337         mlockall(MCL_CURRENT | MCL_FUTURE);
1338
1339         setup_thread_attr(&misc_attr, 64 * 1024, 1);
1340         setup_thread_attr(&waiter_attr, 32 * 1024, 1);
1341
1342         if (logsink) {
1343                 setup_thread_attr(&log_attr, 64 * 1024, 0);
1344                 log_thread_start(&log_attr);
1345                 pthread_attr_destroy(&log_attr);
1346         }
1347
1348         condlog(2, "--------start up--------");
1349         condlog(2, "read " DEFAULT_CONFIGFILE);
1350
1351         if (load_config(DEFAULT_CONFIGFILE))
1352                 exit(1);
1353
1354         if (init_checkers()) {
1355                 condlog(0, "failed to initialize checkers");
1356                 exit(1);
1357         }
1358         if (init_prio()) {
1359                 condlog(0, "failed to initialize prioritizers");
1360                 exit(1);
1361         }
1362
1363         setlogmask(LOG_UPTO(conf->verbosity + 3));
1364
1365         /*
1366          * fill the voids left in the config file
1367          */
1368         if (!conf->checkint) {
1369                 conf->checkint = DEFAULT_CHECKINT;
1370                 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1371         }
1372
1373         if (conf->max_fds) {
1374                 struct rlimit fd_limit;
1375                 if (conf->max_fds > 0) {
1376                         fd_limit.rlim_cur = conf->max_fds;
1377                         fd_limit.rlim_max = conf->max_fds;
1378                 }
1379                 else {
1380                         fd_limit.rlim_cur = RLIM_INFINITY;
1381                         fd_limit.rlim_max = RLIM_INFINITY;
1382                 }
1383                 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
1384                         condlog(0, "can't set open fds limit to %d : %s\n",
1385                                 conf->max_fds, strerror(errno));
1386         }
1387
1388         if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1389                 if (logsink)
1390                         log_thread_stop();
1391
1392                 exit(1);
1393         }
1394         signal_init();
1395         setscheduler();
1396         set_oom_adj(-16);
1397         vecs = gvecs = init_vecs();
1398
1399         if (!vecs)
1400                 exit(1);
1401
1402         if (sysfs_init(conf->sysfs_dir, FILE_NAME_SIZE)) {
1403                 condlog(0, "can not find sysfs mount point");
1404                 exit(1);
1405         }
1406         conf->daemon = 1;
1407         /*
1408          * fetch and configure both paths and multipaths
1409          */
1410         if (configure(vecs, 1)) {
1411                 condlog(0, "failure during configuration");
1412                 exit(1);
1413         }
1414         /*
1415          * start threads
1416          */
1417         pthread_create(&check_thr, &misc_attr, checkerloop, vecs);
1418         pthread_create(&uevent_thr, &misc_attr, ueventloop, vecs);
1419         pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs);
1420         pthread_attr_destroy(&misc_attr);
1421
1422         pthread_cond_wait(&exit_cond, &exit_mutex);
1423
1424         /*
1425          * exit path
1426          */
1427         block_signal(SIGHUP, NULL);
1428         lock(vecs->lock);
1429         remove_maps_and_stop_waiters(vecs);
1430         free_pathvec(vecs->pathvec, FREE_PATHS);
1431
1432         pthread_cancel(check_thr);
1433         pthread_cancel(uevent_thr);
1434         pthread_cancel(uxlsnr_thr);
1435
1436         sysfs_cleanup();
1437
1438         free_keys(keys);
1439         keys = NULL;
1440         free_handlers(handlers);
1441         handlers = NULL;
1442         free_polls();
1443
1444         unlock(vecs->lock);
1445         /* Now all the waitevent threads will start rushing in. */
1446         while (vecs->lock.depth > 0) {
1447                 sleep (1); /* This is weak. */
1448                 condlog(3,"Have %d wait event checkers threads to de-alloc, waiting..\n", vecs->lock.depth);
1449         }
1450         pthread_mutex_destroy(vecs->lock.mutex);
1451         FREE(vecs->lock.mutex);
1452         vecs->lock.depth = 0;
1453         vecs->lock.mutex = NULL;
1454         FREE(vecs);
1455         vecs = NULL;
1456
1457         condlog(2, "--------shut down-------");
1458
1459         if (logsink)
1460                 log_thread_stop();
1461
1462         dm_lib_release();
1463         dm_lib_exit();
1464
1465         cleanup_prio();
1466         cleanup_checkers();
1467         /*
1468          * Freeing config must be done after condlog() and dm_lib_exit(),
1469          * because logging functions like dlog() and dm_write_log()
1470          * reference the config.
1471          */
1472         free_config(conf);
1473         conf = NULL;
1474
1475 #ifdef _DEBUG_
1476         dbg_free_final(NULL);
1477 #endif
1478
1479         exit(0);
1480 }
1481
1482 static int
1483 daemonize(void)
1484 {
1485         int pid;
1486         int in_fd, out_fd;
1487
1488         if( (pid = fork()) < 0){
1489                 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1490                 return -1;
1491         }
1492         else if (pid != 0)
1493                 return pid;
1494
1495         setsid();
1496
1497         if ( (pid = fork()) < 0)
1498                 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1499         else if (pid != 0)
1500                 _exit(0);
1501
1502         in_fd = open("/dev/null", O_RDONLY);
1503         if (in_fd < 0){
1504                 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1505                         strerror(errno));
1506                 _exit(0);
1507         }
1508         out_fd = open("/dev/console", O_WRONLY);
1509         if (out_fd < 0){
1510                 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1511                         strerror(errno));
1512                 _exit(0);
1513         }
1514
1515         close(STDIN_FILENO);
1516         dup(in_fd);
1517         close(STDOUT_FILENO);
1518         dup(out_fd);
1519         close(STDERR_FILENO);
1520         dup(out_fd);
1521
1522         close(in_fd);
1523         close(out_fd);
1524         if (chdir("/") < 0)
1525                 fprintf(stderr, "cannot chdir to '/', continuing\n");
1526
1527         return 0;
1528 }
1529
1530 int
1531 main (int argc, char *argv[])
1532 {
1533         extern char *optarg;
1534         extern int optind;
1535         int arg;
1536         int err;
1537
1538         logsink = 1;
1539         dm_init();
1540
1541         if (getuid() != 0) {
1542                 fprintf(stderr, "need to be root\n");
1543                 exit(1);
1544         }
1545
1546         /* make sure we don't lock any path */
1547         chdir("/");
1548         umask(umask(077) | 022);
1549
1550         conf = alloc_config();
1551
1552         if (!conf)
1553                 exit(1);
1554
1555         while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1556         switch(arg) {
1557                 case 'd':
1558                         logsink = 0;
1559                         //debug=1; /* ### comment me out ### */
1560                         break;
1561                 case 'v':
1562                         if (sizeof(optarg) > sizeof(char *) ||
1563                             !isdigit(optarg[0]))
1564                                 exit(1);
1565
1566                         conf->verbosity = atoi(optarg);
1567                         break;
1568                 case 'k':
1569                         uxclnt(optarg);
1570                         exit(0);
1571                 default:
1572                         ;
1573                 }
1574         }
1575         if (optind < argc) {
1576                 char cmd[CMDSIZE];
1577                 char * s = cmd;
1578                 char * c = s;
1579
1580                 while (optind < argc) {
1581                         if (strchr(argv[optind], ' '))
1582                                 c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
1583                         else
1584                                 c += snprintf(c, s + CMDSIZE - c, "%s ", argv[optind]);
1585                         optind++;
1586                 }
1587                 c += snprintf(c, s + CMDSIZE - c, "\n");
1588                 uxclnt(s);
1589                 exit(0);
1590         }
1591
1592         if (!logsink)
1593                 err = 0;
1594         else
1595                 err = daemonize();
1596
1597         if (err < 0)
1598                 /* error */
1599                 exit(1);
1600         else if (err > 0)
1601                 /* parent dies */
1602                 exit(0);
1603         else
1604                 /* child lives */
1605                 return (child(NULL));
1606 }
1607