forked from wertarbyte/triggerhappy
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdsocket.c
More file actions
Latest commit
152 lines (135 loc) · 3.42 KB
/
Copy pathcmdsocket.c
File metadata and controls
152 lines (135 loc) · 3.42 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* Copyright 2010 Stefan Tomanek <stefan.tomanek+th@wertarbyte.de>
* You have permission to copy, modify, and redistribute under the
* terms of the GPLv3 or any later version.
* For full license terms, see COPYING.
*/
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/un.h>
#include<string.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#ifdefHAVE_SYSTEMD
#include<systemd/sd-daemon.h>
#endif
#include"devtag.h"
#include"devices.h"
#include"command.h"
#include"cmdsocket.h"
intbind_cmdsocket( char*name ) {
intcmd_fd;
#ifdefHAVE_SYSTEMD
if (sd_listen_fds(0) ==1) {
fprintf(stderr, SD_DEBUG"Found socket passed from systemd\n");
returnSD_LISTEN_FDS_START+0;
}
#endif
structsockaddr_unaddr;
/* remove any stale files */
structstatsb;
intserr=stat(name, &sb);
if ( !serr&&S_ISSOCK(sb.st_mode)) {
unlink(name);
}
cmd_fd=socket(AF_UNIX, SOCK_DGRAM, 0);
strcpy(addr.sun_path, name);
addr.sun_family=AF_UNIX;
bind (cmd_fd, (structsockaddr*) &addr,
strlen(addr.sun_path) +sizeof (addr.sun_family));
returncmd_fd;
}
intconnect_cmdsocket( char*name ) {
intfd;
structsockaddr_unserver;
fd=socket(AF_UNIX, SOCK_DGRAM, 0);
strcpy(server.sun_path, name);
server.sun_family=AF_UNIX;
connect(fd, (structsockaddr*) &server,
strlen(server.sun_path) +sizeof (server.sun_family));
returnfd;
}
structcommand*read_command( intcmd_fd ) {
structcommand*cmd=malloc(sizeof(structcommand));
if (! cmd) {
fprintf(stderr, "Unable to allocate memory for command!\n");
returnNULL;
}
intfd[1] = {-1};
charbuffer[CMSG_SPACE(sizeoffd)];
structiovecv= {
.iov_base=cmd,
.iov_len=sizeof(*cmd)
};
structmsghdrmsg= {
.msg_iov=&v,
.msg_iovlen=1,
.msg_control=buffer,
.msg_controllen=sizeof(buffer)
};
intdone=recvmsg( cmd_fd, &msg, 0 );
if (done==-1) {
fprintf(stderr, "Error reading command.");
free(cmd);
returnNULL;
}
structcmsghdr*cmessage=CMSG_FIRSTHDR(&msg);
if (cmessage) {
memcpy(fd, CMSG_DATA(cmessage), sizeoffd);
/* place FD back in the command message */
cmd->fd= (int) fd[0];
}
returncmd;
}
intsend_command( intcmd_fd, enumcommand_typetype, char*param, intpassfd, intexclusive, char*tag ) {
if (type==CMD_ADD&&passfd==1) {
type=CMD_PASSFD;
}
structcommandcmd= {
.fd=-1,
.exclusive=exclusive,
.type=type,
.param= {0},
.tag= {0}
};
if (param!=NULL) {
strncpy(cmd.param, param, TH_COMMAND_PARAM_LENGTH);
cmd.param[TH_COMMAND_PARAM_LENGTH-1] ='\0';
}
if (tag!=NULL) {
strncpy(cmd.tag, tag, TH_DEVICE_TAG_LENGTH);
cmd.tag[TH_DEVICE_TAG_LENGTH-1] ='\0';
}
structiovecv= {
.iov_base=&cmd,
.iov_len=sizeof(cmd)
};
structmsghdrm= {
.msg_iov=&v,
.msg_iovlen=1
};
/* add FD */
intdev_fd[1] = { -1 };
charbuffer[CMSG_SPACE(sizeof(dev_fd))];
if (passfd) {
intfd=open( param, O_RDONLY );
if (fd<0) {
perror("open");
return1;
}
dev_fd[0] =fd ;
m.msg_control=buffer;
m.msg_controllen=sizeof(buffer);
structcmsghdr*cmessage=CMSG_FIRSTHDR(&m);
cmessage->cmsg_level=SOL_SOCKET;
cmessage->cmsg_type=SCM_RIGHTS;
cmessage->cmsg_len=CMSG_LEN(sizeof(dev_fd));
m.msg_controllen=cmessage->cmsg_len;
memcpy(CMSG_DATA(cmessage), dev_fd, sizeofdev_fd);
}
intdone=sendmsg( cmd_fd, &m, 0 );
return (done==-1);
}