Compare commits

..

No commits in common. "35fc4420e7ded3182892183aacb54d958bcba1dd" and "048758e4c4e71680c968c8fa121d723ba7171f85" have entirely different histories.

3 changed files with 174 additions and 60 deletions

View file

@ -1,41 +1,65 @@
# SRC = source
# DST = destination
# *D = folder containing *
NAME := qemush NAME := qemush
# qemu Unix user
QEMU_USER := qemu QEMU_USER := qemu
# sudo as qemu
SUDO_QEMU := sudo -u $(QEMU_USER)
SRC_BIN := bin/$(NAME) QEMUSH := /usr/local/bin/$(NAME)
DST_BIN := /usr/local/$(SRC_BIN) SRC_QEMUSH := bin/$(NAME)
SRCD := src
SRC_HOME := $(QEMU_USER) # This repo's equivalent of effective qemu Unix user home
DST_HOME := $(shell echo ~$(QEMU_USER)) SRC_QEMU_HOME := qemu
SRC_QEMU_BIN := $(SRC_QEMU_HOME)/bin
SRC_LAUNCHERSD := $(SRC_QEMU_HOME)/launchers
# Actual qemu Unix user home
QEMU_HOME := $(shell echo ~$(QEMU_USER))
QEMU_BIN := $(QEMU_HOME)/bin
LAUNCHERSD := $(QEMU_HOME)/launchers
SRC_BIND := $(SRC_HOME)/bin # Names of all qemush modules
SRC_LAUNCHERSD := $(SRC_HOME)/launchers SRC_MODULES_NAMES := $(notdir $(wildcard $(SRC_QEMU_BIN)/*))
DST_BIND := $(DST_HOME)/bin # Target location of modules
DST_LAUNCHERSD := $(DST_HOME)/launchers MODULES := $(addprefix $(QEMU_BIN)/,$(SRC_MODULES_NAMES))
# Names of launchers scripts
SRC_LAUNCHERS_NAMES := $(notdir $(wildcard $(SRC_LAUNCHERSD)/*))
# Target location of launchers
LAUNCHERS := $(addprefix $(LAUNCHERSD)/,$(SRC_LAUNCHERS_NAMES))
# Mode to apply
BINS_MODE := 740
MODULES_NAMES := $(notdir $(wildcard $(SRC_BIND)/*)) # Compiler options
LAUNCHERS_NAMES := $(notdir $(wildcard $(SRC_LAUNCHERSD)/*)) CC := cc
CC_OPTIONS = -O2
# C sources file format
SRC_FMT := .c
# Names of C programs to compile
SRC_C_SOURCES_NAMES := $(notdir $(basename $(wildcard $(SRCD)/*$(SRC_FMT))))
# Location of C binaries
C_BINARIES := $(addprefix $(QEMU_BIN)/,$(SRC_C_SOURCES_NAMES))
DST_MODULES := $(addprefix $(DST_BIND)/,$(MODULES_NAMES)) # Directories in ~qemu necessary for qemush to work
DST_LAUNCHERS := $(addprefix $(DST_LAUNCHERSD)/,$(LAUNCHERS_NAMES)) QEMUSH_DIRS_NAMES := bin launchers disks sockets/monitors sockets/spice
QEMUSH_DIRS := $(addprefix $(QEMU_HOME)/,$(QEMUSH_DIRS_NAMES))
QEMUSH_DIRS := $(addprefix $(DST_HOME)/,bin launchers disks sockets/monitors sockets/spice)
.PHONY: install .PHONY: install
install: $(QEMUSH_DIRS) $(DST_MODULES) $(DST_BIN) $(DST_LAUNCHERS) install: $(QEMUSH_DIRS) $(MODULES) $(C_BINARIES) $(QEMUSH) $(LAUNCHERS)
$(QEMUSH_DIRS): $(QEMUSH_DIRS):
sudo -u $(QEMU_USER) mkdir -m 750 -p -- $@ $(SUDO_QEMU) mkdir -p $@
$(SUDO_QEMU) chmod 750 $@
$(DST_BIN): $(SRC_BIN) $(QEMUSH): $(SRC_QEMUSH)
sudo install -m 755 -- $^ $@ sudo install -m 755 $^ $@
$(DST_MODULES): $(DST_HOME)%: $(SRC_HOME)% $(MODULES): $(QEMU_HOME)%: $(SRC_QEMU_HOME)%
sudo -u $(QEMU_USER) install -m 740 -- $^ $@ $(SUDO_QEMU) install -m $(BINS_MODE) $^ $@
$(LAUNCHERS): $(DST_LAUNCHERSD)%: $(SRC_LAUNCHERSD)% $(C_BINARIES): $(QEMU_BIN)%: $(SRCD)%$(SRC_FMT)
sudo -u $(QEMU_USER) install -m 740 -- $^ $@ $(SUDO_QEMU) $(CC) $(CC_OPTIONS) -o $@ $^
$(SUDO_QEMU) chmod $(BINS_MODE) $@
$(LAUNCHERS): $(LAUNCHERSD)%: $(SRC_LAUNCHERSD)%
$(SUDO_QEMU) install -m $(BINS_MODE) $^ $@

View file

@ -1,34 +0,0 @@
#!/usr/bin/env sh
#set -x
name=$(basename "$0")
used_tcp_ports=$(sed 1d /proc/net/tcp \
| awk '($4 == "0A") { print $2 }' \
| awk -F : '($1 == "00000000" || $1 == "0100007F") { print $2 }' \
| perl -e 'foreach my $line (<>) { print hex($line) . "\n"; }'
)
perror() {
>&2 printf "%s: %s\n" "$name" "$*"
}
port_is_free() {
for used_port in $used_tcp_ports; do
[ "$port" = "$used_port" ] && return 1
done
:
}
port=$1
if ! { [ "$port" -gt 0 ] && [ "$port" -lt 65536 ]; } 2> /dev/null; then
perror "you must specify a valid TCP port"
exit 1
fi
while ! port_is_free; do
port=$((port + 1))
done
printf %s\\n "$port"

124
src/first-free-port.c Normal file
View file

@ -0,0 +1,124 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sysexits.h>
#include <string.h>
#include <errno.h>
#define MIN_TCP_PORT 1
#define TCP_TABLE_PATH "/proc/net/tcp"
#define TCP_TABLE_LINE_LENGTH 151
#define LOCALHOSTIP_HEX "0100007F"
#define WILDCARDIP_HEX "00000000"
#define LISTENING_HEX "0A"
#define BLOCKS_TO_ALLOC 4
/**
* Print a nice error message
*/
static void print_error(char *name, const char *message) {
fprintf(stderr, "%s: %s\n", name, message);
}
/**
* Get all the listening TCP ports
*/
static unsigned short *get_listening_ports(FILE *tcp_table_fptr) {
unsigned short *listening_ports = malloc(BLOCKS_TO_ALLOC * sizeof(short));
char line[TCP_TABLE_LINE_LENGTH];
char delimiter[] = " ";
unsigned short len = 0;
char *address;
char *state;
char *field;
size_t blocks_allocated = BLOCKS_TO_ALLOC;
// Skip first line (header)
fgets(line, sizeof(line), tcp_table_fptr);
// Tokenize lines one by one
while (fgets(line, sizeof(line), tcp_table_fptr) != NULL) {
strtok(line, delimiter);
address = strtok(NULL, delimiter);
strtok(NULL, delimiter);
state = strtok(NULL, delimiter);
field = strtok(NULL, delimiter);
while (field != NULL) {
field = strtok(NULL, delimiter);
}
if ((!strncmp(address, LOCALHOSTIP_HEX, 8) || !strncmp(address, WILDCARDIP_HEX, 8)) && !strncmp(state, LISTENING_HEX, 2)) {
if (len == blocks_allocated) {
blocks_allocated = blocks_allocated + BLOCKS_TO_ALLOC;
listening_ports = realloc(listening_ports, blocks_allocated * sizeof(short));
}
listening_ports[len] = strtol(address + strlen(address) - 4, NULL, 16);
len++;
}
}
if (len == blocks_allocated) {
listening_ports = realloc(listening_ports, blocks_allocated + 1);
}
listening_ports[len] = 0;
return listening_ports;
}
static unsigned char is_port_available(const unsigned short port, const unsigned short *listening_ports) {
unsigned short i = 0;
while (listening_ports[i] != 0 && listening_ports[i] != port) {
i++;
}
return listening_ports[i] == 0;
}
static inline unsigned char is_valid_tcp_port(const unsigned short tcp_port) {
return tcp_port >= MIN_TCP_PORT;
}
int main(int argc, char *argv[]) {
unsigned short current_port;
if (argc >= 2) {
current_port = atoi(argv[1]);
} else {
current_port = 0;
}
if (!is_valid_tcp_port(current_port)) {
print_error(argv[0], "provide a valid TCP port number as first argument.");
return EX_USAGE;
}
// Open TCP table
FILE *tcp_table_fptr = fopen(TCP_TABLE_PATH, "r");
if (tcp_table_fptr == NULL) {
print_error(argv[0], "error opening the TCP table.");
return errno;
}
unsigned short *listening_ports = get_listening_ports(tcp_table_fptr);
if (fclose(tcp_table_fptr) != 0) {
print_error(argv[0], "can't close the TCP table.");
return errno;
}
// Check if the current port is available, add
while (!is_port_available(current_port, listening_ports)) {
current_port++;
}
free(listening_ports);
if (is_valid_tcp_port(current_port)) {
printf("%d\n", current_port);
return EXIT_SUCCESS;
} else {
print_error(argv[0], "no more ports available; how did you fuck up that bad ???");
return EX_TEMPFAIL;
}
}