return 0;
}
-static int get_cmd_info(struct crash_info *cinfo)
+static bool get_cmd_info(struct crash_info *cinfo)
{
- cinfo->cmd_line = get_cmd_line(cinfo->pid_info);
+ assert(cinfo);
+
+ cinfo->cmd_line = malloc(PATH_MAX);
+ if (!cinfo->cmd_line)
+ return false;
+
+ if (!read_proc_file(cinfo->pid_info, "cmdline", cinfo->cmd_line, PATH_MAX, NULL))
+ goto err;
+
cinfo->cmd_path = get_exe_path(cinfo->pid_info);
+ if (!cinfo->cmd_path)
+ goto err;
- return (cinfo->cmd_line != NULL && cinfo->cmd_path != NULL);
+ return true;
+
+err:
+ free(cinfo->cmd_line);
+ return false;
}
static int set_prstatus(struct crash_info *cinfo)
}
}
- ret = get_cmd_info(cinfo);
- if (ret <= 0) {
+ if (!get_cmd_info(cinfo)) {
_E("Failed to get command info");
return -1;
}
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
+#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
return -1;
}
-char* get_cmd_line(pid_t pid)
+bool filter_drop_trailing_whitespace(char *buf, int size, int datalen)
{
- char cmdline_path[PATH_MAX];
+ assert(buf);
+ assert(datalen <= size);
- snprintf(cmdline_path, sizeof(cmdline_path),
- "/proc/%d/cmdline", pid);
+ bool filtered = false;
- int fd = open(cmdline_path, O_RDONLY);
- if (fd < 0) {
- _E("Failed to open %s: %m\n", cmdline_path);
- return NULL;
+ for (int i = datalen; i >= 0 && isspace(buf[i]); --i) {
+ buf[i] = '\0';
+ filtered = true;
}
- char buffer[PATH_MAX];
- ssize_t ret = read(fd, buffer, sizeof(buffer) - 1);
- close(fd);
+ return filtered;
+}
- if (ret <= 0) {
- _E("Failed to read %s: %m\n", cmdline_path);
- return NULL;
+bool read_proc_file(pid_t pid, const char * const file, char *outbuf, size_t bufsize, charp0filter filter)
+{
+ assert(file);
+ assert(outbuf);
+ assert(bufsize > 0);
+
+ char path[PATH_MAX];
+
+ snprintf(path, sizeof(path), "/proc/%d/%s", pid, file);
+
+ int fd = open(path, O_RDONLY);
+ if (fd < 0) {
+ _E("Failed to open %s: %m\n", path);
+ return false;
}
- buffer[ret] = '\0';
- char *result;
- if (asprintf(&result, "%s", buffer) == -1) {
- _E("asprintf() error: %m\n");
- return NULL;
+ ssize_t ret = read(fd, outbuf, bufsize - 1);
+ if (ret < 0) {
+ _E("Failed to read %s: %m\n", path);
+ goto err_close;
}
+ outbuf[ret] = '\0';
- return result;
+ close(fd);
+
+ if (ret > 0 && filter)
+ (void)filter(outbuf, bufsize, ret - 1);
+
+ return true;
+
+err_close:
+ close(fd);
+ return false;
}
char* get_exe_path(pid_t pid)
char* get_exe_path(pid_t pid);
-char* get_cmd_line(pid_t pid);
+typedef bool (*charp0filter)(char *buf, int size, int datalen);
+bool filter_drop_trailing_whitespace(char *buf, int size, int datalen);
+bool read_proc_file(pid_t pid, const char * const file, char *outbuf, size_t bufsize, charp0filter filter);
char* concatenate(char *const vec[]);