-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_cmd_client.c
More file actions
180 lines (148 loc) · 3.75 KB
/
app_cmd_client.c
File metadata and controls
180 lines (148 loc) · 3.75 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <unistd.h>
#include <sys/select.h>
#include <time.h>
#include <pthread.h>
#define SOCKED_CMD_PATH "/var/usrCmd.socket"
#define CLI_CMD_ARGC 16
#define CLI_CMD_LEN 64
#define CLI_BUFF_LEN 4096
#define CLI_MAGIC_NUM 0x484B4145
#define offsetof(TYPE, MEMBER) ((intptr_t)&((TYPE *)0)->MEMBER)
#define CLI_ERROR printf
typedef unsigned int UINT32;
typedef unsigned char UINT8;
typedef char CHAR;
typedef int INT32;
typedef void VOID;
typedef struct
{
UINT32 uMagic;
UINT32 uArgc;
CHAR aArgv[CLI_CMD_ARGC][CLI_CMD_LEN];
}CLI_DATA_T;
static INT32 client_readn(INT32 iFd, VOID *pData, UINT32 uLen)
{
UINT32 uLeft = 0;
INT32 uRead = 0;
INT32 uRet = 0;
UINT32 uTry = 0;
CHAR *pDst = NULL;
struct timeval tv = {0};
fd_set rdFds;
uLeft = uLen;
pDst = pData;
tv.tv_sec = 2;
tv.tv_usec = 0;
while (uLeft > 0)
{
FD_ZERO(&rdFds);
FD_SET(iFd, &rdFds);
uRet = select(iFd + 1, &rdFds, NULL, NULL, &tv);
if (uRet < 0)
{
CLI_ERROR("Select error out.");
return -1;
}
else if (uRet == 0)
{
uTry++;
if (uTry > 10)
{
break;
}
CLI_ERROR("Select time out try (%d) again.", uTry);
}
if(FD_ISSET(iFd, &rdFds))
{
if ((uRead = recv(iFd, pDst, uLeft, 0)) <= 0)
{
CLI_ERROR("Recv %s uRead:%d\n", strerror(errno), uRead);
break;
}
uLeft -= uRead;
pDst += uRead;
}
}
return uLen - uLeft;
}
static INT32 client_writen(INT32 iFd, VOID *pData, UINT32 uLen)
{
UINT32 uLeft = 0;
INT32 uWriten = 0;
CHAR *pSrc = NULL;
uLeft = uLen;
pSrc = pData;
while (uLeft > 0)
{
if ((uWriten = send(iFd, pSrc, uLeft, 0)) < 0)
{
CLI_ERROR("Send %s\n", strerror(errno));
break;
}
uLeft -= uWriten;
pSrc += uWriten;
}
return uLen- uLeft;
}
static INT32 client_conn(const char *name)
{
INT32 iSockFd = 0;
INT32 iRet = 0;
INT32 iLen = 0;
struct sockaddr_un un = {0};
iSockFd = socket(AF_UNIX, SOCK_STREAM, 0);
if (iSockFd == -1)
{
CLI_ERROR("Socket %s\n", strerror(errno));
return -1;
}
un.sun_family = AF_UNIX;
strcpy(un.sun_path, name);
iLen = (INT32)(offsetof(struct sockaddr_un, sun_path) + strlen(name));
iRet = connect(iSockFd, (struct sockaddr *)&un, iLen);
if (iRet < 0)
{
CLI_ERROR("Connect %s\n", strerror(errno));
close(iSockFd);
return -1;
}
return iSockFd;
}
INT32 main(int argc, char *argv[])
{
INT32 i = 0;
INT32 iRet = 0;
INT32 iSockFd = 0;
CLI_DATA_T stCmdData = {0};
if (argc > CLI_CMD_ARGC)
{
CLI_ERROR("argc num too large argc : %d.\n", argc);
return -1;
}
iSockFd = client_conn(SOCKED_CMD_PATH);
if (iSockFd < 0)
{
CLI_ERROR("Connect %s %s\n", SOCKED_CMD_PATH, strerror(errno));
return -1;
}
stCmdData.uMagic = CLI_MAGIC_NUM;
stCmdData.uArgc = argc;
for (i = 0; i < argc; i++)
{
strncpy(stCmdData.aArgv[i], argv[i], strlen(argv[i]));
}
iRet = client_writen(iSockFd, &stCmdData, sizeof(stCmdData));
if (iRet != sizeof(stCmdData))
{
CLI_ERROR("Write %s\n", strerror(errno));
return -1;
}
return 0;
}