Imported Upstream version 20081130
[ltp-debian.git] / testcases / audit-test / utils / bin / do_socketcall.c
blob81eec263966bcdde254136961cd4f71fdce10de2
1 /* (c) Copyright Hewlett-Packard Development Company, L.P., 2007
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of version 2 the GNU General Public License as
5 * published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #include "includes.h"
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <linux/net.h>
21 int main(int argc, char **argv)
23 int exitval, result;
24 int sockfd;
25 struct sockaddr_in my_addr;
26 socklen_t addrlen = sizeof(my_addr);
28 if (argc < 2) {
29 fprintf(stderr, "Usage:\n%s <op> <arg>...\n", argv[0]);
30 return TEST_ERROR;
33 if (!strcmp(argv[1], "bind")) {
34 if (argc != 4) {
35 fprintf(stderr, "Usage:\n%s bind <port> <0|127>\n", argv[0]);
36 return TEST_ERROR;
39 memset(&my_addr, 0, addrlen);
40 my_addr.sin_family = AF_INET;
41 my_addr.sin_port = htons(atoi(argv[2]));
42 my_addr.sin_addr.s_addr = atoi(argv[3]);
44 sockfd = socket(PF_INET, SOCK_STREAM, 0);
45 if (sockfd < 0) {
46 perror("do_socketcall: open socket");
47 return TEST_ERROR;
50 errno = 0;
51 /* use library routine for build's sake */
52 exitval = bind(sockfd, (struct sockaddr *)&my_addr, addrlen);
54 } else {
55 fprintf(stderr, "%s: unimplemented op: %s\n", argv[0], argv[1]);
56 return TEST_ERROR;
59 result = exitval < 0;
60 printf("%d %d %d\n", result, result ? errno : exitval, getpid());
61 return result;