*/
char *utils_strdupdelim(const char *begin, const char *end)
{
- char *str;
+ char *str = zmalloc<char>(end - begin + 1);
- str = (char *) zmalloc(end - begin + 1);
if (str == NULL) {
PERROR("zmalloc strdupdelim");
goto error;
*/
int utils_create_pid_file(pid_t pid, const char *filepath)
{
- int ret;
- FILE *fp;
+ int ret, fd = -1;
+ FILE *fp = NULL;
LTTNG_ASSERT(filepath);
- fp = fopen(filepath, "w");
+ fd = open(filepath, O_CREAT | O_WRONLY, S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH);
+ if (fd < 0) {
+ PERROR("open file %s", filepath);
+ ret = -1;
+ goto error;
+ }
+
+ fp = fdopen(fd, "w");
if (fp == NULL) {
- PERROR("open pid file %s", filepath);
+ PERROR("fdopen file %s", filepath);
ret = -1;
+ if (close(fd)) {
+ PERROR("Failed to close `%s` file descriptor while handling fdopen error", filepath);
+ }
+
goto error;
}
ret = fprintf(fp, "%d\n", (int) pid);
if (ret < 0) {
- PERROR("fprintf pid file");
+ PERROR("fprintf file %s", filepath);
+ ret = -1;
goto error;
}
- if (fclose(fp)) {
- PERROR("fclose");
- }
- DBG("Pid %d written in file %s", (int) pid, filepath);
+ DBG("'%d' written in file %s", (int) pid, filepath);
ret = 0;
+
error:
+ if (fp && fclose(fp)) {
+ PERROR("fclose file %s", filepath);
+ }
return ret;
}
goto end;
}
retry:
- buf = (char *) zmalloc(buflen);
+ buf = zmalloc<char>(buflen);
if (!buf) {
goto end;
}
string_len += long_options[i].has_arg ? 1 : 0;
}
- optstring = (char *) zmalloc(string_len);
+ optstring = zmalloc<char>(string_len);
if (!optstring) {
goto end;
}
buflen = FALLBACK_USER_BUFLEN;
}
- buf = (char *) zmalloc(buflen);
+ buf = zmalloc<char>(buflen);
if (!buf) {
ret_val = LTTNG_ERR_NOMEM;
goto end;
case ERANGE:
buflen *= 2;
free(buf);
- buf = (char *) zmalloc(buflen);
+ buf = zmalloc<char>(buflen);
if (!buf) {
ret_val = LTTNG_ERR_NOMEM;
goto end;
buflen = FALLBACK_GROUP_BUFLEN;
}
- buf = (char *) zmalloc(buflen);
+ buf = zmalloc<char>(buflen);
if (!buf) {
ret_val = LTTNG_ERR_NOMEM;
goto end;
case ERANGE:
buflen *= 2;
free(buf);
- buf = (char *) zmalloc(buflen);
+ buf = zmalloc<char>(buflen);
if (!buf) {
ret_val = LTTNG_ERR_NOMEM;
goto end;