Eliminate narrow_string_rep_t

This was used to cache a narrow string representation
of commands, so that if certain system calls returned errors
after fork, we could output error messages without allocating
memory. But in practice these errors are very uncommon, as are
commands that have wide characters. It is simpler to do a best-effort
output of the wide string, instead of caching a narrow string
unconditionally.
This commit is contained in:
ridiculousfish 2016-02-28 01:38:28 -08:00
parent 3633c51ad8
commit 9151ec7092
5 changed files with 32 additions and 62 deletions

View File

@ -795,6 +795,24 @@ void format_long_safe(wchar_t buff[64], long val)
}
}
void narrow_string_safe(char buff[64], const wchar_t *s)
{
size_t idx = 0;
for (size_t widx = 0; s[widx] != L'\0'; widx++)
{
wchar_t c = s[widx];
if (c <= 127)
{
buff[idx++] = char(c);
if (idx + 1 == 64)
{
break;
}
}
}
buff[idx] = '\0';
}
wcstring reformat_for_screen(const wcstring &msg)
{
wcstring buff;

View File

@ -396,6 +396,9 @@ void debug_safe(int level, const char *msg, const char *param1 = NULL, const cha
void format_long_safe(char buff[64], long val);
void format_long_safe(wchar_t buff[64], long val);
/** "Narrows" a wide character string. This just grabs any ASCII characters and trunactes. */
void narrow_string_safe(char buff[64], const wchar_t *s);
template<typename T>
T from_string(const wcstring &x)
@ -526,36 +529,6 @@ public:
/* Helper function to convert from a null_terminated_array_t<wchar_t> to a null_terminated_array_t<char_t> */
void convert_wide_array_to_narrow(const null_terminated_array_t<wchar_t> &arr, null_terminated_array_t<char> *output);
/* Helper class to cache a narrow version of a wcstring in a malloc'd buffer, so that we can read it after fork() */
class narrow_string_rep_t
{
private:
const char *str;
/* No copying */
narrow_string_rep_t &operator=(const narrow_string_rep_t &);
narrow_string_rep_t(const narrow_string_rep_t &x);
public:
~narrow_string_rep_t()
{
free((void *)str);
}
narrow_string_rep_t() : str(NULL) {}
void set(const wcstring &s)
{
free((void *)str);
str = wcs2str(s.c_str());
}
const char *get() const
{
return str;
}
};
bool is_forked_child();

View File

@ -56,7 +56,6 @@ static void debug_safe_int(int level, const char *format, int val)
debug_safe(level, format, buff);
}
// PCA These calls to debug are rather sketchy because they may allocate memory. Fortunately they only occur if an error occurs.
int set_child_group(job_t *j, process_t *p, int print_errors)
{
int res = 0;
@ -76,18 +75,22 @@ int set_child_group(job_t *j, process_t *p, int print_errors)
char job_id_buff[128];
char getpgid_buff[128];
char job_pgid_buff[128];
char argv0[64];
char command[64];
format_long_safe(pid_buff, p->pid);
format_long_safe(job_id_buff, j->job_id);
format_long_safe(getpgid_buff, getpgid(p->pid));
format_long_safe(job_pgid_buff, j->pgid);
narrow_string_safe(argv0, p->argv0());
narrow_string_safe(command, j->command_wcstr());
debug_safe(1,
"Could not send process %s, '%s' in job %s, '%s' from group %s to group %s",
pid_buff,
p->argv0_cstr(),
argv0,
job_id_buff,
j->command_cstr(),
command,
getpgid_buff,
job_pgid_buff);
@ -105,9 +108,11 @@ int set_child_group(job_t *j, process_t *p, int print_errors)
{
if (tcsetpgrp(0, j->pgid) && print_errors)
{
char job_id_buff[128];
char job_id_buff[64];
char command_buff[64];
format_long_safe(job_id_buff, j->job_id);
debug_safe(1, "Could not send job %s ('%s') to foreground", job_id_buff, j->command_cstr());
narrow_string_safe(command_buff, j->command_wcstr());
debug_safe(1, "Could not send job %s ('%s') to foreground", job_id_buff, command_buff);
safe_perror("tcsetpgrp");
res = -1;
}

View File

@ -453,11 +453,7 @@ static void handle_child_status(pid_t pid, int status)
}
process_t::process_t() :
argv_array(),
argv0_narrow(),
type(),
internal_block_node(NODE_OFFSET_INVALID),
actual_cmd(),
pid(0),
pipe_write_fd(0),
pipe_read_fd(0),
@ -480,8 +476,6 @@ process_t::~process_t()
}
job_t::job_t(job_id_t jobid, const io_chain_t &bio) :
command_str(),
command_narrow(),
block_io(bio),
first_process(NULL),
pgid(0),

View File

@ -123,9 +123,6 @@ private:
null_terminated_array_t<wchar_t> argv_array;
/* narrow copy of argv0 so we don't have to convert after fork */
narrow_string_rep_t argv0_narrow;
io_chain_t process_io_chain;
/* No copying */
@ -151,7 +148,6 @@ public:
void set_argv(const wcstring_list_t &argv)
{
argv_array.set(argv);
argv0_narrow.set(argv.empty() ? L"" : argv[0]);
}
/** Returns argv */
@ -173,18 +169,12 @@ public:
}
/** Returns argv[0], or NULL */
const wchar_t *argv0(void) const
const wchar_t *argv0() const
{
const wchar_t * const *argv = argv_array.get();
return argv ? argv[0] : NULL;
}
/** Returns argv[0] as a char * */
const char *argv0_cstr(void) const
{
return argv0_narrow.get();
}
/* IO chain getter and setter */
const io_chain_t &io_chain() const
{
@ -278,9 +268,6 @@ class job_t
*/
wcstring command_str;
/* narrow copy so we don't have to convert after fork */
narrow_string_rep_t command_narrow;
/* The IO chain associated with the block */
const io_chain_t block_io;
@ -311,17 +298,10 @@ public:
return command_str;
}
/** Returns the command as a char *. */
const char *command_cstr() const
{
return command_narrow.get();
}
/** Sets the command */
void set_command(const wcstring &cmd)
{
command_str = cmd;
command_narrow.set(cmd);
}
/**