Fix asprintf and scanf ignoring return value
[ust.git] / libustcmd / ustcmd.c
index 7a05afa20079944b0ce3e79e60efcf06cc17f233..5b4fd02efcf242854e9ed69a6455a6ede087ac4e 100644 (file)
@@ -25,7 +25,7 @@
 #include <dirent.h>
 
 #include "ustcomm.h"
-#include "ustcmd.h"
+#include "ust/ustcmd.h"
 #include "usterr.h"
 
 pid_t *ustcmd_get_online_pids(void)
@@ -90,7 +90,11 @@ int ustcmd_set_marker_state(const char *mn, int state, pid_t pid)
                return USTCMD_ERR_ARG;
        }
 
-       asprintf(&cmd, "%s %s", cmd_str[state], mn);
+       if (asprintf(&cmd, "%s %s", cmd_str[state], mn) < 0) {
+               ERR("ustcmd_set_marker_state : asprintf failed (%s %s)",
+                   cmd_str[state], mn);
+               return USTCMD_ERR_GEN;
+       }
 
        result = ustcmd_send_cmd(cmd, pid, NULL);
        if (result) {
@@ -114,7 +118,11 @@ int ustcmd_set_subbuf_size(const char *channel_size, pid_t pid)
        char *cmd;
        int result;
 
-       asprintf(&cmd, "%s %s", "set_subbuf_size", channel_size);
+       if (asprintf(&cmd, "%s %s", "set_subbuf_size", channel_size) < 0) {
+               ERR("ustcmd_set_subbuf_size : asprintf failed (set_subbuf_size %s)",
+                   channel_size);
+               return -1;
+       }
 
        result = ustcmd_send_cmd(cmd, pid, NULL);
        if (result != 1) {
@@ -138,7 +146,11 @@ int ustcmd_set_subbuf_num(const char *channel_size, pid_t pid)
        char *cmd;
        int result;
 
-       asprintf(&cmd, "%s %s", "set_subbuf_num", channel_size);
+       if (asprintf(&cmd, "%s %s", "set_subbuf_num", channel_size) < 0) {
+               ERR("ustcmd_set_subbuf_num : asprintf failed (set_subbuf_num %s",
+                   channel_size);
+               return -1;
+       }
 
        result = ustcmd_send_cmd(cmd, pid, NULL);
        if (result != 1) {
@@ -150,6 +162,69 @@ int ustcmd_set_subbuf_num(const char *channel_size, pid_t pid)
        return 0;
 }
 
+/**
+ * Get subbuffer size.
+ *
+ * @param channel      Channel name
+ * @param pid          Traced process ID
+ * @return             subbuf size if successful, or error
+ */
+int ustcmd_get_subbuf_size(const char *channel, pid_t pid)
+{
+       char *cmd, *reply;
+       int result;
+
+       /* format: channel_cpu */
+       if (asprintf(&cmd, "%s %s_0", "get_subbuf_size", channel) < 0) {
+               ERR("ustcmd_get_subbuf_size : asprintf failed (get_subbuf_size, %s_0",
+                   channel);
+               return -1;
+       }
+
+       result = ustcmd_send_cmd(cmd, pid, &reply);
+       if (result) {
+               free(cmd);
+               free(reply);
+               return -1;
+       }
+
+       result = atoi(reply);
+       free(cmd);
+       free(reply);
+       return result;
+}
+
+/**
+ * Get subbuffer num.
+ *
+ * @param channel      Channel name
+ * @param pid          Traced process ID
+ * @return             subbuf cnf if successful, or error
+ */
+int ustcmd_get_subbuf_num(const char *channel, pid_t pid)
+{
+       char *cmd, *reply;
+       int result;
+
+       /* format: channel_cpu */
+       if (asprintf(&cmd, "%s %s_0", "get_n_subbufs", channel) < 0) {
+               ERR("ustcmd_get_subbuf_num : asprintf failed (get_n_subbufs, %s_0",
+                   channel);
+               return -1;
+       }
+
+       result = ustcmd_send_cmd(cmd, pid, &reply);
+       if (result) {
+               free(cmd);
+               free(reply);
+               return -1;
+       }
+
+       result = atoi(reply);
+       free(cmd);
+       free(reply);
+       return result;
+}
 
 /**
  * Destroys an UST trace according to a PID.
@@ -365,6 +440,75 @@ int ustcmd_get_cmsf(struct marker_status **cmsf, const pid_t pid)
        return 0;
 }
 
+/**
+ * Set socket path
+ *
+ * @param sock_path    Socket path
+ * @param pid          Traced process ID
+ * @return             0 if successful, or error
+ */
+int ustcmd_set_sock_path(const char *sock_path, pid_t pid)
+{
+       char *cmd;
+       int result;
+
+       if (asprintf(&cmd, "%s %s", "set_sock_path", sock_path) < 0) {
+               ERR("ustcmd_set_sock_path : asprintf failed (set_sock_path, %s",
+                   sock_path);
+               return -1;
+       }
+
+       result = ustcmd_send_cmd(cmd, pid, NULL);
+       if (result != 1) {
+               free(cmd);
+               return USTCMD_ERR_GEN;
+       }
+
+       free(cmd);
+       return 0;
+}
+
+/**
+ * Get socket path
+ *
+ * @param sock_path    Pointer to where the socket path will be returned
+ * @param pid          Traced process ID
+ * @return             0 if successful, or error
+ */
+int ustcmd_get_sock_path(char **sock_path, pid_t pid)
+{
+       char *cmd, *reply;
+       int result;
+
+       if (asprintf(&cmd, "%s", "get_sock_path") < 0) {
+               ERR("ustcmd_get_sock_path : asprintf failed");
+               return USTCMD_ERR_GEN;
+       }
+
+       result = ustcmd_send_cmd(cmd, pid, &reply);
+       if (result != 1) {
+               free(cmd);
+               free(reply);
+               return USTCMD_ERR_GEN;
+       }
+
+       free(cmd);
+       *sock_path = reply;
+       return 0;
+}
+
+int ustcmd_force_switch(pid_t pid)
+{
+       int result;
+
+       result = ustcmd_send_cmd("force_switch", pid, NULL);
+       if (result != 1) {
+               return USTCMD_ERR_GEN;
+       }
+
+       return 0;
+}
+
 /**
  * Sends a given command to a traceable process
  *
This page took 0.023797 seconds and 4 git commands to generate.