diff options
Diffstat (limited to 'utils.c')
-rw-r--r-- | utils.c | 172 |
1 files changed, 0 insertions, 172 deletions
diff --git a/utils.c b/utils.c deleted file mode 100644 index 5ffbd5b..0000000 --- a/utils.c +++ /dev/null @@ -1,172 +0,0 @@ -/* - * rhctl - * - * Copyright (C) 2009-2014 Christian Pointner <equinox@helsinki.at> - * - * This file is part of rhctl. - * - * rhctl is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * rhctl is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with rhctl. If not, see <http://www.gnu.org/licenses/>. - */ - -#include "datatypes.h" - -#include <sys/un.h> -#include <termios.h> -#include <unistd.h> -#include <errno.h> - -#include "log.h" - -#include "utils.h" - -int init_command_socket(const char* path) -{ - int fd = socket(AF_UNIX, SOCK_STREAM, 0); - if(fd < 0) { - log_printf(ERROR, "unable to open socket: %s", strerror(errno)); - return -1; - } - - struct sockaddr_un local; - local.sun_family = AF_UNIX; - if(sizeof(local.sun_path) <= strlen(path)) { - log_printf(ERROR, "socket path is to long (max %d)", sizeof(local.sun_path)-1); - return -1; - } - strcpy(local.sun_path, path); - unlink(local.sun_path); - int len = SUN_LEN(&local); - int ret = bind(fd, (struct sockaddr*)&local, len); - if(ret) { - log_printf(ERROR, "unable to bind to '%s': %s", local.sun_path, strerror(errno)); - return -1; - } - - ret = listen(fd, 4); - if(ret) { - log_printf(ERROR, "unable to listen on command socket: %s", local.sun_path, strerror(errno)); - return -1; - } - - log_printf(INFO, "now listening on %s for incoming commands", path); - - return fd; -} - -int connect_command_socket(const char* path) -{ - int fd = socket(AF_UNIX, SOCK_STREAM, 0); - if(fd < 0) { - log_printf(ERROR, "unable to open socket: %s", strerror(errno)); - return -1; - } - - struct sockaddr_un remote; - remote.sun_family = AF_UNIX; - if(sizeof(remote.sun_path) <= strlen(path)) { - log_printf(ERROR, "socket path is to long (max %d)", sizeof(remote.sun_path)-1); - return -1; - } - strcpy(remote.sun_path, path); - int len = SUN_LEN(&remote); - int ret = connect(fd, (struct sockaddr*)&remote, len); - if(ret) { - log_printf(ERROR, "unable to connect to '%s': %s", remote.sun_path, strerror(errno)); - return -1; - } - - return fd; -} - -int send_string(int fd, const char* string) -{ - int len = strlen(string); - int offset = 0; - int ret; - for(;;) { - ret = write(fd, &string[offset], len - offset); - if(ret < 0) { - if(errno != EINTR) - return ret; - - ret = 0; - } - - offset += ret; - if(offset+1 >= len) - break; - } - return ret; -} - -int setup_tty(int fd, speed_t speed) -{ - struct termios tmio; - - int ret = tcgetattr(fd, &tmio); - if(ret) { - log_printf(ERROR, "Error on tcgetattr(): %s", strerror(errno)); - return ret; - } - - tmio.c_iflag &= ~(INLCR | ICRNL | IGNCR | IXON | IXOFF); - tmio.c_oflag &= ~(ONLCR | OCRNL | ONOCR | ONLRET); - tmio.c_cflag |= CS8 | CLOCAL | CREAD; - tmio.c_cflag &= ~(CSTOPB | PARENB); - tmio.c_lflag &= ~(ICANON | ECHO); - tmio.c_cc[VTIME] = 0; - tmio.c_cc[VMIN] = 1; - - ret = cfsetospeed(&tmio, speed); - if(ret) { - log_printf(ERROR, "Error on cfsetospeed(): %s", strerror(errno)); - return ret; - } - - ret = cfsetispeed(&tmio, speed); - if(ret) { - log_printf(ERROR, "Error on cfsetispeed(): %s", strerror(errno)); - return ret; - } - - ret = tcsetattr(fd, TCSANOW, &tmio); - if(ret) { - log_printf(ERROR, "Error on tcsetattr(): %s", strerror(errno)); - return ret; - } - - ret = tcflush(fd, TCIFLUSH); - if(ret) { - log_printf(ERROR, "Error on tcflush(): %s", strerror(errno)); - return ret; - } - - fd_set fds; - struct timeval tv; - FD_ZERO(&fds); - FD_SET(fd, &fds); - tv.tv_sec = 0; - tv.tv_usec = 50000; - for(;;) { - ret = select(fd+1, &fds, NULL, NULL, &tv); - if(ret > 0) { - char buffer[100]; - ret = read(fd, buffer, sizeof(buffer)); - } - else - break; - } - - return 0; -} |