This repository has been archived on 2021-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
uIRCd/src/filesystem.c

80 lines
2.4 KiB
C

/*
* This file is part of uIRCd. (https://git.redxen.eu/caskd/uIRCd)
* Copyright (c) 2019, 2020 Alex-David Denes
*
* uIRCd 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.
*
* uIRCd 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 uIRCd. If not, see <https://www.gnu.org/licenses/>.
*/
#include "filesystem.h"
#include "connection.h"
#include "limits.h"
#include "logging.h"
#include <ctype.h> // isalpha() isdigit()
#include <errno.h> // errno
#include <fcntl.h> // fnctl()
#include <stdbool.h> // bool
#include <stdio.h> // fopen() FILE fprintf()
#include <string.h> // strerror()
#include <sys/stat.h> // mkfifo() mkdir()
#include <sys/types.h> // size_t ssize_t mode_t
#include <syslog.h> // syslog()
int
makeinput(const char* path)
{
if (path == NULL) return -1;
if (mkfifo(path, S_IRUSR | S_IWUSR | S_IWGRP) == 0) {
LOG(LOG_DEBUG, "Created a FIFO pipe for input at \"%s\"", path);
} else if (errno != EEXIST) {
LOG(LOG_WARNING, "Couldn't create FIFO at \"%s\" for input: " ERRNOFMT, path, strerror(errno), errno);
return -1;
}
int fd;
if ((fd = open(path, O_RDONLY | O_NONBLOCK)) == -1) {
LOG(LOG_WARNING, "Couldn't open FIFO pipe \"%s\" for reading: " ERRNOFMT, path, strerror(errno), errno);
} else
LOG(LOG_DEBUG, "Opened FIFO pipe \"%s\" for reading", path);
return fd;
}
bool
write_log(const char* path, const char* message)
{
FILE* logfile;
if ((logfile = fopen(path, "a")) != NULL) {
fprintf(logfile, "%s", message);
LOG(LOG_DEBUG, "Wrote to log \"%s\"", path);
fclose(logfile);
return 1;
} else
LOG(LOG_WARNING, "Couldn't open file \"%s\" for appending: " ERRNOFMT, path, strerror(errno), errno);
return 0;
}
bool
add_socket_flags(int fd, int flags)
{
int cflgs;
if ((cflgs = fcntl(fd, F_GETFL)) != -1) {
if (fcntl(fd, F_SETFL, cflgs | flags) == -1) {
LOG(LOG_WARNING, "Couldn't add socket flags: " ERRNOFMT, strerror(errno), errno);
} else
return 1;
} else
LOG(LOG_WARNING, "Failed to get socket flags: " ERRNOFMT, strerror(errno), errno);
return 0;
}