-
Notifications
You must be signed in to change notification settings - Fork 5
/
rpbarsend.cc
76 lines (67 loc) · 1.95 KB
/
rpbarsend.cc
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
// Copyright (C) 2010 Daniel Maturana
// This file is part of rpbar.
//
// rpbar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// rpbar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with rpbar. If not, see <http://www.gnu.org/licenses/>.
//
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
// TODO just use c stuff
#include <string>
#include <sstream>
#include "settings.hh"
int main(int argc, const char *argv[]) {
const char *default_message = "m";
const char *message;
if (argc == 2) {
message = argv[1];
} else {
message = default_message;
}
int sockfd;
if ((sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
perror("socket");
exit(2);
}
struct sockaddr_un servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sun_family = AF_UNIX;
uid_t uid = geteuid();
std::stringstream ss;
ss << RPBAR_SOCKET_PATH << "-" << uid;
std::string socket_path(ss.str());
strcpy(servaddr.sun_path, socket_path.c_str());
int numbytes;
if ((numbytes = sendto(sockfd,
message,
strlen(message),
0,
(struct sockaddr *) &servaddr,
sizeof(servaddr))) == -1) {
perror("sendto");
exit(1);
}
close(sockfd);
return 0;
}