/* * -1 - Internal error (can't alloc memory) * 0 - No error * 1 - IPC returned error * 2 - Unknown status line */ static int read_ipc_reply(FILE *f) { struct dynar read_str; int ch; int status_readed; int res; static const char *ok_str = "OK"; static const char *err_str = "Error"; int err_set; char c; dynar_init(&read_str, IPC_READ_BUF_SIZE); status_readed = 0; err_set = 0; res = 0; while ((ch = fgetc(f)) != EOF) { if (status_readed) { putc(ch, (err_set ? stderr : stdout)); } else { if (ch == '\r') { } else if (ch == '\n') { status_readed = 1; c = '\0'; if (dynar_cat(&read_str, &c, sizeof(c)) != 0) { res = -1; goto exit_destroy; } if (strcasecmp(dynar_data(&read_str), ok_str) == 0) { } else if (strcasecmp(dynar_data(&read_str), err_str) == 0) { err_set = 1; res = 1; fprintf(stderr, "Error: "); } else { res = 2; goto exit_destroy; } } else { c = ch; if (dynar_cat(&read_str, &c, sizeof(c)) != 0) { res = -1; goto exit_destroy; } } } } exit_destroy: dynar_destroy(&read_str); return (res); }
/* * 1 Full line readed * 0 Partial read (no error) * -1 End of connection * -2 Buffer too long * -3 Unhandled error */ int unix_socket_client_io_read(struct unix_socket_client *client) { char buf[UNIX_SOCKET_CLIENT_BUFFER]; ssize_t readed; int res; size_t zi; res = 0; readed = unix_socket_read(client->socket, buf, sizeof(buf)); if (readed > 0) { client->msg_already_received_bytes += readed; if (dynar_cat(&client->receive_buffer, buf, readed) == -1) { res = -2; goto exit_err; } for (zi = 0; zi < (size_t)readed; zi++) { if (buf[zi] == '\n') { res = 1; } } } if (readed == 0) { res = -1; } if (readed < 0 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) { res = -3; } exit_err: return (res); }
static int store_command(struct dynar *str, enum qdevice_tool_operation operation, int verbose) { const char *nline = "\n\0"; const int nline_len = 2; switch (operation) { case QDEVICE_TOOL_OPERATION_NONE: errx(QDEVICE_TOOL_EXIT_CODE_INTERNAL_ERROR, "Unhandled operation none"); break; case QDEVICE_TOOL_OPERATION_SHUTDOWN: if (dynar_str_cat(str, "shutdown ") != 0) { return (-1); } break; case QDEVICE_TOOL_OPERATION_STATUS: if (dynar_str_cat(str, "status ") != 0) { return (-1); } break; } if (verbose) { if (dynar_str_cat(str, "verbose ") != 0) { return (-1); } } if (dynar_cat(str, nline, nline_len) != 0) { return (-1); } return (0); }
static int store_command(struct dynar *str, enum qnetd_tool_operation operation, int verbose, const char *cluster_name) { const char *nline = "\n\0"; const int nline_len = 2; switch (operation) { case QNETD_TOOL_OPERATION_NONE: errx(QNETD_TOOL_EXIT_CODE_INTERNAL_ERROR, "Unhandled operation none"); break; case QNETD_TOOL_OPERATION_SHUTDOWN: if (dynar_str_cat(str, "shutdown ") != 0) { return (-1); } break; case QNETD_TOOL_OPERATION_STATUS: if (dynar_str_cat(str, "status ") != 0) { return (-1); } break; case QNETD_TOOL_OPERATION_LIST: if (dynar_str_cat(str, "list ") != 0) { return (-1); } break; } if (verbose) { if (dynar_str_cat(str, "verbose ") != 0) { return (-1); } } if (cluster_name != NULL) { if (dynar_str_cat(str, "cluster ") != 0 || dynar_str_quote_cat(str, cluster_name) != 0 || dynar_str_cat(str, " ") != 0) { return (-1); } } if (dynar_cat(str, nline, nline_len) != 0) { return (-1); } return (0); }