59a72ed043d7573b3f8b28e6e017172d24dcefd8
[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 static int print_cmd_valid(int k, const vector pathvec,
354                            struct config *conf)
355 {
356         static const int vals[] = { 1, 0 };
357
358         if (k < 0 || k >= sizeof(vals))
359                 return 1;
360
361         printf("DM_MULTIPATH_DEVICE_PATH=\"%d\"\n", vals[k]);
362         return k == 1;
363 }
364
365 /*
366  * Return value:
367  *  -1: Retry
368  *   0: Success
369  *   1: Failure
370  */
371 static int
372 configure (struct config *conf, enum mpath_cmds cmd,
373            enum devtypes dev_type, char *devpath)
374 {
375         vector curmp = NULL;
376         vector pathvec = NULL;
377         struct vectors vecs;
378         int r = 1;
379         int di_flag = 0;
380         char * refwwid = NULL;
381         char * dev = NULL;
382
383         /*
384          * allocate core vectors to store paths and multipaths
385          */
386         curmp = vector_alloc();
387         pathvec = vector_alloc();
388
389         if (!curmp || !pathvec) {
390                 condlog(0, "can not allocate memory");
391                 goto out;
392         }
393         vecs.pathvec = pathvec;
394         vecs.mpvec = curmp;
395
396         dev = convert_dev(devpath, (dev_type == DEV_DEVNODE));
397
398         /*
399          * if we have a blacklisted device parameter, exit early
400          */
401         if (dev && (dev_type == DEV_DEVNODE ||
402                     dev_type == DEV_UEVENT) &&
403             cmd != CMD_REMOVE_WWID &&
404             (filter_devnode(conf->blist_devnode,
405                             conf->elist_devnode, dev) > 0)) {
406                 goto print_valid;
407         }
408
409         /*
410          * scope limiting must be translated into a wwid
411          * failing the translation is fatal (by policy)
412          */
413         if (devpath) {
414                 int failed = get_refwwid(cmd, devpath, dev_type,
415                                          pathvec, &refwwid);
416                 if (!refwwid) {
417                         condlog(4, "%s: failed to get wwid", devpath);
418                         if (failed == 2 && cmd == CMD_VALID_PATH)
419                                 goto print_valid;
420                         else
421                                 condlog(3, "scope is null");
422                         goto out;
423                 }
424                 if (cmd == CMD_REMOVE_WWID) {
425                         r = remove_wwid(refwwid);
426                         if (r == 0)
427                                 printf("wwid '%s' removed\n", refwwid);
428                         else if (r == 1) {
429                                 printf("wwid '%s' not in wwids file\n",
430                                         refwwid);
431                                 r = 0;
432                         }
433                         goto out;
434                 }
435                 if (cmd == CMD_ADD_WWID) {
436                         r = remember_wwid(refwwid);
437                         if (r >= 0)
438                                 printf("wwid '%s' added\n", refwwid);
439                         else
440                                 printf("failed adding '%s' to wwids file\n",
441                                        refwwid);
442                         goto out;
443                 }
444                 condlog(3, "scope limited to %s", refwwid);
445                 /* If you are ignoring the wwids file and find_multipaths is
446                  * set, you need to actually check if there are two available
447                  * paths to determine if this path should be multipathed. To
448                  * do this, we put off the check until after discovering all
449                  * the paths.
450                  * Paths listed in the wwids file are always considered valid.
451                  */
452                 if (cmd == CMD_VALID_PATH) {
453                         if ((!find_multipaths_on(conf) && ignore_wwids_on(conf))
454                             || check_wwids_file(refwwid, 0) == 0)
455                                 r = 0;
456                         if (r == 0 ||
457                             !find_multipaths_on(conf) ||
458                             !ignore_wwids_on(conf)) {
459                                 goto print_valid;
460                         }
461                 }
462         }
463
464         /*
465          * get a path list
466          */
467         if (devpath)
468                 di_flag = DI_WWID;
469
470         if (cmd == CMD_LIST_LONG)
471                 /* extended path info '-ll' */
472                 di_flag |= DI_SYSFS | DI_CHECKER | DI_SERIAL;
473         else if (cmd == CMD_LIST_SHORT)
474                 /* minimum path info '-l' */
475                 di_flag |= DI_SYSFS;
476         else
477                 /* maximum info */
478                 di_flag = DI_ALL;
479
480         if (path_discovery(pathvec, di_flag) < 0)
481                 goto out;
482
483         if (conf->verbosity > 2)
484                 print_all_paths(pathvec, 1);
485
486         get_path_layout(pathvec, 0);
487         foreign_path_layout();
488
489         if (get_dm_mpvec(cmd, curmp, pathvec, refwwid))
490                 goto out;
491
492         filter_pathvec(pathvec, refwwid);
493
494
495         if (cmd == CMD_VALID_PATH) {
496                 /* This only happens if find_multipaths and
497                  * ignore_wwids is set.
498                  * If there is currently a multipath device matching
499                  * the refwwid, or there is more than one path matching
500                  * the refwwid, then the path is valid */
501                 if (VECTOR_SIZE(curmp) != 0 || VECTOR_SIZE(pathvec) > 1)
502                         r = 0;
503                 goto print_valid;
504         }
505
506         if (cmd != CMD_CREATE && cmd != CMD_DRY_RUN) {
507                 r = 0;
508                 goto out;
509         }
510
511         /*
512          * core logic entry point
513          */
514         r = coalesce_paths(&vecs, NULL, refwwid,
515                            conf->force_reload, cmd);
516
517 print_valid:
518         if (cmd == CMD_VALID_PATH)
519                 r = print_cmd_valid(r, pathvec, conf);
520
521 out:
522         if (refwwid)
523                 FREE(refwwid);
524
525         free_multipathvec(curmp, KEEP_PATHS);
526         free_pathvec(pathvec, FREE_PATHS);
527
528         return r;
529 }
530
531 static int
532 dump_config (struct config *conf)
533 {
534         char * c, * tmp = NULL;
535         char * reply;
536         unsigned int maxlen = 256;
537         int again = 1;
538
539         reply = MALLOC(maxlen);
540
541         while (again) {
542                 if (!reply) {
543                         if (tmp)
544                                 free(tmp);
545                         return 1;
546                 }
547                 c = tmp = reply;
548                 c += snprint_defaults(conf, c, reply + maxlen - c);
549                 again = ((c - reply) == maxlen);
550                 if (again) {
551                         reply = REALLOC(reply, maxlen *= 2);
552                         continue;
553                 }
554                 c += snprint_blacklist(conf, c, reply + maxlen - c);
555                 again = ((c - reply) == maxlen);
556                 if (again) {
557                         reply = REALLOC(reply, maxlen *= 2);
558                         continue;
559                 }
560                 c += snprint_blacklist_except(conf, c, reply + maxlen - c);
561                 again = ((c - reply) == maxlen);
562                 if (again) {
563                         reply = REALLOC(reply, maxlen *= 2);
564                         continue;
565                 }
566                 c += snprint_hwtable(conf, c, reply + maxlen - c, conf->hwtable);
567                 again = ((c - reply) == maxlen);
568                 if (again) {
569                         reply = REALLOC(reply, maxlen *= 2);
570                         continue;
571                 }
572                 c += snprint_overrides(conf, c, reply + maxlen - c,
573                                        conf->overrides);
574                 again = ((c - reply) == maxlen);
575                 if (again) {
576                         reply = REALLOC(reply, maxlen *= 2);
577                         continue;
578                 }
579                 if (VECTOR_SIZE(conf->mptable) > 0) {
580                         c += snprint_mptable(conf, c, reply + maxlen - c,
581                                              conf->mptable);
582                         again = ((c - reply) == maxlen);
583                         if (again)
584                                 reply = REALLOC(reply, maxlen *= 2);
585                 }
586         }
587
588         printf("%s", reply);
589         FREE(reply);
590         return 0;
591 }
592
593 static int
594 get_dev_type(char *dev) {
595         struct stat buf;
596         int i;
597
598         if (stat(dev, &buf) == 0 && S_ISBLK(buf.st_mode)) {
599                 if (dm_is_dm_major(major(buf.st_rdev)))
600                         return DEV_DEVMAP;
601                 return DEV_DEVNODE;
602         }
603         else if (sscanf(dev, "%d:%d", &i, &i) == 2)
604                 return DEV_DEVT;
605         else if (valid_alias(dev))
606                 return DEV_DEVMAP;
607         return DEV_NONE;
608 }
609
610 /*
611  * Some multipath commands are dangerous to run while multipathd is running.
612  * For example, "multipath -r" may apply a modified configuration to the kernel,
613  * while multipathd is still using the old configuration, leading to
614  * inconsistent state.
615  *
616  * It is safer to use equivalent multipathd client commands instead.
617  */
618 int delegate_to_multipathd(enum mpath_cmds cmd, const char *dev,
619                            enum devtypes dev_type, const struct config *conf)
620 {
621         int fd;
622         char command[1024], *p, *reply = NULL;
623         int n, r = 0;
624
625         fd = mpath_connect();
626         if (fd == -1)
627                 return 0;
628
629         p = command;
630         *p = '\0';
631         n = sizeof(command);
632
633         if (cmd == CMD_CREATE && conf->force_reload == FORCE_RELOAD_YES) {
634                 p += snprintf(p, n, "reconfigure");
635         }
636         /* Add other translations here */
637
638         if (strlen(command) == 0)
639                 /* No command found, no need to delegate */
640                 return 0;
641         else if (p >= command + sizeof(command)) {
642                 condlog(0, "internal error - command buffer overflow");
643                 r = -1;
644                 goto out;
645         }
646
647         condlog(3, "delegating command to multipathd");
648         r = mpath_process_cmd(fd, command, &reply, conf->uxsock_timeout);
649
650         if (r == -1) {
651                 condlog(1, "error in multipath command %s: %s",
652                         command, strerror(errno));
653                 goto out;
654         }
655
656         if (reply != NULL && *reply != '\0' && strcmp(reply, "ok\n"))
657                 printf("%s", reply);
658         r = 1;
659
660 out:
661         FREE(reply);
662         close(fd);
663         if (r < 0)
664                 exit(1);
665         return r;
666 }
667
668 int
669 main (int argc, char *argv[])
670 {
671         int arg;
672         extern char *optarg;
673         extern int optind;
674         int r = 1;
675         enum mpath_cmds cmd = CMD_CREATE;
676         enum devtypes dev_type = DEV_NONE;
677         char *dev = NULL;
678         struct config *conf;
679         int retries = -1;
680
681         udev = udev_new();
682         logsink = 0;
683         conf = load_config(DEFAULT_CONFIGFILE);
684         if (!conf)
685                 exit(1);
686         multipath_conf = conf;
687         conf->retrigger_tries = 0;
688         while ((arg = getopt(argc, argv, ":adcChl::FfM:v:p:b:BrR:itquUwW")) != EOF ) {
689                 switch(arg) {
690                 case 1: printf("optarg : %s\n",optarg);
691                         break;
692                 case 'v':
693                         if (sizeof(optarg) > sizeof(char *) ||
694                             !isdigit(optarg[0])) {
695                                 usage (argv[0]);
696                                 exit(1);
697                         }
698
699                         conf->verbosity = atoi(optarg);
700                         break;
701                 case 'b':
702                         conf->bindings_file = strdup(optarg);
703                         break;
704                 case 'B':
705                         conf->bindings_read_only = 1;
706                         break;
707                 case 'q':
708                         conf->allow_queueing = 1;
709                         break;
710                 case 'c':
711                         cmd = CMD_VALID_PATH;
712                         break;
713                 case 'C':
714                         cmd = CMD_USABLE_PATHS;
715                         break;
716                 case 'd':
717                         if (cmd == CMD_CREATE)
718                                 cmd = CMD_DRY_RUN;
719                         break;
720                 case 'f':
721                         conf->remove = FLUSH_ONE;
722                         break;
723                 case 'F':
724                         conf->remove = FLUSH_ALL;
725                         break;
726                 case 'l':
727                         if (optarg && !strncmp(optarg, "l", 1))
728                                 cmd = CMD_LIST_LONG;
729                         else
730                                 cmd = CMD_LIST_SHORT;
731
732                         break;
733                 case 'M':
734 #if _DEBUG_
735                         debug = atoi(optarg);
736 #endif
737                         break;
738                 case 'p':
739                         conf->pgpolicy_flag = get_pgpolicy_id(optarg);
740                         if (conf->pgpolicy_flag == IOPOLICY_UNDEF) {
741                                 printf("'%s' is not a valid policy\n", optarg);
742                                 usage(argv[0]);
743                                 exit(1);
744                         }
745                         break;
746                 case 'r':
747                         conf->force_reload = FORCE_RELOAD_YES;
748                         break;
749                 case 'i':
750                         conf->find_multipaths |= _FIND_MULTIPATHS_I;
751                         break;
752                 case 't':
753                         r = dump_config(conf);
754                         goto out_free_config;
755                 case 'h':
756                         usage(argv[0]);
757                         exit(0);
758                 case 'u':
759                         cmd = CMD_VALID_PATH;
760                         dev_type = DEV_UEVENT;
761                         break;
762                 case 'U':
763                         cmd = CMD_USABLE_PATHS;
764                         dev_type = DEV_UEVENT;
765                         break;
766                 case 'w':
767                         cmd = CMD_REMOVE_WWID;
768                         break;
769                 case 'W':
770                         cmd = CMD_RESET_WWIDS;
771                         break;
772                 case 'a':
773                         cmd = CMD_ADD_WWID;
774                         break;
775                 case 'R':
776                         retries = atoi(optarg);
777                         break;
778                 case ':':
779                         fprintf(stderr, "Missing option argument\n");
780                         usage(argv[0]);
781                         exit(1);
782                 case '?':
783                         fprintf(stderr, "Unknown switch: %s\n", optarg);
784                         usage(argv[0]);
785                         exit(1);
786                 default:
787                         usage(argv[0]);
788                         exit(1);
789                 }
790         }
791
792         if (getuid() != 0) {
793                 fprintf(stderr, "need to be root\n");
794                 exit(1);
795         }
796
797         if (optind < argc) {
798                 dev = MALLOC(FILE_NAME_SIZE);
799
800                 if (!dev)
801                         goto out;
802
803                 strncpy(dev, argv[optind], FILE_NAME_SIZE);
804                 if (dev_type != DEV_UEVENT)
805                         dev_type = get_dev_type(dev);
806                 if (dev_type == DEV_NONE) {
807                         condlog(0, "'%s' is not a valid argument\n", dev);
808                         goto out;
809                 }
810         }
811         if (dev_type == DEV_UEVENT) {
812                 openlog("multipath", 0, LOG_DAEMON);
813                 setlogmask(LOG_UPTO(conf->verbosity + 3));
814                 logsink = 1;
815         }
816
817         if (conf->max_fds) {
818                 struct rlimit fd_limit;
819
820                 fd_limit.rlim_cur = conf->max_fds;
821                 fd_limit.rlim_max = conf->max_fds;
822                 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
823                         condlog(0, "can't set open fds limit to %d : %s",
824                                 conf->max_fds, strerror(errno));
825         }
826
827         libmp_udev_set_sync_support(1);
828
829         if (init_checkers(conf->multipath_dir)) {
830                 condlog(0, "failed to initialize checkers");
831                 goto out;
832         }
833         if (init_prio(conf->multipath_dir)) {
834                 condlog(0, "failed to initialize prioritizers");
835                 goto out;
836         }
837         /* Failing here is non-fatal */
838         init_foreign(conf->multipath_dir);
839         if (cmd == CMD_USABLE_PATHS) {
840                 r = check_usable_paths(conf, dev, dev_type);
841                 goto out;
842         }
843         if (cmd == CMD_VALID_PATH &&
844             (!dev || dev_type == DEV_DEVMAP)) {
845                 condlog(0, "the -c option requires a path to check");
846                 goto out;
847         }
848         if (cmd == CMD_VALID_PATH &&
849             dev_type == DEV_UEVENT) {
850                 int fd;
851
852                 fd = mpath_connect();
853                 if (fd == -1) {
854                         condlog(3, "%s: daemon is not running", dev);
855                         if (!systemd_service_enabled(dev)) {
856                                 r = print_cmd_valid(1, NULL, conf);
857                                 goto out;
858                         }
859                 } else
860                         mpath_disconnect(fd);
861         }
862
863         if (cmd == CMD_REMOVE_WWID && !dev) {
864                 condlog(0, "the -w option requires a device");
865                 goto out;
866         }
867
868         if (delegate_to_multipathd(cmd, dev, dev_type, conf))
869                 exit(0);
870
871         if (cmd == CMD_RESET_WWIDS) {
872                 struct multipath * mpp;
873                 int i;
874                 vector curmp;
875
876                 curmp = vector_alloc();
877                 if (!curmp) {
878                         condlog(0, "can't allocate memory for mp list");
879                         goto out;
880                 }
881                 if (dm_get_maps(curmp) == 0)
882                         r = replace_wwids(curmp);
883                 if (r == 0)
884                         printf("successfully reset wwids\n");
885                 vector_foreach_slot_backwards(curmp, mpp, i) {
886                         vector_del_slot(curmp, i);
887                         free_multipath(mpp, KEEP_PATHS);
888                 }
889                 vector_free(curmp);
890                 goto out;
891         }
892         if (retries < 0)
893                 retries = conf->remove_retries;
894         if (conf->remove == FLUSH_ONE) {
895                 if (dev_type == DEV_DEVMAP) {
896                         r = dm_suspend_and_flush_map(dev, retries);
897                 } else
898                         condlog(0, "must provide a map name to remove");
899
900                 goto out;
901         }
902         else if (conf->remove == FLUSH_ALL) {
903                 r = dm_flush_maps(retries);
904                 goto out;
905         }
906         while ((r = configure(conf, cmd, dev_type, dev)) < 0)
907                 condlog(3, "restart multipath configuration process");
908
909 out:
910         dm_lib_release();
911         dm_lib_exit();
912
913         cleanup_foreign();
914         cleanup_prio();
915         cleanup_checkers();
916
917         if (dev_type == DEV_UEVENT)
918                 closelog();
919
920 out_free_config:
921         /*
922          * Freeing config must be done after dm_lib_exit(), because
923          * the logging function (dm_write_log()), which is called there,
924          * references the config.
925          */
926         free_config(conf);
927         conf = NULL;
928         udev_unref(udev);
929         if (dev)
930                 FREE(dev);
931 #ifdef _DEBUG_
932         dbg_free_final(NULL);
933 #endif
934         return r;
935 }