]>
git.proxmox.com Git - ovs.git/blob - lib/unixctl.c
2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 #include <sys/socket.h>
30 #include "dynamic-string.h"
31 #include "fatal-signal.h"
34 #include "poll-loop.h"
36 #include "socket-util.h"
39 #ifndef SCM_CREDENTIALS
43 #define THIS_MODULE VLM_unixctl
46 struct unixctl_command
{
55 enum { S_RECV
, S_PROCESS
, S_SEND
} state
;
61 /* Server for control connection. */
62 struct unixctl_server
{
68 /* Client for control connection. */
69 struct unixctl_client
{
75 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
77 static struct shash commands
= SHASH_INITIALIZER(&commands
);
80 unixctl_help(struct unixctl_conn
*conn
, const char *args OVS_UNUSED
,
83 struct ds ds
= DS_EMPTY_INITIALIZER
;
84 struct shash_node
*node
;
86 ds_put_cstr(&ds
, "The available commands are:\n");
87 SHASH_FOR_EACH (node
, &commands
) {
88 ds_put_format(&ds
, "\t%s\n", node
->name
);
90 unixctl_command_reply(conn
, 214, ds_cstr(&ds
));
95 unixctl_command_register(const char *name
, unixctl_cb_func
*cb
, void *aux
)
97 struct unixctl_command
*command
;
99 assert(!shash_find_data(&commands
, name
)
100 || shash_find_data(&commands
, name
) == cb
);
101 command
= xmalloc(sizeof *command
);
104 shash_add(&commands
, name
, command
);
108 translate_reply_code(int code
)
111 case 200: return "OK";
112 case 201: return "Created";
113 case 202: return "Accepted";
114 case 204: return "No Content";
115 case 211: return "System Status";
116 case 214: return "Help";
117 case 400: return "Bad Request";
118 case 401: return "Unauthorized";
119 case 403: return "Forbidden";
120 case 404: return "Not Found";
121 case 500: return "Internal Server Error";
122 case 501: return "Invalid Argument";
123 case 503: return "Service Unavailable";
124 default: return "Unknown";
129 unixctl_command_reply(struct unixctl_conn
*conn
,
130 int code
, const char *body
)
132 struct ds
*out
= &conn
->out
;
134 COVERAGE_INC(unixctl_replied
);
135 assert(conn
->state
== S_PROCESS
);
136 conn
->state
= S_SEND
;
140 ds_put_format(out
, "%03d %s\n", code
, translate_reply_code(code
));
143 for (p
= body
; *p
!= '\0'; ) {
144 size_t n
= strcspn(p
, "\n");
147 ds_put_char(out
, '.');
149 ds_put_buffer(out
, p
, n
);
150 ds_put_char(out
, '\n');
157 ds_put_cstr(out
, ".\n");
160 /* Creates a unixctl server listening on 'path', which may be:
162 * - NULL, in which case <rundir>/<program>.<pid>.ctl is used.
164 * - A name that does not start with '/', in which case it is put in
167 * - An absolute path (starting with '/') that gives the exact name of
168 * the Unix domain socket to listen on.
170 * A program that (optionally) daemonizes itself should call this function
171 * *after* daemonization, so that the socket name contains the pid of the
172 * daemon instead of the pid of the program that exited. (Otherwise,
173 * "ovs-appctl --target=<program>" will fail.)
175 * Returns 0 if successful, otherwise a positive errno value. If successful,
176 * sets '*serverp' to the new unixctl_server, otherwise to NULL. */
178 unixctl_server_create(const char *path
, struct unixctl_server
**serverp
)
180 struct unixctl_server
*server
;
183 unixctl_command_register("help", unixctl_help
, NULL
);
185 server
= xmalloc(sizeof *server
);
186 list_init(&server
->conns
);
189 if (path
[0] == '/') {
190 server
->path
= xstrdup(path
);
192 server
->path
= xasprintf("%s/%s", ovs_rundir
, path
);
195 server
->path
= xasprintf("%s/%s.%ld.ctl", ovs_rundir
,
196 program_name
, (long int) getpid());
199 server
->fd
= make_unix_socket(SOCK_STREAM
, true, false, server
->path
,
201 if (server
->fd
< 0) {
203 ovs_error(error
, "could not initialize control socket %s",
208 if (chmod(server
->path
, S_IRUSR
| S_IWUSR
) < 0) {
210 ovs_error(error
, "failed to chmod control socket %s", server
->path
);
214 if (listen(server
->fd
, 10) < 0) {
216 ovs_error(error
, "Failed to listen on control socket %s",
225 if (server
->fd
>= 0) {
235 new_connection(struct unixctl_server
*server
, int fd
)
237 struct unixctl_conn
*conn
;
241 conn
= xmalloc(sizeof *conn
);
242 list_push_back(&server
->conns
, &conn
->node
);
244 conn
->state
= S_RECV
;
245 ofpbuf_init(&conn
->in
, 128);
251 run_connection_output(struct unixctl_conn
*conn
)
253 while (conn
->out_pos
< conn
->out
.length
) {
254 size_t bytes_written
;
257 error
= write_fully(conn
->fd
, conn
->out
.string
+ conn
->out_pos
,
258 conn
->out
.length
- conn
->out_pos
, &bytes_written
);
259 conn
->out_pos
+= bytes_written
;
264 conn
->state
= S_RECV
;
269 process_command(struct unixctl_conn
*conn
, char *s
)
271 struct unixctl_command
*command
;
275 COVERAGE_INC(unixctl_received
);
276 conn
->state
= S_PROCESS
;
279 name_len
= strcspn(name
, " ");
280 args
= name
+ name_len
;
281 args
+= strspn(args
, " ");
282 name
[name_len
] = '\0';
284 command
= shash_find_data(&commands
, name
);
286 command
->cb(conn
, args
, command
->aux
);
288 char *msg
= xasprintf("\"%s\" is not a valid command", name
);
289 unixctl_command_reply(conn
, 400, msg
);
295 run_connection_input(struct unixctl_conn
*conn
)
302 newline
= memchr(conn
->in
.data
, '\n', conn
->in
.size
);
304 char *command
= conn
->in
.data
;
305 size_t n
= newline
- command
+ 1;
307 if (n
> 0 && newline
[-1] == '\r') {
312 process_command(conn
, command
);
314 ofpbuf_pull(&conn
->in
, n
);
315 if (!conn
->in
.size
) {
316 ofpbuf_clear(&conn
->in
);
321 ofpbuf_prealloc_tailroom(&conn
->in
, 128);
322 error
= read_fully(conn
->fd
, ofpbuf_tail(&conn
->in
),
323 ofpbuf_tailroom(&conn
->in
), &bytes_read
);
324 conn
->in
.size
+= bytes_read
;
325 if (conn
->in
.size
> 65536) {
326 VLOG_WARN_RL(&rl
, "excess command length, killing connection");
330 if (error
== EAGAIN
|| error
== EWOULDBLOCK
) {
335 if (error
!= EOF
|| conn
->in
.size
!= 0) {
336 VLOG_WARN_RL(&rl
, "read failed: %s",
338 ? "connection dropped mid-command"
348 run_connection(struct unixctl_conn
*conn
)
354 old_state
= conn
->state
;
355 switch (conn
->state
) {
357 error
= run_connection_input(conn
);
365 error
= run_connection_output(conn
);
374 } while (conn
->state
!= old_state
);
379 kill_connection(struct unixctl_conn
*conn
)
381 list_remove(&conn
->node
);
382 ofpbuf_uninit(&conn
->in
);
383 ds_destroy(&conn
->out
);
389 unixctl_server_run(struct unixctl_server
*server
)
391 struct unixctl_conn
*conn
, *next
;
394 for (i
= 0; i
< 10; i
++) {
395 int fd
= accept(server
->fd
, NULL
, NULL
);
397 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
398 VLOG_WARN_RL(&rl
, "accept failed: %s", strerror(errno
));
402 new_connection(server
, fd
);
405 LIST_FOR_EACH_SAFE (conn
, next
,
406 struct unixctl_conn
, node
, &server
->conns
) {
407 int error
= run_connection(conn
);
408 if (error
&& error
!= EAGAIN
) {
409 kill_connection(conn
);
415 unixctl_server_wait(struct unixctl_server
*server
)
417 struct unixctl_conn
*conn
;
419 poll_fd_wait(server
->fd
, POLLIN
);
420 LIST_FOR_EACH (conn
, struct unixctl_conn
, node
, &server
->conns
) {
421 if (conn
->state
== S_RECV
) {
422 poll_fd_wait(conn
->fd
, POLLIN
);
423 } else if (conn
->state
== S_SEND
) {
424 poll_fd_wait(conn
->fd
, POLLOUT
);
429 /* Destroys 'server' and stops listening for connections. */
431 unixctl_server_destroy(struct unixctl_server
*server
)
434 struct unixctl_conn
*conn
, *next
;
436 LIST_FOR_EACH_SAFE (conn
, next
,
437 struct unixctl_conn
, node
, &server
->conns
) {
438 kill_connection(conn
);
442 fatal_signal_unlink_file_now(server
->path
);
448 /* Connects to a Vlog server socket. 'path' should be the name of a Vlog
449 * server socket. If it does not start with '/', it will be prefixed with
450 * ovs_rundir (e.g. /var/run).
452 * Returns 0 if successful, otherwise a positive errno value. If successful,
453 * sets '*clientp' to the new unixctl_client, otherwise to NULL. */
455 unixctl_client_create(const char *path
, struct unixctl_client
**clientp
)
458 struct unixctl_client
*client
;
462 /* Determine location. */
463 client
= xmalloc(sizeof *client
);
464 if (path
[0] == '/') {
465 client
->connect_path
= xstrdup(path
);
467 client
->connect_path
= xasprintf("%s/%s", ovs_rundir
, path
);
469 client
->bind_path
= xasprintf("/tmp/vlog.%ld.%d",
470 (long int) getpid(), counter
++);
473 fd
= make_unix_socket(SOCK_STREAM
, false, false,
474 client
->bind_path
, client
->connect_path
);
480 /* Bind socket to stream. */
481 client
->stream
= fdopen(fd
, "r+");
482 if (!client
->stream
) {
484 VLOG_WARN("%s: fdopen failed (%s)",
485 client
->connect_path
, strerror(error
));
495 free(client
->connect_path
);
496 free(client
->bind_path
);
502 /* Destroys 'client'. */
504 unixctl_client_destroy(struct unixctl_client
*client
)
507 fatal_signal_unlink_file_now(client
->bind_path
);
508 free(client
->bind_path
);
509 free(client
->connect_path
);
510 fclose(client
->stream
);
515 /* Sends 'request' to the server socket and waits for a reply. Returns 0 if
516 * successful, otherwise to a positive errno value. If successful, sets
517 * '*reply' to the reply, which the caller must free, otherwise to NULL. */
519 unixctl_client_transact(struct unixctl_client
*client
,
521 int *reply_code
, char **reply_body
)
523 struct ds line
= DS_EMPTY_INITIALIZER
;
524 struct ds reply
= DS_EMPTY_INITIALIZER
;
527 /* Send 'request' to server. Add a new-line if 'request' didn't end in
529 fputs(request
, client
->stream
);
530 if (request
[0] == '\0' || request
[strlen(request
) - 1] != '\n') {
531 putc('\n', client
->stream
);
533 if (ferror(client
->stream
)) {
534 VLOG_WARN("error sending request to %s: %s",
535 client
->connect_path
, strerror(errno
));
539 /* Wait for response. */
544 error
= ds_get_line(&line
, client
->stream
);
546 VLOG_WARN("error reading reply from %s: %s",
547 client
->connect_path
,
548 (error
== EOF
? "unexpected end of file"
554 if (*reply_code
== -1) {
555 if (!isdigit((unsigned char)s
[0])
556 || !isdigit((unsigned char)s
[1])
557 || !isdigit((unsigned char)s
[2])) {
558 VLOG_WARN("reply from %s does not start with 3-digit code",
559 client
->connect_path
);
563 sscanf(s
, "%3d", reply_code
);
571 ds_put_cstr(&reply
, s
);
572 ds_put_char(&reply
, '\n');
575 *reply_body
= ds_cstr(&reply
);
584 return error
== EOF
? EPROTO
: error
;
587 /* Returns the path of the server socket to which 'client' is connected. The
588 * caller must not modify or free the returned string. */
590 unixctl_client_target(const struct unixctl_client
*client
)
592 return client
->connect_path
;