点击(此处)折叠或打开
-
//thread_wdctl_handler(void *arg)函数是wifidog启动后自动创建的控制线程,主要用于与wdctrl进行socket通信,根据wdctrl命令执行不同的操作。这里我们着重讲解的是wdctrl发送restart后wifidog的执行逻辑。
-
static void *
-
thread_wdctl_handler(void *arg)
-
{
-
int fd,
-
done,
-
i;
-
char request[MAX_BUF];
-
ssize_t read_bytes,
-
len;
-
-
debug(LOG_DEBUG, "Entering thread_wdctl_handler....");
-
-
fd = (int)arg;
-
-
debug(LOG_DEBUG, "Read bytes and stuff from %d", fd);
-
-
/* 初始化变量 */
-
read_bytes = 0;
-
done = 0;
-
memset(request, 0, sizeof(request));
-
-
/* 读取命令 */
-
while (!done && read_bytes < (sizeof(request) - 1)) {
-
len = read(fd, request + read_bytes,
-
sizeof(request) - read_bytes); //读取wdctrl发送的命令
-
-
/* 判断命令正确性 */
-
for (i = read_bytes; i < (read_bytes + len); i++) {
-
if (request[i] == 'r' || request[i] == 'n') {
-
request[i] = ' ';
-
done = 1;
-
}
-
}
-
-
/* Increment position */
-
read_bytes += len;
-
}
-
-
//判断命令
-
if (strncmp(request, "status", 6) == 0) {
-
wdctl_status(fd);
-
} else if (strncmp(request, "stop", 4) == 0) {
-
wdctl_stop(fd);
-
} else if (strncmp(request, "reset", 5) == 0) {
-
wdctl_reset(fd, (request + 6));
-
} else if (strncmp(request, "restart", 7) == 0) {
-
wdctl_restart(fd); //执行wdctl_restart(int afd)函数
-
}
-
-
if (!done) {
-
debug(LOG_ERR, "Invalid wdctl request.");
-
//关闭套接字
-
shutdown(fd, 2);
-
close(fd);
-
pthread_exit(NULL);
-
}
-
-
debug(LOG_DEBUG, "Request received: [%s]", request);
-
-
//关闭套接字
-
shutdown(fd, 2);
-
close(fd);
-
debug(LOG_DEBUG, "Exiting thread_wdctl_handler....");
-
-
return NULL;
-
}
-
-
-
//wdctl_restart(int afd)函数详解
-
static void
-
wdctl_restart(int afd)
-
{
-
int sock,
-
fd;
-
char *sock_name;
-
struct sockaddr_un sa_un;
-
s_config * conf = NULL;
-
t_client * client = NULL;
-
char * tempstring = NULL;
-
pid_t pid;
-
ssize_t written;
-
socklen_t len;
-
-
conf = config_get_config();
-
-
debug(LOG_NOTICE, "Will restart myself");
-
-
/*
-
* 准备内部连接socket
-
*/
-
memset(&sa_un, 0, sizeof(sa_un));
-
sock_name = conf->internal_sock; //conf->internal_sock值为"/tmp/wifidog.sock"
-
debug(LOG_DEBUG, "Socket name: %s", sock_name);
-
-
if (strlen(sock_name) > (sizeof(sa_un.sun_path) - 1)) {
-
-
debug(LOG_ERR, "INTERNAL socket name too long");
-
return;
-
}
-
-
debug(LOG_DEBUG, "Creating socket");
-
sock = socket(PF_UNIX, SOCK_STREAM, 0); //建立内部socket套接字
-
-
debug(LOG_DEBUG, "Got internal socket %d", sock);
-
-
/* 如果sock_name文件存在,则删除*/
-
unlink(sock_name);
-
-
debug(LOG_DEBUG, "Filling sockaddr_un");
-
strcpy(sa_un.sun_path, sock_name);
-
sa_un.sun_family = AF_UNIX;
-
-
debug(LOG_DEBUG, "Binding socket (%s) (%d)", sa_un.sun_path, strlen(sock_name));
-
-
-
if (bind(sock, (struct sockaddr *)&sa_un, strlen(sock_name) + sizeof(sa_un.sun_family))) {
-
debug(LOG_ERR, "Could not bind internal socket: %s", strerror(errno));
-
return;
-
}
-
-
if (listen(sock, 5)) {
-
debug(LOG_ERR, "Could not listen on internal socket: %s", strerror(errno));
-
return;
-
}
-
-
/*
-
* socket建立完成,创建子进程
-
*/
-
debug(LOG_DEBUG, "Forking in preparation for exec()...");
-
pid = safe_fork();
-
if (pid > 0) {
-
/* 父进程 */
-
-
/* 等待子进程连接此socket :*/
-
debug(LOG_DEBUG, "Waiting for child to connect on internal socket");
-
len = sizeof(sa_un);
-
if ((fd = accept(sock, (struct sockaddr *)&sa_un, &len)) == -1){ //接受连接
-
debug(LOG_ERR, "Accept failed on internal socket: %s", strerror(errno));
-
close(sock);
-
return;
-
}
-
-
close(sock);
-
-
debug(LOG_DEBUG, "Received connection from child. Sending them all existing clients");
-
-
/*子进程已经完成连接,发送客户端列表 */
-
LOCK_CLIENT_LIST();
-
client = client_get_first_client(); //获取第一个客户端
-
while (client) {
-
/* Send this client */
-
safe_asprintf(&tempstring, "CLIENT|ip=%s|mac=%s|token=%s|fw_connection_state=%u|fd=%d|counters_incoming=%llu|counters_outgoing=%llu|counters_last_updated=%lun", client->ip, client->mac, client->token, client->fw_connection_state, client->fd, client->counters.incoming, client->counters.outgoing, client->counters.last_updated);
-
debug(LOG_DEBUG, "Sending to child client data: %s", tempstring);
-
len = 0;
-
while (len != strlen(tempstring)) {
-
written = write(fd, (tempstring + len), strlen(tempstring) - len); //发送给子进程
-
if (written == -1) {
-
debug(LOG_ERR, "Failed to write client data to child: %s", strerror(errno));
-
free(tempstring);
-
break;
-
}
-
else {
-
len += written;
-
}
-
}
-
free(tempstring);
-
client = client->next;
-
}
-
UNLOCK_CLIENT_LIST();
-
-
close(fd);
-
-
debug(LOG_INFO, "Sent all existing clients to child. Committing suicide!");
-
-
shutdown(afd, 2);
-
close(afd);
-
-
-
wdctl_stop(afd);
-
}
-
else {
-
/* 子进程,先关闭资源 */
-
close(wdctl_socket_server);
-
close(icmp_fd);
-
close(sock);
-
shutdown(afd, 2);
-
close(afd);
-
debug(LOG_NOTICE, "Re-executing myself (%s)", restartargv[0]);
-
-
setsid();
-
execvp(restartargv[0], restartargv); //执行外部命令,这里重新启动wifidog
-
-
debug(LOG_ERR, "I failed to re-execute myself: %s", strerror(errno));
-
debug(LOG_ERR, "Exiting without cleanup");
-
exit(1);
-
}
-
}
小结
客户端列表只有在restart命令中才会执行,实际上流程就是
-
父wifidog准备socket
-
父wifidog启动子wifidog
-
子wifidog连接父wifidog
-
客户端列表传递
-
子wifidog终止父wifidog
(编辑:PHP编程网 - 黄冈站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|