mirror of
https://github.com/schoebel/mars
synced 2025-04-17 04:36:06 +00:00
infra: make string allocation fully dynamic
This commit is contained in:
parent
43e4312c1a
commit
6050b4157f
@ -157,6 +157,8 @@ EXPORT_SYMBOL_GPL(mars_reset_emergency);
|
|||||||
int mars_keep_msg = 10;
|
int mars_keep_msg = 10;
|
||||||
EXPORT_SYMBOL_GPL(mars_keep_msg);
|
EXPORT_SYMBOL_GPL(mars_keep_msg);
|
||||||
|
|
||||||
|
#define MARS_SYMLINK_MAX 1023
|
||||||
|
|
||||||
struct key_value_pair {
|
struct key_value_pair {
|
||||||
const char *key;
|
const char *key;
|
||||||
char *val;
|
char *val;
|
||||||
|
@ -8,8 +8,6 @@
|
|||||||
#include "../mars.h"
|
#include "../mars.h"
|
||||||
|
|
||||||
#define MARS_ARGV_MAX 4
|
#define MARS_ARGV_MAX 4
|
||||||
#define MARS_PATH_MAX 256
|
|
||||||
#define MARS_SYMLINK_MAX 1023
|
|
||||||
|
|
||||||
extern loff_t global_total_space;
|
extern loff_t global_total_space;
|
||||||
extern loff_t global_remaining_space;
|
extern loff_t global_remaining_space;
|
||||||
|
@ -65,6 +65,20 @@ EXPORT_SYMBOL_GPL(mars_dent_meta);
|
|||||||
|
|
||||||
// some helpers
|
// some helpers
|
||||||
|
|
||||||
|
static inline
|
||||||
|
int _length_paranoia(int len, int line)
|
||||||
|
{
|
||||||
|
if (unlikely(len < 0)) {
|
||||||
|
MARS_ERR("implausible string length %d (line=%d)\n", len, line);
|
||||||
|
len = PAGE_SIZE - 2;
|
||||||
|
} else if (unlikely(len > PAGE_SIZE - 2)) {
|
||||||
|
MARS_WRN("string length %d will be truncated to %d (line=%d)\n",
|
||||||
|
len, (int)PAGE_SIZE - 2, line);
|
||||||
|
len = PAGE_SIZE - 2;
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
int mars_stat(const char *path, struct kstat *stat, bool use_lstat)
|
int mars_stat(const char *path, struct kstat *stat, bool use_lstat)
|
||||||
{
|
{
|
||||||
mm_segment_t oldfs;
|
mm_segment_t oldfs;
|
||||||
@ -549,6 +563,7 @@ int get_inode(char *newpath, struct mars_dent *dent)
|
|||||||
status = -EINVAL;
|
status = -EINVAL;
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
len = _length_paranoia(len, __LINE__);
|
||||||
|
|
||||||
status = user_path_at(AT_FDCWD, newpath, 0, &path);
|
status = user_path_at(AT_FDCWD, newpath, 0, &path);
|
||||||
if (unlikely(status < 0)) {
|
if (unlikely(status < 0)) {
|
||||||
@ -559,14 +574,12 @@ int get_inode(char *newpath, struct mars_dent *dent)
|
|||||||
inode = path.dentry->d_inode;
|
inode = path.dentry->d_inode;
|
||||||
|
|
||||||
status = -ENOMEM;
|
status = -ENOMEM;
|
||||||
link = brick_string_alloc(0);
|
link = brick_string_alloc(len + 2);
|
||||||
if (likely(link)) {
|
|
||||||
MARS_IO("len = %d\n", len);
|
MARS_IO("len = %d\n", len);
|
||||||
status = inode->i_op->readlink(path.dentry, link, len + 1);
|
status = inode->i_op->readlink(path.dentry, link, len + 1);
|
||||||
link[len] = '\0';
|
link[len] = '\0';
|
||||||
if (status < 0 ||
|
if (status < 0 ||
|
||||||
(dent->new_link && !strncmp(dent->new_link, link, len))) {
|
(dent->new_link && !strncmp(dent->new_link, link, len))) {
|
||||||
//MARS_IO("symlink no change '%s' -> '%s' (%s) status = %d\n", newpath, link, dent->new_link ? dent->new_link : "", status);
|
|
||||||
brick_string_free(link);
|
brick_string_free(link);
|
||||||
} else {
|
} else {
|
||||||
MARS_IO("symlink '%s' -> '%s' (%s) status = %d\n", newpath, link, dent->new_link ? dent->new_link : "", status);
|
MARS_IO("symlink '%s' -> '%s' (%s) status = %d\n", newpath, link, dent->new_link ? dent->new_link : "", status);
|
||||||
@ -574,7 +587,6 @@ int get_inode(char *newpath, struct mars_dent *dent)
|
|||||||
dent->old_link = dent->new_link;
|
dent->old_link = dent->new_link;
|
||||||
dent->new_link = link;
|
dent->new_link = link;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
path_put(&path);
|
path_put(&path);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1421,11 +1433,18 @@ EXPORT_SYMBOL_GPL(mars_kill_brick_when_possible);
|
|||||||
|
|
||||||
char *_vpath_make(int line, const char *fmt, va_list *args)
|
char *_vpath_make(int line, const char *fmt, va_list *args)
|
||||||
{
|
{
|
||||||
char *res = _brick_string_alloc(MARS_SYMLINK_MAX, line);
|
va_list copy_args;
|
||||||
|
char dummy[2];
|
||||||
|
int len;
|
||||||
|
char *res;
|
||||||
|
|
||||||
|
memcpy(©_args, args, sizeof(copy_args));
|
||||||
|
len = vsnprintf(dummy, sizeof(dummy), fmt, copy_args);
|
||||||
|
len = _length_paranoia(len, line);
|
||||||
|
res = _brick_string_alloc(len + 2, line);
|
||||||
|
|
||||||
|
vsnprintf(res, len + 1, fmt, *args);
|
||||||
|
|
||||||
if (likely(res)) {
|
|
||||||
vsnprintf(res, MARS_SYMLINK_MAX, fmt, *args);
|
|
||||||
}
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(_vpath_make);
|
EXPORT_SYMBOL_GPL(_vpath_make);
|
||||||
@ -1444,12 +1463,23 @@ EXPORT_SYMBOL_GPL(_path_make);
|
|||||||
char *_backskip_replace(int line, const char *path, char delim, bool insert, const char *fmt, ...)
|
char *_backskip_replace(int line, const char *path, char delim, bool insert, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
int path_len = strlen(path);
|
int path_len = strlen(path);
|
||||||
int total_len = strlen(fmt) + path_len + MARS_SYMLINK_MAX;
|
int fmt_len;
|
||||||
char *res = _brick_string_alloc(total_len + 1, line);
|
int total_len;
|
||||||
if (likely(res)) {
|
char *res;
|
||||||
va_list args;
|
va_list args;
|
||||||
int pos = path_len;
|
int pos = path_len;
|
||||||
int plus;
|
int plus;
|
||||||
|
char dummy[2];
|
||||||
|
|
||||||
|
va_start(args, fmt);
|
||||||
|
fmt_len = vsnprintf(dummy, sizeof(dummy), fmt, args);
|
||||||
|
va_end(args);
|
||||||
|
fmt_len = _length_paranoia(fmt_len, line);
|
||||||
|
|
||||||
|
total_len = fmt_len + path_len;
|
||||||
|
total_len = _length_paranoia(total_len, line);
|
||||||
|
|
||||||
|
res = _brick_string_alloc(total_len + 2, line);
|
||||||
|
|
||||||
while (pos > 0 && path[pos] != '/') {
|
while (pos > 0 && path[pos] != '/') {
|
||||||
pos--;
|
pos--;
|
||||||
@ -1468,7 +1498,6 @@ char *_backskip_replace(int line, const char *path, char delim, bool insert, con
|
|||||||
if (insert) {
|
if (insert) {
|
||||||
strncpy(res + pos + plus, path + pos + 1, total_len - pos - plus);
|
strncpy(res + pos + plus, path + pos + 1, total_len - pos - plus);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(_backskip_replace);
|
EXPORT_SYMBOL_GPL(_backskip_replace);
|
||||||
|
@ -51,6 +51,7 @@ my $user_version = 0.1;
|
|||||||
my $mars = "/mars";
|
my $mars = "/mars";
|
||||||
my $host = `uname -n` or ldie "cannot determine my network node name\n";
|
my $host = `uname -n` or ldie "cannot determine my network node name\n";
|
||||||
chomp $host;
|
chomp $host;
|
||||||
|
check_id($host);
|
||||||
my $real_host = $host;
|
my $real_host = $host;
|
||||||
my $force = 0;
|
my $force = 0;
|
||||||
my $timeout = -1;
|
my $timeout = -1;
|
||||||
@ -319,16 +320,25 @@ sub wait_cluster {
|
|||||||
##################################################################
|
##################################################################
|
||||||
|
|
||||||
# syntactic checks
|
# syntactic checks
|
||||||
|
# (also check for existence)
|
||||||
|
|
||||||
sub check_id {
|
sub check_id {
|
||||||
my $str = shift;
|
my ($str, $must_exist) = @_;
|
||||||
ldie "identifier '$str' has disallowed characters" unless $str =~ m/^[A-Za-z_][-A-Za-z0-9_]*$/;
|
ldie "identifier '$str' is empty" unless defined($str) && $str;
|
||||||
ldie "identifier '$str' is too long (only 16 chars allowed)" if length($str) > 16;
|
ldie "identifier '$str' has disallowed characters" unless $str =~ m/^[A-Za-z0-9_][-A-Za-z0-9_]*$/;
|
||||||
|
ldie "identifier '$str' is too long (only 63 chars supported according to RFC 1123)" if length($str) > 63;
|
||||||
|
if (defined($must_exist) && $must_exist) {
|
||||||
|
my $ip_path = "$mars/ips/ip-$str";
|
||||||
|
ldie "host '$str' does not exist in $mars/ips/" unless get_link($ip_path, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sub check_id_list {
|
sub check_id_list {
|
||||||
my $str = shift;
|
my ($str, $must_exist) = @_;
|
||||||
ldie "comma-separated list '$str' has disallowed characters" unless $str =~ m/^[A-Za-z_][-A-Za-z0-9_,]*$/;
|
ldie "comma-separated list '$str' has disallowed characters" unless $str =~ m/^[A-Za-z0-9_][-A-Za-z0-9_,]*$/;
|
||||||
|
foreach my $id (split(",", $str)) {
|
||||||
|
check_id($id, $must_exist);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
##################################################################
|
##################################################################
|
||||||
@ -1015,6 +1025,7 @@ sub _writable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sub _get_ip {
|
sub _get_ip {
|
||||||
|
check_id($host);
|
||||||
my $ip_path = "$mars/ips/ip-$host";
|
my $ip_path = "$mars/ips/ip-$host";
|
||||||
if (my $from_link = get_link($ip_path, 2)) {
|
if (my $from_link = get_link($ip_path, 2)) {
|
||||||
lprint_stderr "Using IP '$from_link' from '$ip_path'\n";
|
lprint_stderr "Using IP '$from_link' from '$ip_path'\n";
|
||||||
@ -1119,7 +1130,7 @@ sub set_connect_pref_list {
|
|||||||
lprint "$value\n";
|
lprint "$value\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
check_id_list($list);
|
check_id_list($list, 1);
|
||||||
set_link($list, $dst);
|
set_link($list, $dst);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3805,6 +3816,7 @@ foreach my $arg (@ARGV) {
|
|||||||
$threshold = $arg;
|
$threshold = $arg;
|
||||||
next;
|
next;
|
||||||
} elsif ($arg =~ s/--host\s*=\s*([-_A-Za-z0-9]+)/$1/) {
|
} elsif ($arg =~ s/--host\s*=\s*([-_A-Za-z0-9]+)/$1/) {
|
||||||
|
check_id($arg);
|
||||||
ldie "host '$arg' does not exist in /mars/ips/ip-*\n" unless -l "/mars/ips/ip-$arg";
|
ldie "host '$arg' does not exist in /mars/ips/ip-*\n" unless -l "/mars/ips/ip-$arg";
|
||||||
if ($arg ne $host) {
|
if ($arg ne $host) {
|
||||||
lprint "ATTENTION: acting as if I were host '$arg'\n";
|
lprint "ATTENTION: acting as if I were host '$arg'\n";
|
||||||
|
Loading…
Reference in New Issue
Block a user