1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <errno.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <unistd.h>
23 #include <cstdlib>
24 
25 int main(int argc, char* argv[]) {
26     if (argc != 3) {
27         fprintf(stderr, "syntax: %s <path> <command>\n", argv[0]);
28         return 1;
29     }
30 
31     int fd = socket(AF_UNIX, SOCK_STREAM, 0);
32     if (fd < 0) {
33         fprintf(stderr, "socket creation failed: %d\n", errno);
34         return 1;
35     }
36 
37     struct sockaddr_un addr;
38     memset(&addr, 0, sizeof(addr));
39     addr.sun_family = AF_UNIX;
40     strncpy(addr.sun_path, argv[1], sizeof(addr.sun_path));
41 
42     int ok = connect(fd, (struct sockaddr*)&addr, sizeof(addr));
43     if (ok < 0) {
44         fprintf(stderr, "connection could not be established: %d\n", errno);
45         return 1;
46     }
47 
48     ok = write(fd, argv[2], strlen(argv[2]));
49     if (ok < 0) {
50         fprintf(stderr, "write failed: %d\n", errno);
51         return 1;
52     }
53 
54     return 0;
55 }
56