-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
63 lines (53 loc) · 964 Bytes
/
Copy pathclient.c
File metadata and controls
63 lines (53 loc) · 964 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <linux/limits.h>
#include <errno.h>
#include "msg.h"
#include "cfg.h"
extern char **environ;
int run_client(const struct cfg *cfg)
{
char buf[PATH_MAX];
struct msg msg;
if (cfg->kill)
{
msg.type = MSG_KILL;
}
else if (cfg->interrupt)
{
msg.type = MSG_INTERRUPT;
}
else
{
msg.type = MSG_COMMAND;
msg.cmd = cfg->cmd;
msg.cwd = getcwd(buf, sizeof buf);
msg.env = environ;
}
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0)
{
perror("socket()");
return EXIT_FAILURE;
}
if (connect(sock, cfg->sa_ptr, cfg->sa_len) < 0)
{
if (errno == ECONNREFUSED)
{
fprintf(stderr, "error: no such server: %s\n", cfg->name);
}
else
{
perror("connect()");
}
return EXIT_FAILURE;
}
if (msg_write(sock, &msg) < 0)
{
fprintf(stderr, "error: failed to write message\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}