1 /*****************************************************************************
2 Copyright (c) 2006 EMC Corporation.
3 Copyright (c) 2011 Factor-SPE
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2 of the License, or (at your option)
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc., 59
17 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 The full GNU General Public License is included in this distribution in the
22 Authors: Srinivas Aji <Aji_Srinivas@emc.com>
23 Authors: Vitalii Demianets <vitas@nppfactor.kiev.ua>
25 ******************************************************************************/
30 #include "ctl_socket_client.h"
31 #include "epoll_loop.h"
34 static int server_socket(void)
36 struct sockaddr_un sa
;
39 TST(strlen(MSTP_SERVER_SOCK_NAME
) < sizeof(sa
.sun_path
), -1);
41 if(0 > (s
= socket(PF_UNIX
, SOCK_DGRAM
, 0)))
43 ERROR("Couldn't open unix socket: %m");
47 set_socket_address(&sa
, MSTP_SERVER_SOCK_NAME
);
49 if(0 != bind(s
, (struct sockaddr
*)&sa
, sizeof(sa
)))
51 ERROR("Couldn't bind socket: %m");
59 static int handle_message(int cmd
, void *inbuf
, int lin
,
60 void *outbuf
, int lout
)
64 SERVER_MESSAGE_CASE(get_cist_bridge_status
);
65 SERVER_MESSAGE_CASE(get_msti_bridge_status
);
66 SERVER_MESSAGE_CASE(set_cist_bridge_config
);
67 SERVER_MESSAGE_CASE(set_msti_bridge_config
);
68 SERVER_MESSAGE_CASE(get_cist_port_status
);
69 SERVER_MESSAGE_CASE(get_msti_port_status
);
70 SERVER_MESSAGE_CASE(set_cist_port_config
);
71 SERVER_MESSAGE_CASE(set_msti_port_config
);
72 SERVER_MESSAGE_CASE(port_mcheck
);
73 SERVER_MESSAGE_CASE(set_debug_level
);
74 SERVER_MESSAGE_CASE(get_mstilist
);
75 SERVER_MESSAGE_CASE(create_msti
);
76 SERVER_MESSAGE_CASE(delete_msti
);
77 SERVER_MESSAGE_CASE(get_mstconfid
);
78 SERVER_MESSAGE_CASE(set_mstconfid
);
79 SERVER_MESSAGE_CASE(get_vids2fids
);
80 SERVER_MESSAGE_CASE(get_fids2mstids
);
81 SERVER_MESSAGE_CASE(set_vid2fid
);
82 SERVER_MESSAGE_CASE(set_fid2mstid
);
83 SERVER_MESSAGE_CASE(set_vids2fids
);
84 SERVER_MESSAGE_CASE(set_fids2mstids
);
87 ERROR("CTL: Unknown command %d", cmd
);
92 int ctl_in_handler
= 0;
93 static unsigned char msg_logbuf
[LOG_STRING_LEN
];
94 static unsigned int msg_log_offset
;
95 void _ctl_err_log(char *fmt
, ...)
97 if((sizeof(msg_logbuf
) - 1) <= msg_log_offset
)
102 r
= vsnprintf((char *)msg_logbuf
+ msg_log_offset
,
103 sizeof(msg_logbuf
) - msg_log_offset
,
107 if(sizeof(msg_logbuf
) <= msg_log_offset
)
109 msg_log_offset
= sizeof(msg_logbuf
) - 1;
110 msg_logbuf
[sizeof(msg_logbuf
) - 1] = 0;
114 #define MSG_BUF_LEN 10000
115 static unsigned char msg_inbuf
[MSG_BUF_LEN
];
116 static unsigned char msg_outbuf
[MSG_BUF_LEN
];
118 static void ctl_rcv_handler(uint32_t events
, struct epoll_event_handler
*p
)
120 struct ctl_msg_hdr mhdr
;
122 struct sockaddr_un sa
;
127 msg
.msg_namelen
= sizeof(sa
);
130 msg
.msg_control
= NULL
;
131 msg
.msg_controllen
= 0;
132 iov
[0].iov_base
= &mhdr
;
133 iov
[0].iov_len
= sizeof(mhdr
);
134 iov
[1].iov_base
= msg_inbuf
;
135 iov
[1].iov_len
= MSG_BUF_LEN
;
136 iov
[2].iov_base
= NULL
;
138 l
= recvmsg(p
->fd
, &msg
, MSG_NOSIGNAL
| MSG_DONTWAIT
);
140 if((0 != msg
.msg_flags
) || (sizeof(mhdr
) > l
)
141 || (l
!= sizeof(mhdr
) + mhdr
.lin
)
142 || (MSG_BUF_LEN
< mhdr
.lout
)
146 ERROR("CTL: Unexpected message. Ignoring");
153 mhdr
.res
= handle_message(mhdr
.cmd
, msg_inbuf
, mhdr
.lin
,
154 msg_outbuf
, mhdr
.lout
);
158 memset(msg_outbuf
, 0, mhdr
.lout
);
159 if(msg_log_offset
< mhdr
.llog
)
160 mhdr
.llog
= msg_log_offset
;
162 iov
[1].iov_base
= msg_outbuf
;
163 iov
[1].iov_len
= mhdr
.lout
;
164 iov
[2].iov_base
= msg_logbuf
;
165 iov
[2].iov_len
= mhdr
.llog
;
166 l
= sendmsg(p
->fd
, &msg
, MSG_NOSIGNAL
);
168 ERROR("CTL: Couldn't send response: %m");
169 else if(l
!= sizeof(mhdr
) + mhdr
.lout
+ mhdr
.llog
)
172 ("CTL: Couldn't send full response, sent %d bytes instead of %zd.",
173 l
, sizeof(mhdr
) + mhdr
.lout
+ mhdr
.llog
);
177 static struct epoll_event_handler ctl_handler
= {0};
179 int ctl_socket_init(void)
181 int s
= server_socket();
186 ctl_handler
.handler
= ctl_rcv_handler
;
188 TST(add_epoll(&ctl_handler
) == 0, -1);
192 void ctl_socket_cleanup(void)
194 remove_epoll(&ctl_handler
);
195 close(ctl_handler
.fd
);