• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Unionman Technology Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/socket.h>
20 #include <sys/un.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 
24 #include "sample_server.h"
25 
main(int argc,char * argv[])26 int main(int argc, char *argv[])
27 {
28     struct sockaddr_un name;
29     memset(&name, 0, sizeof(struct sockaddr_un));
30     int ret = 0;
31     int connection_socket = 0;
32     int data_socket = 0;
33 
34     /*
35      * In case the program exited inadvertently on the last run,
36      * remove the socket.
37      */
38     unlink(SOCKET_NAME);
39 
40     /* Create local socket. */
41     connection_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
42     if (connection_socket == -1) {
43         perror("socket");
44         return SOCKET_FAIL;
45     }
46 
47     /* Bind socket to socket name. */
48     name.sun_family = AF_UNIX;
49     strncpy(name.sun_path, SOCKET_NAME, sizeof(name.sun_path) - 1);
50     printf("server name.sun_path = %s\n", name.sun_path);
51 
52     ret = bind(connection_socket, (const struct sockaddr *)&name, sizeof(struct sockaddr_un));
53     if (ret == -1) {
54         perror("bind");
55         return SOCKET_FAIL;
56     }
57     ret = listen(connection_socket, 20L);
58     if (ret == -1) {
59         perror("listen");
60         return SOCKET_FAIL;
61     }
62 
63     /* Wait for incoming connection. */
64     for (;;) {
65         data_socket = accept(connection_socket, NULL, NULL);
66         if (data_socket == -1) {
67             perror("accept");
68             return SOCKET_FAIL;
69         }
70 
71         ret = communication(data_socket);
72         if (ret == -1) {
73             perror("communication");
74             return SOCKET_FAIL;
75         }
76 
77         /* Close socket. */
78         close(data_socket);
79     }
80     close(connection_socket);
81     unlink(SOCKET_NAME);
82     return SOCKET_OK;
83 }
84 
85 // deal with client socket
communication(int data_socket)86 int communication(int data_socket)
87 {
88     int ret = 0;
89     char buffer[BUFFER_SIZE] = {0};
90 
91     /* Wait for next data packet. */
92     ret = read(data_socket, buffer, BUFFER_SIZE);
93     if (ret == -1) {
94         perror("read");
95         return SOCKET_FAIL;
96     }
97     printf("read path = %s\n", buffer);
98 
99     /* open buffer path file read and write permission. */
100     ret = chmod(buffer, READ_AND_WRITE);
101     if (ret == -1) {
102         perror("chmod");
103         return SOCKET_FAIL;
104     }
105 
106     /* Send chmod result. */
107     (void)sprintf(buffer, "%d", ret);
108     ret = write(data_socket, buffer, BUFFER_SIZE);
109     if (ret == -1) {
110         perror("write");
111         return SOCKET_FAIL;
112     }
113 
114     return SOCKET_OK;
115 }
116