Imported Upstream version 0.8.9
[platform/upstream/multipath-tools.git] / libmpathpersist / mpath_updatepr.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdarg.h>
6 #include <fcntl.h>
7 #include <sys/ioctl.h>
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <sys/un.h>
11 #include <poll.h>
12 #include <errno.h>
13 #include <libudev.h>
14 #include <mpath_persist.h>
15 #include "debug.h"
16 #include "mpath_cmd.h"
17 #include "uxsock.h"
18 #include "mpathpr.h"
19
20
21 static int do_update_pr(char *alias, char *arg)
22 {
23         int fd;
24         char str[256];
25         char *reply;
26         int ret = 0;
27
28         fd = mpath_connect();
29         if (fd == -1) {
30                 condlog (0, "ux socket connect error");
31                 return -1;
32         }
33
34         snprintf(str,sizeof(str),"map %s %s", alias, arg);
35         condlog (2, "%s: pr message=%s", alias, str);
36         if (send_packet(fd, str) != 0) {
37                 condlog(2, "%s: message=%s send error=%d", alias, str, errno);
38                 mpath_disconnect(fd);
39                 return -1;
40         }
41         ret = recv_packet(fd, &reply, DEFAULT_REPLY_TIMEOUT);
42         if (ret < 0) {
43                 condlog(2, "%s: message=%s recv error=%d", alias, str, errno);
44                 ret = -1;
45         } else {
46                 condlog (2, "%s: message=%s reply=%s", alias, str, reply);
47                 if (reply && strncmp(reply,"ok", 2) == 0)
48                         ret = 0;
49                 else
50                         ret = -1;
51         }
52
53         free(reply);
54         mpath_disconnect(fd);
55         return ret;
56 }
57
58 int update_prflag(char *mapname, int set) {
59         return do_update_pr(mapname, (set)? "setprstatus" : "unsetprstatus");
60 }
61
62 int update_prkey_flags(char *mapname, uint64_t prkey, uint8_t sa_flags) {
63         char str[256];
64         char *flagstr = "";
65
66         if (sa_flags & MPATH_F_APTPL_MASK)
67                 flagstr = ":aptpl";
68         if (prkey)
69                 sprintf(str, "setprkey key %" PRIx64 "%s", prkey, flagstr);
70         else
71                 sprintf(str, "unsetprkey");
72         return do_update_pr(mapname, str);
73 }