Debian Codegraph @

src/tls/wsinstance-start.c

raw
1 /*
2 * Copyright (C) 2019 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 #include <err.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/socket.h>
14 #include <unistd.h>
15
16 #include "socket-io.h"
17
18 int
19 main (int argc,
20 char **argv)
21 {
22 const char *wsinstance_sockdir = "/run/cockpit/wsinstance";
23 int dirfd;
24 char result[20];
25 int fd;
26
27 if (argc != 2 && argc != 3)
28 errx (EXIT_FAILURE, "usage: ./wsinstance-start [instanceid] [wsinstance_sockdir]");
29
30 if (argc == 3)
31 wsinstance_sockdir = argv[2];
32
33 dirfd = open (wsinstance_sockdir, O_PATH | O_DIRECTORY | O_CLOEXEC);
34 if (dirfd == -1)
35 err (EXIT_FAILURE, "Couldn't open wsinstance_sockdir %s", wsinstance_sockdir);
36
37 fd = socket (AF_UNIX, SOCK_STREAM, 0);
38 if (fd == -1)
39 err (EXIT_FAILURE, "Couldn't create AF_UNIX socket");
40
41 if (af_unix_connectat (fd, dirfd, "https-factory.sock"))
42 err (EXIT_FAILURE, "Couldn't connect to factory socket");
43
44 if (!send_all (fd, argv[1], strlen (argv[1]), 50 * 1000000))
45 errx (EXIT_FAILURE, "Couldn't send instance name");
46
47 if (!recv_alnum (fd, result, sizeof result, 30 * 1000000))
48 errx (EXIT_FAILURE, "Failed to receive result");
49
50 printf ("%s\n", result);
51
52 close (dirfd);
53 close (fd);
54
55 return 0;
56 }