Compare commits
3 commits
ad45725642
...
9a3462a2ba
Author | SHA1 | Date | |
---|---|---|---|
|
9a3462a2ba | ||
|
1da2ba12b6 | ||
|
ab5148150c |
8 changed files with 160 additions and 6 deletions
|
@ -16,13 +16,9 @@ exec_as qemu "$@"
|
||||||
|
|
||||||
bin=bin
|
bin=bin
|
||||||
images=images
|
images=images
|
||||||
if [ -t 1 ]; then
|
|
||||||
cat=src-hilite-lesspipe.sh
|
|
||||||
else
|
|
||||||
cat='cat'
|
|
||||||
fi
|
|
||||||
PATH="./${bin}:${PATH}"
|
PATH="./${bin}:${PATH}"
|
||||||
EDITOR="${EDITOR:-nvim}"
|
EDITOR="${EDITOR:-nvim}"
|
||||||
|
[ -t 1 ] && alias cat='source-highlight -o STDOUT -f esc'
|
||||||
alias ls='ls --color=auto'
|
alias ls='ls --color=auto'
|
||||||
alias exec='exec '
|
alias exec='exec '
|
||||||
shopt -s expand_aliases
|
shopt -s expand_aliases
|
||||||
|
@ -109,7 +105,7 @@ public_shell() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public_cat() {
|
public_cat() {
|
||||||
exec "$cat" "${bin}/${1}"
|
exec cat "${bin}/${1}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function="$1"
|
function="$1"
|
||||||
|
|
8
qemu/bin/linux
Executable file
8
qemu/bin/linux
Executable file
|
@ -0,0 +1,8 @@
|
||||||
|
#!/bin/sh -x
|
||||||
|
qemu-system-x86_64 \
|
||||||
|
-enable-kvm \
|
||||||
|
-M q35 \
|
||||||
|
-cpu host -smp "$(("$(nproc)" / 2))" \
|
||||||
|
-m "$(("$(free | awk '($1 == "Mem:") { print $2 }')" / 2))K" \
|
||||||
|
-net nic \
|
||||||
|
"$@"
|
8
qemu/bin/linux-spice
Executable file
8
qemu/bin/linux-spice
Executable file
|
@ -0,0 +1,8 @@
|
||||||
|
#!/bin/sh -x
|
||||||
|
exec linux \
|
||||||
|
-vga qxl \
|
||||||
|
-device virtio-serial \
|
||||||
|
-chardev spicevmc,id=vdagent,debug=0,name=vdagent \
|
||||||
|
-device virtserialport,chardev=vdagent,name=com.redhat.spice.0 \
|
||||||
|
-spice port="$(first-free-port 5900)",addr=127.0.0.1,disable-ticketing=on \
|
||||||
|
"$@"
|
10
qemu/bin/redox
Executable file
10
qemu/bin/redox
Executable file
|
@ -0,0 +1,10 @@
|
||||||
|
#!/bin/sh -x
|
||||||
|
exec qemu-system-x86_64 \
|
||||||
|
-enable-kvm \
|
||||||
|
-M q35 \
|
||||||
|
-cpu host -smp "$(("$(nproc)" / 2))" \
|
||||||
|
-m "$(("$(free | grep '^Mem:\s' | awk '{ print $NF }')" / 2))K" \
|
||||||
|
-device e1000,netdev=net0 \
|
||||||
|
-device nec-usb-xhci,id=xhci \
|
||||||
|
-device ich9-intel-hda -device hda-duplex \
|
||||||
|
"$@"
|
4
qemu/bin/redox-spice
Executable file
4
qemu/bin/redox-spice
Executable file
|
@ -0,0 +1,4 @@
|
||||||
|
#!/bin/sh -x
|
||||||
|
exec redox \
|
||||||
|
-vga qxl -spice port="$(first-free-port 5900)",addr=127.0.0.1,disable-ticketing=on \
|
||||||
|
"$@"
|
9
qemu/bin/windows
Executable file
9
qemu/bin/windows
Executable file
|
@ -0,0 +1,9 @@
|
||||||
|
#!/bin/sh -x
|
||||||
|
qemu-system-x86_64 \
|
||||||
|
-enable-kvm \
|
||||||
|
-cpu host -smp "$(("$(nproc)" / 2))" \
|
||||||
|
-m "$(("$(free | grep '^Mem: ' | awk '{ print $NF }')" / 2))K" \
|
||||||
|
-net nic \
|
||||||
|
-device virtio-serial \
|
||||||
|
-usbdevice tablet \
|
||||||
|
"$@"
|
7
qemu/bin/windows-spice
Executable file
7
qemu/bin/windows-spice
Executable file
|
@ -0,0 +1,7 @@
|
||||||
|
#!/bin/sh -x
|
||||||
|
exec windows \
|
||||||
|
-vga qxl \
|
||||||
|
-spice port="$(first-free-port 5900)",addr=127.0.0.1,disable-ticketing=on \
|
||||||
|
-chardev spicevmc,id=vdagent,name=vdagent \
|
||||||
|
-device virtserialport,chardev=vdagent,name=com.redhat.spice.0 \
|
||||||
|
"$@"
|
112
src/first-free-port.c
Normal file
112
src/first-free-port.c
Normal file
|
@ -0,0 +1,112 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sysexits.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define MIN_TCP_PORT 1
|
||||||
|
#define TCP_TABLE "/proc/net/tcp"
|
||||||
|
#define TCP_TABLE_LINE_LENGTH 151
|
||||||
|
#define LOCALHOST_HEX "0100007F"
|
||||||
|
#define WILDCARD_HEX "00000000"
|
||||||
|
#define LISTENING_HEX "0A"
|
||||||
|
#define PORTS_BLOCKS_TO_ALLOW 4
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all the listening TCP ports
|
||||||
|
*/
|
||||||
|
static unsigned short *get_listening_ports(unsigned short *listening_ports, FILE *tcp_table_fptr) {
|
||||||
|
char line[TCP_TABLE_LINE_LENGTH];
|
||||||
|
char delimiter[] = " ";
|
||||||
|
unsigned short len = 0;
|
||||||
|
char *address;
|
||||||
|
char *state;
|
||||||
|
char *field;
|
||||||
|
size_t allowed_for_ports = PORTS_BLOCKS_TO_ALLOW;
|
||||||
|
|
||||||
|
// 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, LOCALHOST_HEX, 8) || !strncmp(address, WILDCARD_HEX, 8)) && !strncmp(state, LISTENING_HEX, 2)) {
|
||||||
|
if (len == allowed_for_ports) {
|
||||||
|
allowed_for_ports = allowed_for_ports + PORTS_BLOCKS_TO_ALLOW;
|
||||||
|
listening_ports = realloc(listening_ports, allowed_for_ports * sizeof(unsigned short));
|
||||||
|
}
|
||||||
|
listening_ports[len] = strtol(address + strlen(address) - 4, NULL, 16);
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len == allowed_for_ports) {
|
||||||
|
allowed_for_ports = allowed_for_ports + PORTS_BLOCKS_TO_ALLOW;
|
||||||
|
listening_ports = realloc(listening_ports, allowed_for_ports * sizeof(unsigned short));
|
||||||
|
}
|
||||||
|
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)) {
|
||||||
|
fprintf(stderr, "Provide a valid TCP port number as first argument.\n");
|
||||||
|
return EX_USAGE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open TCP table
|
||||||
|
FILE *tcp_table_fptr = fopen(TCP_TABLE, "r");
|
||||||
|
if (tcp_table_fptr == NULL) {
|
||||||
|
fprintf(stderr, "Error opening the TCP table.\n");
|
||||||
|
return EX_OSFILE;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned short *listening_ports = malloc(PORTS_BLOCKS_TO_ALLOW * sizeof(unsigned short));
|
||||||
|
listening_ports = get_listening_ports(listening_ports, tcp_table_fptr);
|
||||||
|
|
||||||
|
// 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 {
|
||||||
|
fprintf(stderr, "No more ports available. How did you fuck up that bad ???\n");
|
||||||
|
return EX_TEMPFAIL;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue