X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Futils.c;fp=src%2Fcommon%2Futils.c;h=0147c8c994d3cb89f7fa936633863e043cdd2221;hp=3cdb6b76063caf4545761a93b52523dd2c39b14c;hb=ce9ee1fb2847b1603c4382c82aef95121f0e3d07;hpb=a1e4ab8b82429320c5f962038c0f28b874f8ea6f diff --git a/src/common/utils.c b/src/common/utils.c index 3cdb6b760..0147c8c99 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -1501,3 +1501,35 @@ int utils_get_memory_total(size_t *value) { return read_proc_meminfo_field(PROC_MEMINFO_MEMTOTAL_LINE, value); } + +LTTNG_HIDDEN +int utils_change_working_directory(const char *path) +{ + int ret; + + assert(path); + + DBG("Changing working directory to \"%s\"", path); + ret = chdir(path); + if (ret) { + PERROR("Failed to change working directory to \"%s\"", path); + goto end; + } + + /* Check for write access */ + if (access(path, W_OK)) { + if (errno == EACCES) { + /* + * Do not treat this as an error since the permission + * might change in the lifetime of the process + */ + DBG("Working directory \"%s\" is not writable", path); + } else { + PERROR("Failed to check if working directory \"%s\" is writable", + path); + } + } + +end: + return ret; +}