2012-08-02 20:41:25 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
2012-10-03 20:07:08 +03:00
|
|
|
#include "helpers.h"
|
2012-08-02 22:37:20 +02:00
|
|
|
#include "common.h"
|
2012-08-02 20:41:25 +02:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2012-09-26 00:09:44 +03:00
|
|
|
int sock_fd;
|
2012-08-02 20:41:25 +02:00
|
|
|
struct sockaddr_un sock_address;
|
2012-10-02 20:28:20 +03:00
|
|
|
size_t msglen = 0;
|
2012-10-02 21:36:44 +03:00
|
|
|
char msg[BUFSIZ];
|
|
|
|
char rsp[BUFSIZ];
|
2012-08-02 20:41:25 +02:00
|
|
|
|
2012-08-02 22:37:20 +02:00
|
|
|
if (argc < 2)
|
2013-01-06 18:36:27 +01:00
|
|
|
err("No arguments given.\n");
|
2012-08-02 20:41:25 +02:00
|
|
|
|
2012-09-26 00:09:44 +03:00
|
|
|
char *sock_path = getenv(SOCKET_ENV_VAR);
|
|
|
|
if (sock_path == NULL || strlen(sock_path) == 0)
|
2013-01-06 18:36:27 +01:00
|
|
|
warn("The environment variable '%s' is not set or empty, we will use: '%s'.\n", SOCKET_ENV_VAR, DEFAULT_SOCKET_PATH);
|
2012-09-26 00:09:44 +03:00
|
|
|
else if (sizeof(sock_address.sun_path) <= strlen(sock_path))
|
2013-01-06 18:36:27 +01:00
|
|
|
err("The string can't fit in the socket address: '%s'.\n", sock_path);
|
2012-08-29 12:45:44 +02:00
|
|
|
|
2012-08-02 20:41:25 +02:00
|
|
|
sock_address.sun_family = AF_UNIX;
|
2012-10-03 22:44:25 +02:00
|
|
|
strncpy(sock_address.sun_path, (sock_path == NULL ? DEFAULT_SOCKET_PATH : sock_path), sizeof(sock_address.sun_path));
|
2012-09-26 00:09:44 +03:00
|
|
|
sock_address.sun_path[sizeof(sock_address.sun_path) - 1] = 0;
|
|
|
|
|
2012-10-04 14:46:41 +02:00
|
|
|
for (int offset = 0, len = sizeof(msg), n = 0; --argc && ++argv && len > 0; offset += n, len -= n)
|
2012-09-26 00:09:44 +03:00
|
|
|
n = snprintf(msg + offset, len, "%s ", *argv);
|
2012-08-02 20:41:25 +02:00
|
|
|
|
2012-10-02 20:28:20 +03:00
|
|
|
msglen = strlen(msg);
|
2012-10-03 23:00:03 +02:00
|
|
|
if (msg[msglen - 1] == ' ')
|
2012-10-02 20:28:38 +03:00
|
|
|
msg[--msglen] = '\0';
|
2012-10-02 20:28:20 +03:00
|
|
|
|
2012-08-02 20:41:25 +02:00
|
|
|
sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
2012-09-26 00:09:44 +03:00
|
|
|
if (sock_fd == -1)
|
2013-01-06 18:36:27 +01:00
|
|
|
err("Failed to create the socket.\n");
|
2012-09-26 00:09:44 +03:00
|
|
|
|
|
|
|
if (connect(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
|
2013-01-06 18:36:27 +01:00
|
|
|
err("Failed to connect to the socket.\n");
|
2012-08-02 20:41:25 +02:00
|
|
|
|
2012-10-02 20:28:20 +03:00
|
|
|
if (send(sock_fd, msg, msglen, 0) == -1)
|
2013-01-06 18:36:27 +01:00
|
|
|
err("Failed to send the data.\n");
|
2012-08-28 21:15:29 +02:00
|
|
|
|
2012-09-26 00:09:44 +03:00
|
|
|
int n = recv(sock_fd, rsp, sizeof(rsp), 0);
|
2012-10-03 22:44:25 +02:00
|
|
|
if (n == -1) {
|
2013-01-06 18:36:27 +01:00
|
|
|
err("Failed to get the response.\n");
|
2012-10-03 22:44:25 +02:00
|
|
|
} else if (n > 0) {
|
2012-09-26 00:09:44 +03:00
|
|
|
rsp[n] = '\0';
|
|
|
|
printf("%s\n", rsp);
|
2012-08-02 20:41:25 +02:00
|
|
|
}
|
2012-08-07 12:17:47 +02:00
|
|
|
|
2012-09-26 00:09:44 +03:00
|
|
|
if (sock_fd)
|
|
|
|
close(sock_fd);
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2012-08-02 20:41:25 +02:00
|
|
|
}
|