void do_send_task(int fd, t_task_base *base, t_pfs_sig_head *h) { struct conn *curcon = &acon[fd]; curcon->send_len = 0; char obuf[2048] = {0x0}; pfs_cs_peer *peer = (pfs_cs_peer *) curcon->user; peer->hbtime = time(NULL); uint8_t tmp_status = FILE_SYNC_DST_2_SRC; if (base->fsize == 0 || base->retry) { if (get_localfile_stat(base) != LOCALFILE_OK) { LOG(pfs_data_log, LOG_ERROR, "filename[%s] get_localfile_stat ERROR %m!\n", base->filename); tmp_status = FILE_SYNC_DST_2_SRC_E_OPENFILE; } } if (tmp_status == FILE_SYNC_DST_2_SRC) { t_pfs_tasklist *task = NULL; if (pfs_get_task(&task, TASK_Q_HOME)) { LOG(pfs_data_log, LOG_ERROR, "filename[%s] do_newtask ERROR!\n", base->filename); tmp_status = FILE_SYNC_DST_2_SRC_E_MALLOC; } else { memset(&(task->task), 0, sizeof(task->task)); t_task_sub *sub = &(task->task.sub); memset(sub, 0, sizeof(t_task_sub)); ip2str(sub->peerip, peer->ip); sub->processlen = base->fsize; sub->starttime = time(NULL); sub->oper_type = OPER_GET_RSP; sub->need_sync = TASK_SRC_NOSYNC; memcpy(&(task->task.base), base, sizeof(t_task_base)); int lfd = -1; if (open_localfile_4_read(base, &lfd) != LOCALFILE_OK) { LOG(pfs_data_log, LOG_ERROR, "fd[%d] err open [%s] %m close it\n", fd, base->filename); tmp_status = FILE_SYNC_DST_2_SRC_E_OPENFILE; task->task.base.overstatus = OVER_E_OPEN_SRCFILE; pfs_set_task(task, TASK_Q_FIN); } else { task->task.base.overstatus = OVER_UNKNOWN; pfs_set_task(task, TASK_Q_SEND); peer->sendtask = task; set_client_fd(fd, lfd, 0, sub->processlen); peer->sock_stat = SENDFILEING; } } } int n = create_sig_msg(CMD_GET_FILE_RSP, tmp_status, (t_pfs_sig_body *)base, obuf, sizeof(t_task_base)); set_client_data(fd, obuf, n); peer->headlen = n; }
static void read_commands(struct client *client) { char buf[1024]; int pos = 0; if (!client->authenticated) client->authenticated = addr.sa.sa_family == AF_UNIX; while (1) { int rc, s, i; rc = read(client->fd, buf + pos, sizeof(buf) - pos); if (rc == -1) { if (errno == EINTR) continue; if (errno == EAGAIN) return; goto close; } if (rc == 0) goto close; pos += rc; s = 0; for (i = 0; i < pos; i++) { const char *line, *msg; char *cmd, *arg; int ret; if (buf[i] != '\n') continue; buf[i] = 0; line = buf + s; s = i + 1; if (!client->authenticated) { if (!server_password) { msg = "password is unset, tcp/ip disabled"; d_print("%s\n", msg); ret = send_answer(client->fd, "%s\n\n", msg); goto close; } if (strncmp(line, "passwd ", 7) == 0) line += 7; client->authenticated = !strcmp(line, server_password); if (!client->authenticated) { msg = "authentication failed"; d_print("%s\n", msg); ret = send_answer(client->fd, "%s\n\n", msg); goto close; } ret = write_all(client->fd, "\n", 1); continue; } while (isspace((unsigned char)*line)) line++; if (*line == '/') { int restricted = 0; line++; search_direction = SEARCH_FORWARD; if (*line == '/') { line++; restricted = 1; } search_text(line, restricted, 1); ret = write_all(client->fd, "\n", 1); } else if (*line == '?') { int restricted = 0; line++; search_direction = SEARCH_BACKWARD; if (*line == '?') { line++; restricted = 1; } search_text(line, restricted, 1); ret = write_all(client->fd, "\n", 1); } else if (parse_command(line, &cmd, &arg)) { if (!strcmp(cmd, "status")) { ret = cmd_status(client); } else { if (strcmp(cmd, "passwd") != 0) { set_client_fd(client->fd); run_parsed_command(cmd, arg); set_client_fd(-1); } ret = write_all(client->fd, "\n", 1); } free(cmd); free(arg); } else { // don't hang cmus-remote ret = write_all(client->fd, "\n", 1); } if (ret < 0) { d_print("write: %s\n", strerror(errno)); goto close; } } memmove(buf, buf + s, pos - s); pos -= s; } return; close: close(client->fd); list_del(&client->node); free(client); }