cosmetics

This commit is contained in:
John Törnblom committed 2025-10-30 20:54:42 +01:00
1 parent 8366ce37c5
commit aed2106e3b
9 files changed
+142 -89

No files matched your search

+1 -1
View File
@@ -21,7 +21,7 @@ CFLAGS := -Wall -Werror -DVERSION_TAG=\"$(VERSION_TAG)\"
all: $(ELF)
$(ELF): main_posix.c srv.c cmd_common.c cmd_posix.c notify.c
$(ELF): main_posix.c io.c srv.c cmd_common.c cmd_posix.c notify.c
$(CC) $(CFLAGS) -o $@ $^
clean:
+1 -1
View File
@@ -30,7 +30,7 @@ CFLAGS := -Wall -Werror -DVERSION_TAG=\"$(VERSION_TAG)\"
all: $(ELF)
$(ELF): main_ps4.c srv.c cmd_common.c cmd_ps4.c notify.c
$(ELF): main_ps4.c io.c srv.c cmd_common.c cmd_ps4.c notify.c
$(CC) $(CFLAGS) -o $@ $^
clean:
+1 -1
View File
@@ -30,7 +30,7 @@ CFLAGS := -Wall -Werror -DVERSION_TAG=\"$(VERSION_TAG)\"
all: $(ELF)
$(ELF): main_ps5.c srv.c cmd_common.c cmd_ps5.c notify.c
$(ELF): main_ps5.c io.c srv.c cmd_common.c cmd_ps5.c notify.c
$(CC) $(CFLAGS) -o $@ $^
clean:
+2 -2
View File
@@ -31,6 +31,7 @@ along with this program; see the file COPYING. If not, see
#include <unistd.h>
#include "cmd.h"
#include "io.h"
#include "log.h"
@@ -138,7 +139,7 @@ ftp_active_printf(ftp_env_t *env, const char *fmt, ...) {
len = strlen(buf);
if(write(env->active_fd, buf, len) != len) {
if(io_nwrite(env->active_fd, buf, len)) {
return -1;
}
@@ -529,7 +530,6 @@ ftp_cmd_RETR(ftp_env_t *env, const char* arg) {
}
close(fd);
if(ftp_data_close(env)) {
return ftp_perror(env);
}
+13 -82
View File
@@ -23,11 +23,11 @@ along with this program; see the file COPYING. If not, see
#include <sys/_iovec.h>
#include <sys/mount.h>
#include <sys/syscall.h>
#include <sys/mman.h>
#include <sys/errno.h>
#include "cmd.h"
#include "io.h"
/**
@@ -87,75 +87,6 @@ typedef struct self_entry {
} self_entry_t;
/**
* Read exactly N bytes from the given file descriptor.
**/
static int
nread(int fd, void* buf, size_t n) {
int r;
if((r=read(fd, buf, n)) < 0) {
return -1;
}
if(r != n) {
errno = EIO;
return -1;
}
return 0;
}
/**
* Write exactly N bytes to the given file descriptor.
**/
static int
nwrite(int fd, const void* buf, size_t n) {
int r;
if((r=write(fd, buf, n)) < 0) {
return -1;
}
if(r != n) {
errno = EIO;
return -1;
}
return 0;
}
/**
* Copy exactly N bytes from one file descriptor to another.
**/
static int
ncopy(int fd_in, int fd_out, size_t size) {
size_t copied = 0;
char buf[0x1000];
ssize_t n;
while(copied < size) {
n = size - copied;
if(n > sizeof(buf)) {
n = sizeof(buf);
}
if(nread(fd_in, buf, n)) {
return -1;
}
if(nwrite(fd_out, buf, n)) {
return -1;
}
copied += n;
}
return 0;
}
/**
* Decrypt and copy an ELF segment.
**/
@@ -181,7 +112,7 @@ decrypt_segment(int self_fd, int elf_fd, const Elf64_Phdr* phdr, size_t ind) {
// ensure kernel is mapping the segments
memcpy(&tmp, data, 1);
if(nwrite(elf_fd, data, phdr->p_filesz)) {
if(io_nwrite(elf_fd, data, phdr->p_filesz)) {
munmap(data, phdr->p_filesz);
return -1;
}
@@ -222,7 +153,7 @@ copy_segment(int from_fd, off_t from_start, int to_fd, off_t to_start,
return -1;
}
if(ncopy(from_fd, to_fd, size)) {
if(io_ncopy(from_fd, to_fd, size)) {
return -1;
}
@@ -257,7 +188,7 @@ self2elf(const char* self_path, const char* elf_path) {
}
// Read the SELF header
if(nread(self_fd, &head, sizeof(head))) {
if(io_nread(self_fd, &head, sizeof(head))) {
close(self_fd);
return -1;
}
@@ -275,7 +206,7 @@ self2elf(const char* self_path, const char* elf_path) {
close(self_fd);
return -1;
}
if(nread(self_fd, entries, head.num_entries * sizeof(self_entry_t))) {
if(io_nread(self_fd, entries, head.num_entries * sizeof(self_entry_t))) {
close(self_fd);
free(entries);
return -1;
@@ -283,7 +214,7 @@ self2elf(const char* self_path, const char* elf_path) {
// Read the ELF header bundled within the SELF file
elf_off = lseek(self_fd, 0, SEEK_CUR);
if(nread(self_fd, &ehdr, sizeof(ehdr))) {
if(io_nread(self_fd, &ehdr, sizeof(ehdr))) {
close(self_fd);
free(entries);
return -1;
@@ -320,7 +251,7 @@ self2elf(const char* self_path, const char* elf_path) {
}
// Write the ELF header
if(nwrite(elf_fd, &ehdr, sizeof(ehdr))) {
if(io_nwrite(elf_fd, &ehdr, sizeof(ehdr))) {
close(self_fd);
close(elf_fd);
free(entries);
@@ -329,13 +260,13 @@ self2elf(const char* self_path, const char* elf_path) {
// Enumerate ELF program headers
for(int i=0; i<ehdr.e_phnum; i++) {
if(nread(self_fd, &phdr, sizeof(phdr))) {
if(io_nread(self_fd, &phdr, sizeof(phdr))) {
close(self_fd);
close(elf_fd);
free(entries);
return -1;
}
if(nwrite(elf_fd, &phdr, sizeof(phdr))) {
if(io_nwrite(elf_fd, &phdr, sizeof(phdr))) {
close(self_fd);
close(elf_fd);
free(entries);
@@ -409,7 +340,7 @@ is_self(const char* path) {
if((fd=open(path, O_RDONLY, 0)) < 0) {
r = 0;
} else if(nread(fd, &head, sizeof(head))) {
} else if(io_nread(fd, &head, sizeof(head))) {
r = 0;
} else if(head.magic[0] != 0x4f || head.magic[1] != 0x15 ||
@@ -419,7 +350,7 @@ is_self(const char* path) {
} else if(lseek(fd, head.num_entries * sizeof(self_entry_t), SEEK_CUR) < 0) {
r = 0;
} else if(nread(fd, &ehdr, sizeof(ehdr))) {
} else if(io_nread(fd, &ehdr, sizeof(ehdr))) {
r = 0;
} else if(ehdr.e_ident[0] != 0x7f || ehdr.e_ident[1] != 'E' ||
@@ -461,11 +392,11 @@ ftp_cmd_MTRW(ftp_env_t *env, const char* arg) {
IOVEC_ENTRY("ignoreacl"), IOVEC_ENTRY(NULL),
};
if(syscall(SYS_nmount, iov_sys, IOVEC_SIZE(iov_sys), MNT_UPDATE)) {
if(nmount(iov_sys, IOVEC_SIZE(iov_sys), MNT_UPDATE)) {
return ftp_perror(env);
}
if(syscall(SYS_nmount, iov_sysex, IOVEC_SIZE(iov_sysex), MNT_UPDATE)) {
if(nmount(iov_sysex, IOVEC_SIZE(iov_sysex), MNT_UPDATE)) {
return ftp_perror(env);
}
+1 -1
View File
@@ -69,7 +69,7 @@ ftp_cmd_MTRW(ftp_env_t *env, const char* arg) {
**/
int
ftp_cmd_RETR_SELF2ELF(ftp_env_t *env, const char* arg) {
// SELF decryption is currently not supported, allways send the entire SELF file.
// SELF decryption is currently not supported, always send the entire SELF file.
return ftp_cmd_RETR(env, arg);
}
+83
View File
@@ -0,0 +1,83 @@
/* Copyright (C) 2025 John Törnblom
This program 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, or (at your option) any
later version.
This program 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 this program; see the file COPYING. If not, see
<http://www.gnu.org/licenses/>. */
#include <errno.h>
#include <unistd.h>
#include <sys/errno.h>
#include "io.h"
int
io_nread(int fd, void* buf, size_t n) {
int r;
if((r=read(fd, buf, n)) < 0) {
return -1;
}
if(r != n) {
errno = EIO;
return -1;
}
return 0;
}
int
io_nwrite(int fd, const void* buf, size_t n) {
int r;
if((r=write(fd, buf, n)) < 0) {
return -1;
}
if(r != n) {
errno = EIO;
return -1;
}
return 0;
}
int
io_ncopy(int fd_in, int fd_out, size_t size) {
size_t copied = 0;
char buf[0x4000];
ssize_t n;
while(copied < size) {
n = size - copied;
if(n > sizeof(buf)) {
n = sizeof(buf);
}
if(io_nread(fd_in, buf, n)) {
return -1;
}
if(io_nwrite(fd_out, buf, n)) {
return -1;
}
copied += n;
}
return 0;
}
+38
View File
@@ -0,0 +1,38 @@
/* Copyright (C) 2025 John Törnblom
This program 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, or (at your option) any
later version.
This program 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 this program; see the file COPYING. If not, see
<http://www.gnu.org/licenses/>. */
#pragma once
#include <stddef.h>
/**
* Read exactly N bytes from the given file descriptor.
**/
int io_nread(int fd, void* buf, size_t n);
/**
* Write exactly N bytes to the given file descriptor.
**/
int io_nwrite(int fd, const void* buf, size_t n);
/**
* Copy exactly N bytes from one file descriptor to another.
**/
int io_ncopy(int fd_in, int fd_out, size_t size);
+2 -1
View File
@@ -27,6 +27,7 @@ along with this program; see the file COPYING. If not, see
#include <string.h>
#include "cmd.h"
#include "io.h"
#include "log.h"
#include "notify.h"
@@ -187,7 +188,7 @@ ftp_greet(ftp_env_t *env) {
strncat(msg, "220 Service is ready\r\n", sizeof(msg)-1);
len = strlen(msg);
if(write(env->active_fd, msg, len) != len) {
if(io_nwrite(env->active_fd, msg, len)) {
return -1;
}