Fix: handle sysconf errors
[lttng-ust.git] / tests / ust-basic-tracing / ust-basic-tracing.c
index 57c025ee15e59081cadf0d2c401e0059f7b0fa46..7bfeb5bc540f7037a15a8a61976f0a2ebbfcc2ef 100644 (file)
 #include <sys/socket.h>
 
 #include <ust-comm.h>
+#include <lttng/ust-error.h>
 #include "../../libringbuffer/backend.h"
 #include "../../libringbuffer/frontend.h"
+#include "../../liblttng-ust/compat.h" /* For ENODATA */
 
 #define MAX_NR_STREAMS 64
 #define MAX_NR_EVENTS  128
@@ -84,7 +86,7 @@ int open_streams(int sock, int channel_handle, struct lttng_ust_object_data *str
                        stream_datas[k].handle = lur.ret_val;
                        printf("received stream handle %u\n",
                                stream_datas[k].handle);
-                       if (lur.ret_code == USTCOMM_OK) {
+                       if (lur.ret_code == LTTNG_UST_OK) {
                                ssize_t len;
 
                                stream_datas[k].memory_map_size = lur.u.stream.memory_map_size;
@@ -101,7 +103,7 @@ int open_streams(int sock, int channel_handle, struct lttng_ust_object_data *str
                        }
                        k++;
                }
-               if (ret == -ENOENT)
+               if (ret == -LTTNG_UST_ERR_NOENT)
                        break;
                if (ret)
                        return ret;
@@ -222,7 +224,7 @@ int consume_stream(struct lttng_ust_shm_handle *handle, int cpu, char *outfile)
        }
 
        /* copy */
-       outfd = open(outfile, O_WRONLY | O_CREAT | O_LARGEFILE | O_TRUNC,
+       outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC,
                        S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
        if (outfd < 0) {
                perror("open output");
@@ -355,7 +357,7 @@ int send_app_msgs(int sock, const char *outputpath,
                return ret;
        metadata_data.handle = lur.ret_val;
        printf("received metadata handle %u\n", metadata_data.handle);
-       if (lur.ret_code == USTCOMM_OK) {
+       if (lur.ret_code == LTTNG_UST_OK) {
                ssize_t len;
 
                metadata_data.memory_map_size = lur.u.channel.memory_map_size;
@@ -396,7 +398,7 @@ int send_app_msgs(int sock, const char *outputpath,
                return ret;
        channel_data.handle = lur.ret_val;
        printf("received channel handle %u\n", channel_data.handle);
-       if (lur.ret_code == USTCOMM_OK) {
+       if (lur.ret_code == LTTNG_UST_OK) {
                ssize_t len;
 
                channel_data.memory_map_size = lur.u.channel.memory_map_size;
@@ -640,11 +642,15 @@ error:
 
 int update_futex(int fd, int active)
 {
-       size_t mmap_size = sysconf(_SC_PAGE_SIZE);
+       long page_size;
        char *wait_shm_mmap;
        int ret;
 
-       wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
+       page_size = sysconf(_SC_PAGE_SIZE);
+       if (page_size < 0) {
+               goto error;
+       }
+       wait_shm_mmap = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
                  MAP_SHARED, fd, 0);
        if (wait_shm_mmap == MAP_FAILED) {
                perror("mmap");
@@ -658,7 +664,7 @@ int update_futex(int fd, int active)
        } else {
                uatomic_set((int32_t *) wait_shm_mmap, 0);
        }
-       ret = munmap(wait_shm_mmap, mmap_size);
+       ret = munmap(wait_shm_mmap, page_size);
        if (ret) {
                perror("Error unmapping wait shm");
                goto error;
@@ -700,6 +706,8 @@ static void set_ulimit(void)
 int main(int argc, const char **argv)
 {
        const char *home_dir;
+       char home_rundir[PATH_MAX];
+       char *cmd = NULL;
        int ret, wait_shm_fd;
        struct sigaction act;
        mode_t old_umask = 0;
@@ -763,6 +771,21 @@ int main(int argc, const char **argv)
                strcpy(apps_sock_path, DEFAULT_GLOBAL_APPS_UNIX_SOCK);
                old_umask = umask(0);
        } else {
+               home_dir = (const char *) getenv("HOME");
+               if (!home_dir) {
+                       perror("getenv error");
+                       return -ENOENT;
+               }
+
+               snprintf(home_rundir, PATH_MAX,
+                        LTTNG_HOME_RUNDIR, home_dir);
+
+               ret = mkdir(home_rundir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
+               if (ret && errno != EEXIST) {
+                       perror("mkdir");
+                       return -1;
+               }
+
                snprintf(local_apps_wait_shm_path, PATH_MAX,
                         DEFAULT_HOME_APPS_WAIT_SHM_PATH, getuid());
                wait_shm_fd = get_wait_shm(local_apps_wait_shm_path,
@@ -771,11 +794,6 @@ int main(int argc, const char **argv)
                        perror("local wait shm error");
                        return -1;
                }
-               home_dir = (const char *) getenv("HOME");
-               if (!home_dir) {
-                       perror("getenv error");
-                       return -ENOENT;
-               }
                snprintf(apps_sock_path, PATH_MAX,
                         DEFAULT_HOME_APPS_UNIX_SOCK, home_dir);
        }
@@ -821,6 +839,7 @@ int main(int argc, const char **argv)
                        pid_t ppid;
                        uid_t uid;
                        gid_t gid;
+                       uint32_t bits_per_long;
                        char name[16];  /* Process name */
                } reg_msg;
                char bufname[17];
@@ -866,5 +885,22 @@ end:
                return -1;
        }
 
+       if (geteuid()) {
+               printf("Removing %s directory\n", home_rundir);
+               ret = asprintf(&cmd, "rm -rf %s", home_rundir);
+               if (ret < 0) {
+                       printf("asprintf failed. Something is really wrong!\n");
+                       return -1;
+               }
+
+               /* Remove lttng run directory */
+               ret = system(cmd);
+               if (ret < 0) {
+                       printf("Unable to clean %s\n", home_rundir);
+                       return -1;
+               }
+       }
+
+
        return 0;
 }
This page took 0.040301 seconds and 4 git commands to generate.