diff --git a/src/common.cpp b/src/common.cpp index 21c4b92cd..2aa76cc5c 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -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; diff --git a/src/common.h b/src/common.h index 2ba498545..d255ac62d 100644 --- a/src/common.h +++ b/src/common.h @@ -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 T from_string(const wcstring &x) @@ -526,36 +529,6 @@ public: /* Helper function to convert from a null_terminated_array_t to a null_terminated_array_t */ void convert_wide_array_to_narrow(const null_terminated_array_t &arr, null_terminated_array_t *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(); diff --git a/src/postfork.cpp b/src/postfork.cpp index 29cf850ca..43c33d56b 100644 --- a/src/postfork.cpp +++ b/src/postfork.cpp @@ -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; } diff --git a/src/proc.cpp b/src/proc.cpp index be106d43e..806da0ec6 100644 --- a/src/proc.cpp +++ b/src/proc.cpp @@ -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), diff --git a/src/proc.h b/src/proc.h index 68ec54543..3b77fe51e 100644 --- a/src/proc.h +++ b/src/proc.h @@ -123,9 +123,6 @@ private: null_terminated_array_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); } /**