]>
git.ipfire.org Git - people/ms/rstp.git/blob - ctl_socket_client.c
1 /*****************************************************************************
2 Copyright (c) 2006 EMC Corporation.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2 of the License, or (at your option)
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 You should have received a copy of the GNU General Public License along with
15 this program; if not, write to the Free Software Foundation, Inc., 59
16 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 The full GNU General Public License is included in this distribution in the
21 Authors: Srinivas Aji <Aji_Srinivas@emc.com>
23 ******************************************************************************/
25 #include "ctl_socket.h"
27 #include <sys/types.h>
28 #include <sys/socket.h>
40 int ctl_client_init(void)
42 struct sockaddr_un sa_svr
;
44 TST(strlen(RSTP_SERVER_SOCK_NAME
) < sizeof(sa_svr
.sun_path
), -1);
46 s
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
48 ERROR("Couldn't open unix socket: %m");
52 set_socket_address(&sa_svr
, RSTP_SERVER_SOCK_NAME
);
54 struct sockaddr_un sa
;
56 sprintf(tmpname
, "RSTPCTL_%d", getpid());
57 set_socket_address(&sa
, tmpname
);
58 /* We need this bind. The autobind on connect isn't working properly.
59 The server doesn't get a proper sockaddr in recvmsg if we don't do this.
61 if (bind(s
, (struct sockaddr
*)&sa
, sizeof(sa
)) != 0) {
62 ERROR("Couldn't bind socket: %m");
67 if (connect(s
, (struct sockaddr
*)&sa_svr
, sizeof(sa_svr
)) != 0) {
68 ERROR("Couldn't connect to server");
77 void ctl_client_cleanup(void)
85 int send_ctl_message(int cmd
, void *inbuf
, int lin
, void *outbuf
, int lout
,
86 LogString
*log
, int *res
)
88 struct ctl_msg_hdr mhdr
;
97 msg
.msg_control
= NULL
;
98 msg
.msg_controllen
= 0;
103 mhdr
.llog
= sizeof(log
->buf
) - 1;
104 iov
[0].iov_base
= &mhdr
;
105 iov
[0].iov_len
= sizeof(mhdr
);
106 iov
[1].iov_base
= (void *)inbuf
;
107 iov
[1].iov_len
= lin
;
108 iov
[2].iov_base
= log
->buf
;
111 l
= sendmsg(fd
, &msg
, 0);
113 ERROR("Error sending message to server: %m");
116 if (l
!= sizeof(mhdr
) + lin
) {
117 ERROR("Error sending message to server: Partial write");
121 iov
[1].iov_base
= outbuf
;
122 iov
[1].iov_len
= lout
;
124 iov
[2].iov_base
= log
->buf
;
125 iov
[2].iov_len
= sizeof(log
->buf
);
129 int timeout
= 5000; /* 5 s */
135 r
= poll(&pfd
, 1, timeout
);
138 ("Error getting message from server: Timeout");
143 ("Error getting message from server: poll error: %m");
147 revents
& (POLLERR
| POLLHUP
| POLLNVAL
| POLLIN
)) ==
150 l
= recvmsg(fd
, &msg
, 0);
152 ERROR("Error getting message from server: %m");
156 || l
!= sizeof(mhdr
) + mhdr
.lout
+ mhdr
.llog
157 || mhdr
.cmd
!= cmd
) {
158 ERROR("Error getting message from server: Bad format");
161 if (mhdr
.lout
!= lout
) {
162 ERROR("Error, unexpected result length %d, "
163 "expected %d\n", mhdr
.lout
, lout
);
166 if (mhdr
.llog
>= sizeof(log
->buf
)) {
167 ERROR("Invalid log message length %d", mhdr
.llog
);
173 log
->buf
[mhdr
.llog
] = 0;