d08c6881b98b3de4ce17005d94375d78ac07c1b5
[multipath-tools/.git] / multipath / main.c
1 /*
2  * Soft:        multipath device mapper target autoconfig
3  *
4  * Version:     $Id: main.h,v 0.0.1 2003/09/18 15:13:38 cvaroqui Exp $
5  *
6  * Author:      Christophe Varoqui
7  *
8  *              This program is distributed in the hope that it will be useful,
9  *              but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *              See the GNU General Public License for more details.
12  *
13  *              This program is free software; you can redistribute it and/or
14  *              modify it under the terms of the GNU General Public License
15  *              as published by the Free Software Foundation; either version
16  *              2 of the License, or (at your option) any later version.
17  *
18  * Copyright (c) 2003, 2004, 2005 Christophe Varoqui
19  * Copyright (c) 2005 Benjamin Marzinski, Redhat
20  * Copyright (c) 2005 Kiyoshi Ueda, NEC
21  * Copyright (c) 2005 Patrick Caulfield, Redhat
22  * Copyright (c) 2005 Edward Goggin, EMC
23  */
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <ctype.h>
30 #include <libudev.h>
31 #include <syslog.h>
32
33 #include "checkers.h"
34 #include "prio.h"
35 #include "vector.h"
36 #include "memory.h"
37 #include <libdevmapper.h>
38 #include "devmapper.h"
39 #include "util.h"
40 #include "defaults.h"
41 #include "config.h"
42 #include "structs.h"
43 #include "structs_vec.h"
44 #include "dmparser.h"
45 #include "sysfs.h"
46 #include "blacklist.h"
47 #include "discovery.h"
48 #include "debug.h"
49 #include "switchgroup.h"
50 #include "dm-generic.h"
51 #include "print.h"
52 #include "alias.h"
53 #include "configure.h"
54 #include "pgpolicies.h"
55 #include "version.h"
56 #include <errno.h>
57 #include <sys/time.h>
58 #include <sys/resource.h>
59 #include "wwids.h"
60 #include "uxsock.h"
61 #include "mpath_cmd.h"
62 #include "foreign.h"
63
64 int logsink;
65 struct udev *udev;
66 struct config *multipath_conf;
67
68 struct config *get_multipath_config(void)
69 {
70         return multipath_conf;
71 }
72
73 void put_multipath_config(void *arg)
74 {
75         /* Noop for now */
76 }
77
78 void rcu_register_thread_memb(void) {}
79
80 void rcu_unregister_thread_memb(void) {}
81
82 static int
83 filter_pathvec (vector pathvec, char * refwwid)
84 {
85         int i;
86         struct path * pp;
87
88         if (!refwwid || !strlen(refwwid))
89                 return 0;
90
91         vector_foreach_slot (pathvec, pp, i) {
92                 if (strncmp(pp->wwid, refwwid, WWID_SIZE) != 0) {
93                         condlog(3, "skip path %s : out of scope", pp->dev);
94                         free_path(pp);
95                         vector_del_slot(pathvec, i);
96                         i--;
97                 }
98         }
99         return 0;
100 }
101
102 static void
103 usage (char * progname)
104 {
105         fprintf (stderr, VERSION_STRING);
106         fprintf (stderr, "Usage:\n");
107         fprintf (stderr, "  %s [-a|-c|-w|-W] [-d] [-r] [-i] [-v lvl] [-p pol] [-b fil] [-q] [dev]\n", progname);
108         fprintf (stderr, "  %s -l|-ll|-f [-v lvl] [-b fil] [-R num] [dev]\n", progname);
109         fprintf (stderr, "  %s -F [-v lvl] [-R num]\n", progname);
110         fprintf (stderr, "  %s -t\n", progname);
111         fprintf (stderr, "  %s -h\n", progname);
112         fprintf (stderr,
113                 "\n"
114                 "Where:\n"
115                 "  -h      print this usage text\n"
116                 "  -l      show multipath topology (sysfs and DM info)\n"
117                 "  -ll     show multipath topology (maximum info)\n"
118                 "  -f      flush a multipath device map\n"
119                 "  -F      flush all multipath device maps\n"
120                 "  -a      add a device wwid to the wwids file\n"
121                 "  -c      check if a device should be a path in a multipath device\n"
122                 "  -C      check if a multipath device has usable paths\n"
123                 "  -q      allow queue_if_no_path when multipathd is not running\n"
124                 "  -d      dry run, do not create or update devmaps\n"
125                 "  -t      display the currently used multipathd configuration\n"
126                 "  -r      force devmap reload\n"
127                 "  -i      ignore wwids file\n"
128                 "  -B      treat the bindings file as read only\n"
129                 "  -b fil  bindings file location\n"
130                 "  -w      remove a device from the wwids file\n"
131                 "  -W      reset the wwids file include only the current devices\n"
132                 "  -p pol  force all maps to specified path grouping policy :\n"
133                 "          . failover            one path per priority group\n"
134                 "          . multibus            all paths in one priority group\n"
135                 "          . group_by_serial     one priority group per serial\n"
136                 "          . group_by_prio       one priority group per priority lvl\n"
137                 "          . group_by_node_name  one priority group per target node\n"
138                 "  -v lvl  verbosity level\n"
139                 "          . 0 no output\n"
140                 "          . 1 print created devmap names only\n"
141                 "          . 2 default verbosity\n"
142                 "          . 3 print debug information\n"
143                 "  -R num  number of times to retry removes of in-use devices\n"
144                 "  dev     action limited to:\n"
145                 "          . multipath named 'dev' (ex: mpath0) or\n"
146                 "          . multipath whose wwid is 'dev' (ex: 60051..)\n"
147                 "          . multipath including the path named 'dev' (ex: /dev/sda)\n"
148                 "          . multipath including the path with maj:min 'dev' (ex: 8:0)\n"
149                 );
150
151 }
152
153 static int
154 update_paths (struct multipath * mpp, int quick)
155 {
156         int i, j;
157         struct pathgroup * pgp;
158         struct path * pp;
159         struct config *conf;
160
161         if (!mpp->pg)
162                 return 0;
163
164         vector_foreach_slot (mpp->pg, pgp, i) {
165                 if (!pgp->paths)
166                         continue;
167
168                 vector_foreach_slot (pgp->paths, pp, j) {
169                         if (!strlen(pp->dev)) {
170                                 if (devt2devname(pp->dev, FILE_NAME_SIZE,
171                                                  pp->dev_t)) {
172                                         /*
173                                          * path is not in sysfs anymore
174                                          */
175                                         pp->chkrstate = pp->state = PATH_DOWN;
176                                         pp->offline = 1;
177                                         continue;
178                                 }
179                                 pp->mpp = mpp;
180                                 if (quick)
181                                         continue;
182                                 conf = get_multipath_config();
183                                 if (pathinfo(pp, conf, DI_ALL))
184                                         pp->state = PATH_UNCHECKED;
185                                 put_multipath_config(conf);
186                                 continue;
187                         }
188                         pp->mpp = mpp;
189                         if (quick)
190                                 continue;
191                         if (pp->state == PATH_UNCHECKED ||
192                             pp->state == PATH_WILD) {
193                                 conf = get_multipath_config();
194                                 if (pathinfo(pp, conf, DI_CHECKER))
195                                         pp->state = PATH_UNCHECKED;
196                                 put_multipath_config(conf);
197                         }
198
199                         if (pp->priority == PRIO_UNDEF) {
200                                 conf = get_multipath_config();
201                                 if (pathinfo(pp, conf, DI_PRIO))
202                                         pp->priority = PRIO_UNDEF;
203                                 put_multipath_config(conf);
204                         }
205                 }
206         }
207         return 0;
208 }
209
210 static int
211 get_dm_mpvec (enum mpath_cmds cmd, vector curmp, vector pathvec, char * refwwid)
212 {
213         int i;
214         struct multipath * mpp;
215         char params[PARAMS_SIZE], status[PARAMS_SIZE];
216
217         if (dm_get_maps(curmp))
218                 return 1;
219
220         vector_foreach_slot (curmp, mpp, i) {
221                 /*
222                  * discard out of scope maps
223                  */
224                 if (refwwid && strlen(refwwid) &&
225                     strncmp(mpp->wwid, refwwid, WWID_SIZE)) {
226                         condlog(3, "skip map %s: out of scope", mpp->alias);
227                         free_multipath(mpp, KEEP_PATHS);
228                         vector_del_slot(curmp, i);
229                         i--;
230                         continue;
231                 }
232
233                 if (cmd == CMD_VALID_PATH)
234                         continue;
235
236                 dm_get_map(mpp->alias, &mpp->size, params);
237                 condlog(3, "params = %s", params);
238                 dm_get_status(mpp->alias, status);
239                 condlog(3, "status = %s", status);
240
241                 disassemble_map(pathvec, params, mpp, 0);
242
243                 /*
244                  * disassemble_map() can add new paths to pathvec.
245                  * If not in "fast list mode", we need to fetch information
246                  * about them
247                  */
248                 update_paths(mpp, (cmd == CMD_LIST_SHORT));
249
250                 if (cmd == CMD_LIST_LONG)
251                         mpp->bestpg = select_path_group(mpp);
252
253                 disassemble_status(status, mpp);
254
255                 if (cmd == CMD_LIST_SHORT ||
256                     cmd == CMD_LIST_LONG) {
257                         struct config *conf = get_multipath_config();
258                         print_multipath_topology(mpp, conf->verbosity);
259                         put_multipath_config(conf);
260                 }
261
262                 if (cmd == CMD_CREATE)
263                         reinstate_paths(mpp);
264         }
265
266         if (cmd == CMD_LIST_SHORT || cmd == CMD_LIST_LONG) {
267                 struct config *conf = get_multipath_config();
268
269                 print_foreign_topology(conf->verbosity);
270                 put_multipath_config(conf);
271         }
272
273         return 0;
274 }
275
276 static int check_usable_paths(struct config *conf,
277                               const char *devpath, enum devtypes dev_type)
278 {
279         struct udev_device *ud = NULL;
280         struct multipath *mpp = NULL;
281         struct pathgroup *pg;
282         struct path *pp;
283         char *mapname;
284         vector pathvec = NULL;
285         char params[PARAMS_SIZE], status[PARAMS_SIZE];
286         dev_t devt;
287         int r = 1, i, j;
288
289         ud = get_udev_device(devpath, dev_type);
290         if (ud == NULL)
291                 return r;
292
293         devt = udev_device_get_devnum(ud);
294         if (!dm_is_dm_major(major(devt))) {
295                 condlog(1, "%s is not a dm device", devpath);
296                 goto out;
297         }
298
299         mapname = dm_mapname(major(devt), minor(devt));
300         if (mapname == NULL) {
301                 condlog(1, "dm device not found: %s", devpath);
302                 goto out;
303         }
304
305         if (!dm_is_mpath(mapname)) {
306                 condlog(1, "%s is not a multipath map", devpath);
307                 goto free;
308         }
309
310         /* pathvec is needed for disassemble_map */
311         pathvec = vector_alloc();
312         if (pathvec == NULL)
313                 goto free;
314
315         mpp = dm_get_multipath(mapname);
316         if (mpp == NULL)
317                 goto free;
318
319         dm_get_map(mpp->alias, &mpp->size, params);
320         dm_get_status(mpp->alias, status);
321         disassemble_map(pathvec, params, mpp, 0);
322         disassemble_status(status, mpp);
323
324         vector_foreach_slot (mpp->pg, pg, i) {
325                 vector_foreach_slot (pg->paths, pp, j) {
326                         pp->udev = get_udev_device(pp->dev_t, DEV_DEVT);
327                         if (pp->udev == NULL)
328                                 continue;
329                         if (pathinfo(pp, conf, DI_SYSFS|DI_NOIO) != PATHINFO_OK)
330                                 continue;
331
332                         if (pp->state == PATH_UP &&
333                             pp->dmstate == PSTATE_ACTIVE) {
334                                 condlog(3, "%s: path %s is usable",
335                                         devpath, pp->dev);
336                                 r = 0;
337                                 goto found;
338                         }
339                 }
340         }
341 found:
342         condlog(r == 0 ? 3 : 2, "%s:%s usable paths found",
343                 devpath, r == 0 ? "" : " no");
344 free:
345         FREE(mapname);
346         free_multipath(mpp, FREE_PATHS);
347         vector_free(pathvec);
348 out:
349         udev_device_unref(ud);
350         return r;
351 }
352
353 /*
354  * Return value:
355  *  -1: Retry
356  *   0: Success
357  *   1: Failure
358  */
359 static int
360 configure (struct config *conf, enum mpath_cmds cmd,
361            enum devtypes dev_type, char *devpath)
362 {
363         vector curmp = NULL;
364         vector pathvec = NULL;
365         struct vectors vecs;
366         int r = 1;
367         int di_flag = 0;
368         char * refwwid = NULL;
369         char * dev = NULL;
370
371         /*
372          * allocate core vectors to store paths and multipaths
373          */
374         curmp = vector_alloc();
375         pathvec = vector_alloc();
376
377         if (!curmp || !pathvec) {
378                 condlog(0, "can not allocate memory");
379                 goto out;
380         }
381         vecs.pathvec = pathvec;
382         vecs.mpvec = curmp;
383
384         dev = convert_dev(devpath, (dev_type == DEV_DEVNODE));
385
386         /*
387          * if we have a blacklisted device parameter, exit early
388          */
389         if (dev && (dev_type == DEV_DEVNODE ||
390                     dev_type == DEV_UEVENT) &&
391             cmd != CMD_REMOVE_WWID &&
392             (filter_devnode(conf->blist_devnode,
393                             conf->elist_devnode, dev) > 0)) {
394                 if (cmd == CMD_VALID_PATH)
395                         printf("%s is not a valid multipath device path\n",
396                                devpath);
397                 goto out;
398         }
399
400         /*
401          * scope limiting must be translated into a wwid
402          * failing the translation is fatal (by policy)
403          */
404         if (devpath) {
405                 int failed = get_refwwid(cmd, devpath, dev_type,
406                                          pathvec, &refwwid);
407                 if (!refwwid) {
408                         condlog(4, "%s: failed to get wwid", devpath);
409                         if (failed == 2 && cmd == CMD_VALID_PATH)
410                                 printf("%s is not a valid multipath device path\n", devpath);
411                         else
412                                 condlog(3, "scope is null");
413                         goto out;
414                 }
415                 if (cmd == CMD_REMOVE_WWID) {
416                         r = remove_wwid(refwwid);
417                         if (r == 0)
418                                 printf("wwid '%s' removed\n", refwwid);
419                         else if (r == 1) {
420                                 printf("wwid '%s' not in wwids file\n",
421                                         refwwid);
422                                 r = 0;
423                         }
424                         goto out;
425                 }
426                 if (cmd == CMD_ADD_WWID) {
427                         r = remember_wwid(refwwid);
428                         if (r == 0)
429                                 printf("wwid '%s' added\n", refwwid);
430                         else
431                                 printf("failed adding '%s' to wwids file\n",
432                                        refwwid);
433                         goto out;
434                 }
435                 condlog(3, "scope limited to %s", refwwid);
436                 /* If you are ignoring the wwids file and find_multipaths is
437                  * set, you need to actually check if there are two available
438                  * paths to determine if this path should be multipathed. To
439                  * do this, we put off the check until after discovering all
440                  * the paths */
441                 if (cmd == CMD_VALID_PATH &&
442                     (!conf->find_multipaths || !conf->ignore_wwids)) {
443                         if (conf->ignore_wwids ||
444                             check_wwids_file(refwwid, 0) == 0)
445                                 r = 0;
446
447                         printf("%s %s a valid multipath device path\n",
448                                devpath, r == 0 ? "is" : "is not");
449                         goto out;
450                 }
451         }
452
453         /*
454          * get a path list
455          */
456         if (devpath)
457                 di_flag = DI_WWID;
458
459         if (cmd == CMD_LIST_LONG)
460                 /* extended path info '-ll' */
461                 di_flag |= DI_SYSFS | DI_CHECKER | DI_SERIAL;
462         else if (cmd == CMD_LIST_SHORT)
463                 /* minimum path info '-l' */
464                 di_flag |= DI_SYSFS;
465         else
466                 /* maximum info */
467                 di_flag = DI_ALL;
468
469         if (path_discovery(pathvec, di_flag) < 0)
470                 goto out;
471
472         if (conf->verbosity > 2)
473                 print_all_paths(pathvec, 1);
474
475         get_path_layout(pathvec, 0);
476         foreign_path_layout();
477
478         if (get_dm_mpvec(cmd, curmp, pathvec, refwwid))
479                 goto out;
480
481         filter_pathvec(pathvec, refwwid);
482
483
484         if (cmd == CMD_VALID_PATH) {
485                 /* This only happens if find_multipaths and
486                  * ignore_wwids is set.
487                  * If there is currently a multipath device matching
488                  * the refwwid, or there is more than one path matching
489                  * the refwwid, then the path is valid */
490                 if (VECTOR_SIZE(curmp) != 0 || VECTOR_SIZE(pathvec) > 1)
491                         r = 0;
492                 printf("%s %s a valid multipath device path\n",
493                        devpath, r == 0 ? "is" : "is not");
494                 goto out;
495         }
496
497         if (cmd != CMD_CREATE && cmd != CMD_DRY_RUN) {
498                 r = 0;
499                 goto out;
500         }
501
502         /*
503          * core logic entry point
504          */
505         r = coalesce_paths(&vecs, NULL, refwwid,
506                            conf->force_reload, cmd);
507
508 out:
509         if (refwwid)
510                 FREE(refwwid);
511
512         free_multipathvec(curmp, KEEP_PATHS);
513         free_pathvec(pathvec, FREE_PATHS);
514
515         return r;
516 }
517
518 static int
519 dump_config (struct config *conf)
520 {
521         char * c, * tmp = NULL;
522         char * reply;
523         unsigned int maxlen = 256;
524         int again = 1;
525
526         reply = MALLOC(maxlen);
527
528         while (again) {
529                 if (!reply) {
530                         if (tmp)
531                                 free(tmp);
532                         return 1;
533                 }
534                 c = tmp = reply;
535                 c += snprint_defaults(conf, c, reply + maxlen - c);
536                 again = ((c - reply) == maxlen);
537                 if (again) {
538                         reply = REALLOC(reply, maxlen *= 2);
539                         continue;
540                 }
541                 c += snprint_blacklist(conf, c, reply + maxlen - c);
542                 again = ((c - reply) == maxlen);
543                 if (again) {
544                         reply = REALLOC(reply, maxlen *= 2);
545                         continue;
546                 }
547                 c += snprint_blacklist_except(conf, c, reply + maxlen - c);
548                 again = ((c - reply) == maxlen);
549                 if (again) {
550                         reply = REALLOC(reply, maxlen *= 2);
551                         continue;
552                 }
553                 c += snprint_hwtable(conf, c, reply + maxlen - c, conf->hwtable);
554                 again = ((c - reply) == maxlen);
555                 if (again) {
556                         reply = REALLOC(reply, maxlen *= 2);
557                         continue;
558                 }
559                 c += snprint_overrides(conf, c, reply + maxlen - c,
560                                        conf->overrides);
561                 again = ((c - reply) == maxlen);
562                 if (again) {
563                         reply = REALLOC(reply, maxlen *= 2);
564                         continue;
565                 }
566                 if (VECTOR_SIZE(conf->mptable) > 0) {
567                         c += snprint_mptable(conf, c, reply + maxlen - c,
568                                              conf->mptable);
569                         again = ((c - reply) == maxlen);
570                         if (again)
571                                 reply = REALLOC(reply, maxlen *= 2);
572                 }
573         }
574
575         printf("%s", reply);
576         FREE(reply);
577         return 0;
578 }
579
580 static int
581 get_dev_type(char *dev) {
582         struct stat buf;
583         int i;
584
585         if (stat(dev, &buf) == 0 && S_ISBLK(buf.st_mode)) {
586                 if (dm_is_dm_major(major(buf.st_rdev)))
587                         return DEV_DEVMAP;
588                 return DEV_DEVNODE;
589         }
590         else if (sscanf(dev, "%d:%d", &i, &i) == 2)
591                 return DEV_DEVT;
592         else if (valid_alias(dev))
593                 return DEV_DEVMAP;
594         return DEV_NONE;
595 }
596
597 /*
598  * Some multipath commands are dangerous to run while multipathd is running.
599  * For example, "multipath -r" may apply a modified configuration to the kernel,
600  * while multipathd is still using the old configuration, leading to
601  * inconsistent state.
602  *
603  * It is safer to use equivalent multipathd client commands instead.
604  */
605 int delegate_to_multipathd(enum mpath_cmds cmd, const char *dev,
606                            enum devtypes dev_type, const struct config *conf)
607 {
608         int fd;
609         char command[1024], *p, *reply = NULL;
610         int n, r = 0;
611
612         fd = mpath_connect();
613         if (fd == -1)
614                 return 0;
615
616         p = command;
617         *p = '\0';
618         n = sizeof(command);
619
620         if (cmd == CMD_CREATE && conf->force_reload == FORCE_RELOAD_YES) {
621                 p += snprintf(p, n, "reconfigure");
622         }
623         /* Add other translations here */
624
625         if (strlen(command) == 0)
626                 /* No command found, no need to delegate */
627                 return 0;
628         else if (p >= command + sizeof(command)) {
629                 condlog(0, "internal error - command buffer overflow");
630                 r = -1;
631                 goto out;
632         }
633
634         condlog(3, "delegating command to multipathd");
635         r = mpath_process_cmd(fd, command, &reply, conf->uxsock_timeout);
636
637         if (r == -1) {
638                 condlog(1, "error in multipath command %s: %s",
639                         command, strerror(errno));
640                 goto out;
641         }
642
643         if (reply != NULL && *reply != '\0' && strcmp(reply, "ok\n"))
644                 printf("%s", reply);
645         r = 1;
646
647 out:
648         FREE(reply);
649         close(fd);
650         if (r < 0)
651                 exit(1);
652         return r;
653 }
654
655 int
656 main (int argc, char *argv[])
657 {
658         int arg;
659         extern char *optarg;
660         extern int optind;
661         int r = 1;
662         enum mpath_cmds cmd = CMD_CREATE;
663         enum devtypes dev_type = DEV_NONE;
664         char *dev = NULL;
665         struct config *conf;
666         int retries = -1;
667
668         udev = udev_new();
669         logsink = 0;
670         conf = load_config(DEFAULT_CONFIGFILE);
671         if (!conf)
672                 exit(1);
673         multipath_conf = conf;
674         conf->retrigger_tries = 0;
675         while ((arg = getopt(argc, argv, ":adcChl::FfM:v:p:b:BrR:itquUwW")) != EOF ) {
676                 switch(arg) {
677                 case 1: printf("optarg : %s\n",optarg);
678                         break;
679                 case 'v':
680                         if (sizeof(optarg) > sizeof(char *) ||
681                             !isdigit(optarg[0])) {
682                                 usage (argv[0]);
683                                 exit(1);
684                         }
685
686                         conf->verbosity = atoi(optarg);
687                         break;
688                 case 'b':
689                         conf->bindings_file = strdup(optarg);
690                         break;
691                 case 'B':
692                         conf->bindings_read_only = 1;
693                         break;
694                 case 'q':
695                         conf->allow_queueing = 1;
696                         break;
697                 case 'c':
698                         cmd = CMD_VALID_PATH;
699                         break;
700                 case 'C':
701                         cmd = CMD_USABLE_PATHS;
702                         break;
703                 case 'd':
704                         if (cmd == CMD_CREATE)
705                                 cmd = CMD_DRY_RUN;
706                         break;
707                 case 'f':
708                         conf->remove = FLUSH_ONE;
709                         break;
710                 case 'F':
711                         conf->remove = FLUSH_ALL;
712                         break;
713                 case 'l':
714                         if (optarg && !strncmp(optarg, "l", 1))
715                                 cmd = CMD_LIST_LONG;
716                         else
717                                 cmd = CMD_LIST_SHORT;
718
719                         break;
720                 case 'M':
721 #if _DEBUG_
722                         debug = atoi(optarg);
723 #endif
724                         break;
725                 case 'p':
726                         conf->pgpolicy_flag = get_pgpolicy_id(optarg);
727                         if (conf->pgpolicy_flag == IOPOLICY_UNDEF) {
728                                 printf("'%s' is not a valid policy\n", optarg);
729                                 usage(argv[0]);
730                                 exit(1);
731                         }
732                         break;
733                 case 'r':
734                         conf->force_reload = FORCE_RELOAD_YES;
735                         break;
736                 case 'i':
737                         conf->ignore_wwids = 1;
738                         break;
739                 case 't':
740                         r = dump_config(conf);
741                         goto out_free_config;
742                 case 'h':
743                         usage(argv[0]);
744                         exit(0);
745                 case 'u':
746                         cmd = CMD_VALID_PATH;
747                         dev_type = DEV_UEVENT;
748                         break;
749                 case 'U':
750                         cmd = CMD_USABLE_PATHS;
751                         dev_type = DEV_UEVENT;
752                         break;
753                 case 'w':
754                         cmd = CMD_REMOVE_WWID;
755                         break;
756                 case 'W':
757                         cmd = CMD_RESET_WWIDS;
758                         break;
759                 case 'a':
760                         cmd = CMD_ADD_WWID;
761                         break;
762                 case 'R':
763                         retries = atoi(optarg);
764                         break;
765                 case ':':
766                         fprintf(stderr, "Missing option argument\n");
767                         usage(argv[0]);
768                         exit(1);
769                 case '?':
770                         fprintf(stderr, "Unknown switch: %s\n", optarg);
771                         usage(argv[0]);
772                         exit(1);
773                 default:
774                         usage(argv[0]);
775                         exit(1);
776                 }
777         }
778
779         /*
780          * FIXME: new device detection with find_multipaths currently
781          * doesn't work reliably.
782          */
783         if (cmd ==  CMD_VALID_PATH &&
784             conf->find_multipaths && conf->ignore_wwids) {
785                 condlog(2, "ignoring -i flag because find_multipath is set in multipath.conf");
786                 conf->ignore_wwids = 0;
787         }
788
789         if (getuid() != 0) {
790                 fprintf(stderr, "need to be root\n");
791                 exit(1);
792         }
793
794         if (optind < argc) {
795                 dev = MALLOC(FILE_NAME_SIZE);
796
797                 if (!dev)
798                         goto out;
799
800                 strncpy(dev, argv[optind], FILE_NAME_SIZE);
801                 if (dev_type != DEV_UEVENT)
802                         dev_type = get_dev_type(dev);
803                 if (dev_type == DEV_NONE) {
804                         condlog(0, "'%s' is not a valid argument\n", dev);
805                         goto out;
806                 }
807         }
808         if (dev_type == DEV_UEVENT) {
809                 openlog("multipath", 0, LOG_DAEMON);
810                 setlogmask(LOG_UPTO(conf->verbosity + 3));
811                 logsink = 1;
812         }
813
814         if (conf->max_fds) {
815                 struct rlimit fd_limit;
816
817                 fd_limit.rlim_cur = conf->max_fds;
818                 fd_limit.rlim_max = conf->max_fds;
819                 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
820                         condlog(0, "can't set open fds limit to %d : %s",
821                                 conf->max_fds, strerror(errno));
822         }
823
824         libmp_udev_set_sync_support(1);
825
826         if (init_checkers(conf->multipath_dir)) {
827                 condlog(0, "failed to initialize checkers");
828                 goto out;
829         }
830         if (init_prio(conf->multipath_dir)) {
831                 condlog(0, "failed to initialize prioritizers");
832                 goto out;
833         }
834         /* Failing here is non-fatal */
835         init_foreign(conf->multipath_dir);
836         if (cmd == CMD_USABLE_PATHS) {
837                 r = check_usable_paths(conf, dev, dev_type);
838                 goto out;
839         }
840         if (cmd == CMD_VALID_PATH &&
841             (!dev || dev_type == DEV_DEVMAP)) {
842                 condlog(0, "the -c option requires a path to check");
843                 goto out;
844         }
845         if (cmd == CMD_VALID_PATH &&
846             dev_type == DEV_UEVENT) {
847                 int fd;
848
849                 fd = mpath_connect();
850                 if (fd == -1) {
851                         condlog(3, "%s: daemon is not running", dev);
852                         if (!systemd_service_enabled(dev)) {
853                                 printf("%s is not a valid "
854                                        "multipath device path\n", dev);
855                                 goto out;
856                         }
857                 } else
858                         mpath_disconnect(fd);
859         }
860
861         if (cmd == CMD_REMOVE_WWID && !dev) {
862                 condlog(0, "the -w option requires a device");
863                 goto out;
864         }
865
866         if (delegate_to_multipathd(cmd, dev, dev_type, conf))
867                 exit(0);
868
869         if (cmd == CMD_RESET_WWIDS) {
870                 struct multipath * mpp;
871                 int i;
872                 vector curmp;
873
874                 curmp = vector_alloc();
875                 if (!curmp) {
876                         condlog(0, "can't allocate memory for mp list");
877                         goto out;
878                 }
879                 if (dm_get_maps(curmp) == 0)
880                         r = replace_wwids(curmp);
881                 if (r == 0)
882                         printf("successfully reset wwids\n");
883                 vector_foreach_slot_backwards(curmp, mpp, i) {
884                         vector_del_slot(curmp, i);
885                         free_multipath(mpp, KEEP_PATHS);
886                 }
887                 vector_free(curmp);
888                 goto out;
889         }
890         if (retries < 0)
891                 retries = conf->remove_retries;
892         if (conf->remove == FLUSH_ONE) {
893                 if (dev_type == DEV_DEVMAP) {
894                         r = dm_suspend_and_flush_map(dev, retries);
895                 } else
896                         condlog(0, "must provide a map name to remove");
897
898                 goto out;
899         }
900         else if (conf->remove == FLUSH_ALL) {
901                 r = dm_flush_maps(retries);
902                 goto out;
903         }
904         while ((r = configure(conf, cmd, dev_type, dev)) < 0)
905                 condlog(3, "restart multipath configuration process");
906
907 out:
908         dm_lib_release();
909         dm_lib_exit();
910
911         cleanup_foreign();
912         cleanup_prio();
913         cleanup_checkers();
914
915         if (dev_type == DEV_UEVENT)
916                 closelog();
917
918 out_free_config:
919         /*
920          * Freeing config must be done after dm_lib_exit(), because
921          * the logging function (dm_write_log()), which is called there,
922          * references the config.
923          */
924         free_config(conf);
925         conf = NULL;
926         udev_unref(udev);
927         if (dev)
928                 FREE(dev);
929 #ifdef _DEBUG_
930         dbg_free_final(NULL);
931 #endif
932         return r;
933 }