-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmake_sock.c
84 lines (72 loc) · 2.13 KB
/
make_sock.c
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
/*
tdiff - tree diffs
make_sock
Copyright (C) 2019 Philippe Troin <[email protected]>
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static int
max(int a, int b)
{
if (a >= b) {
return a;
} else {
return b;
}
}
int
main(int argc, char* argv[])
{
int i;
int exitcode = 0;
for (i=1; i < argc; ++i) {
int fd;
struct sockaddr_un saddrun;
if (strlen(argv[i]) >= sizeof(saddrun.sun_path)) {
fprintf(stderr, "%s: %s: path too long, maximum = %u\n",
argv[0], argv[i], (unsigned)sizeof(saddrun.sun_path));
exitcode = max(exitcode, 1);
continue;
}
fd = socket(PF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
fprintf(stderr, "%s: socket(AF_UNIX, SOCK_STREAM): %s\n",
argv[0], strerror(errno));
exitcode = max(exitcode, 2);
continue;
}
memset(&saddrun, 0, sizeof(saddrun));
saddrun.sun_family = AF_UNIX;
strncpy((char*)&saddrun.sun_path, argv[i], sizeof(saddrun.sun_path)-1);
if (bind(fd, (const struct sockaddr*)&saddrun, sizeof(saddrun)) != 0) {
fprintf(stderr, "%s: %s: bind(): %s\n",
argv[0], argv[i], strerror(errno));
close(fd);
exitcode = max(exitcode, 2);
continue;
}
if (close(fd) != 0) {
fprintf(stderr, "%s: %s: close(): %s\n",
argv[0], argv[i], strerror(errno));
exitcode = max(exitcode, 2);
}
}
exit(exitcode);
}