Add qemu 2.4.0
[kvmfornfv.git] / qemu / tests / qemu-iotests / socket_scm_helper.c
1 /*
2  * SCM_RIGHTS with unix socket help program for test
3  *
4  * Copyright IBM, Inc. 2013
5  *
6  * Authors:
7  *  Wenchao Xia    <xiawenc@linux.vnet.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10  * See the COPYING.LIB file in the top-level directory.
11  */
12
13 #include <stdio.h>
14 #include <errno.h>
15 #include <sys/socket.h>
16 #include <sys/un.h>
17 #include <stdlib.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22
23 /* #define SOCKET_SCM_DEBUG */
24
25 /*
26  * @fd and @fd_to_send will not be checked for validation in this function,
27  * a blank will be sent as iov data to notify qemu.
28  */
29 static int send_fd(int fd, int fd_to_send)
30 {
31     struct msghdr msg;
32     struct iovec iov[1];
33     int ret;
34     char control[CMSG_SPACE(sizeof(int))];
35     struct cmsghdr *cmsg;
36
37     memset(&msg, 0, sizeof(msg));
38     memset(control, 0, sizeof(control));
39
40     /* Send a blank to notify qemu */
41     iov[0].iov_base = (void *)" ";
42     iov[0].iov_len = 1;
43
44     msg.msg_iov = iov;
45     msg.msg_iovlen = 1;
46
47     msg.msg_control = control;
48     msg.msg_controllen = sizeof(control);
49
50     cmsg = CMSG_FIRSTHDR(&msg);
51
52     cmsg->cmsg_len = CMSG_LEN(sizeof(int));
53     cmsg->cmsg_level = SOL_SOCKET;
54     cmsg->cmsg_type = SCM_RIGHTS;
55     memcpy(CMSG_DATA(cmsg), &fd_to_send, sizeof(int));
56
57     do {
58         ret = sendmsg(fd, &msg, 0);
59     } while (ret < 0 && errno == EINTR);
60
61     if (ret < 0) {
62         fprintf(stderr, "Failed to send msg, reason: %s\n", strerror(errno));
63     }
64
65     return ret;
66 }
67
68 /* Convert string to fd number. */
69 static int get_fd_num(const char *fd_str)
70 {
71     int sock;
72     char *err;
73
74     errno = 0;
75     sock = strtol(fd_str, &err, 10);
76     if (errno) {
77         fprintf(stderr, "Failed in strtol for socket fd, reason: %s\n",
78                 strerror(errno));
79         return -1;
80     }
81     if (!*fd_str || *err || sock < 0) {
82         fprintf(stderr, "bad numerical value for socket fd '%s'\n", fd_str);
83         return -1;
84     }
85
86     return sock;
87 }
88
89 /*
90  * To make things simple, the caller needs to specify:
91  * 1. socket fd.
92  * 2. path of the file to be sent.
93  */
94 int main(int argc, char **argv, char **envp)
95 {
96     int sock, fd, ret;
97
98 #ifdef SOCKET_SCM_DEBUG
99     int i;
100     for (i = 0; i < argc; i++) {
101         fprintf(stderr, "Parameter %d: %s\n", i, argv[i]);
102     }
103 #endif
104
105     if (argc != 3) {
106         fprintf(stderr,
107                 "Usage: %s < socket-fd > < file-path >\n",
108                 argv[0]);
109         return EXIT_FAILURE;
110     }
111
112
113     sock = get_fd_num(argv[1]);
114     if (sock < 0) {
115         return EXIT_FAILURE;
116     }
117
118     /* Now only open a file in readonly mode for test purpose. If more precise
119        control is needed, use python script in file operation, which is
120        supposed to fork and exec this program. */
121     fd = open(argv[2], O_RDONLY);
122     if (fd < 0) {
123         fprintf(stderr, "Failed to open file '%s'\n", argv[2]);
124         return EXIT_FAILURE;
125     }
126
127     ret = send_fd(sock, fd);
128     if (ret < 0) {
129         close(fd);
130         return EXIT_FAILURE;
131     }
132
133     close(fd);
134     return EXIT_SUCCESS;
135 }