Whamcloud - gitweb
LU-4955 lfsck: reset param when the LFSCK is reset
[fs/lustre-release.git] / lustre / utils / liblustreapi_hsm.c
index 3f1b8b9..47fe2ec 100644 (file)
@@ -53,9 +53,9 @@
 #include <unistd.h>
 #endif
 
-#include <liblustre.h>
+#include <libcfs/libcfs.h>
 #include <lnet/lnetctl.h>
-#include <obd.h>
+#include <lustre/lustre_idl.h>
 #include <lustre/lustreapi.h>
 #include "lustreapi_internal.h"
 
@@ -113,7 +113,7 @@ enum ct_event {
 };
 
 /* initialized in llapi_hsm_register_event_fifo() */
-FILE *llapi_hsm_event_fp;
+int llapi_hsm_event_fd = -1;
 
 static inline const char *llapi_hsm_ct_ev2str(int type)
 {
@@ -173,12 +173,14 @@ int llapi_hsm_write_json_event(struct llapi_json_item_list **event)
 {
        int                             rc;
        char                            time_string[40];
+       char                            json_buf[PIPE_BUF];
+       FILE                            *buf_file;
        time_t                          event_time = time(0);
        struct tm                       time_components;
        struct llapi_json_item_list     *json_items;
 
-       /* Noop unless the event fp was initialized */
-       if (llapi_hsm_event_fp == NULL)
+       /* Noop unless the event fd was initialized */
+       if (llapi_hsm_event_fd < 0)
                return 0;
 
        if (event == NULL || *event == NULL)
@@ -203,21 +205,24 @@ int llapi_hsm_write_json_event(struct llapi_json_item_list **event)
                return rc;
        }
 
-       rc = llapi_json_write_list(event, llapi_hsm_event_fp);
-       if (rc < 0) {
-               /* Ignore write failures due to missing reader. */
-               if (rc == -EPIPE)
-                       return 0;
+       buf_file = fmemopen(json_buf, sizeof(json_buf), "w");
+       if (buf_file == NULL)
+               return -errno;
 
-               /* Skip llapi_error() here because there's no point
-                * in creating a JSON-formatted error message about
-                * failing to write a JSON-formatted message.
-                */
-               fprintf(stderr,
-                       "\nFATAL ERROR IN llapi_hsm_write_list(): rc %d", rc);
+       rc = llapi_json_write_list(event, buf_file);
+       if (rc < 0) {
+               fclose(buf_file);
                return rc;
        }
 
+       fclose(buf_file);
+
+       if (write(llapi_hsm_event_fd, json_buf, strlen(json_buf)) < 0) {
+               /* Ignore write failures due to missing reader. */
+               if (errno != EPIPE)
+                       return -errno;
+       }
+
        return 0;
 }
 
@@ -304,16 +309,28 @@ out_free:
  * llapi_hsm_write_json_event:
  *
  * Copytool registration and deregistration:
- * {"event_time": "2014-02-26 14:58:01 -0500", "event_type": "REGISTER", "archive": 0, "mount_point": "/mnt/lustre", "uuid": "80379a60-1f8a-743f-daf2-307cde793ec2"}
- * {"event_time": "2014-02-26 14:58:01 -0500", "event_type": "UNREGISTER", "archive": 0, "mount_point": "/mnt/lustre", "uuid": "80379a60-1f8a-743f-daf2-307cde793ec2"}
+ * {"event_time": "2014-02-26 14:58:01 -0500", "event_type": "REGISTER",
+ *  "archive": 0, "mount_point": "/mnt/lustre",
+ *  "uuid": "80379a60-1f8a-743f-daf2-307cde793ec2"}
+ * {"event_time": "2014-02-26 14:58:01 -0500", "event_type": "UNREGISTER",
+ *  "archive": 0, "mount_point": "/mnt/lustre",
+ *  "uuid": "80379a60-1f8a-743f-daf2-307cde793ec2"}
  *
  * An archive action, start to completion:
- * {"event_time": "2014-02-26 14:50:13 -0500", "event_type": "ARCHIVE_START", "total_bytes": 0, "lustre_path": "d71.sanity-hsm/f71.sanity-hsm", "source_fid": "0x2000013a1:0x2:0x0", "data_fid": "0x2000013a1:0x2:0x0"}
- * {"event_time": "2014-02-26 14:50:18 -0500", "event_type": "ARCHIVE_RUNNING", "current_bytes": 5242880, "total_bytes": 39000000, "lustre_path": "d71.sanity-hsm/f71.sanity-hsm", "source_fid": "0x2000013a1:0x2:0x0", "data_fid": "0x2000013a1:0x2:0x0"}
- * {"event_time": "2014-02-26 14:50:50 -0500", "event_type": "ARCHIVE_FINISH", "source_fid": "0x2000013a1:0x2:0x0", "data_fid": "0x2000013a1:0x2:0x0"}
+ * {"event_time": "2014-02-26 14:50:13 -0500", "event_type": "ARCHIVE_START",
+ *  "total_bytes": 0, "lustre_path": "d71.sanity-hsm/f71.sanity-hsm",
+ *  "source_fid": "0x2000013a1:0x2:0x0", "data_fid": "0x2000013a1:0x2:0x0"}
+ * {"event_time": "2014-02-26 14:50:18 -0500", "event_type": "ARCHIVE_RUNNING",
+ *  "current_bytes": 5242880, "total_bytes": 39000000,
+ *  "lustre_path": "d71.sanity-hsm/f71.sanity-hsm",
+ *  "source_fid": "0x2000013a1:0x2:0x0", "data_fid": "0x2000013a1:0x2:0x0"}
+ * {"event_time": "2014-02-26 14:50:50 -0500", "event_type": "ARCHIVE_FINISH",
+ *  "source_fid": "0x2000013a1:0x2:0x0", "data_fid": "0x2000013a1:0x2:0x0"}
  *
  * A log message:
- * {"event_time": "2014-02-26 14:50:13 -0500", "event_type": "LOGGED_MESSAGE", "level": "INFO", "message": "lhsmtool_posix[59401]: copytool fs=lustre archive#=2 item_count=1"}
+ * {"event_time": "2014-02-26 14:50:13 -0500", "event_type": "LOGGED_MESSAGE",
+ *  "level": "INFO",
+ *  "message": "lhsmtool_posix[42]: copytool fs=lustre archive#=2 item_count=1"}
  *
  * \param hcp                Opaque action handle returned by
  *                           llapi_hsm_action_start.
@@ -443,7 +460,7 @@ out_free:
  */
 int llapi_hsm_register_event_fifo(char *path)
 {
-       int read_fd, write_fd;
+       int read_fd;
        struct stat statbuf;
 
        /* Create the FIFO if necessary. */
@@ -476,8 +493,8 @@ int llapi_hsm_register_event_fifo(char *path)
 
        /* Open the FIFO for writes, but don't block on waiting
         * for a reader. */
-       write_fd = open(path, O_WRONLY | O_NONBLOCK);
-       if (write_fd < 0) {
+       llapi_hsm_event_fd = open(path, O_WRONLY | O_NONBLOCK);
+       if (llapi_hsm_event_fd < 0) {
                llapi_error(LLAPI_MSG_ERROR, errno,
                            "cannot open(%s) for write", path);
                return -errno;
@@ -488,19 +505,9 @@ int llapi_hsm_register_event_fifo(char *path)
         * events are lost. NOTE: Only one reader at a time! */
        close(read_fd);
 
-       llapi_hsm_event_fp = fdopen(write_fd, "w");
-       if (llapi_hsm_event_fp == NULL) {
-               llapi_error(LLAPI_MSG_ERROR, errno,
-                           "cannot fdopen(%s) for write", path);
-               return -errno;
-       }
-
        /* Ignore SIGPIPEs -- can occur if the reader goes away. */
        signal(SIGPIPE, SIG_IGN);
 
-       /* Don't buffer the event stream. */
-       setbuf(llapi_hsm_event_fp, NULL);
-
        return 0;
 }
 
@@ -514,16 +521,16 @@ int llapi_hsm_register_event_fifo(char *path)
  */
 int llapi_hsm_unregister_event_fifo(char *path)
 {
-       /* Noop unless the event fp was initialized */
-       if (llapi_hsm_event_fp == NULL)
+       /* Noop unless the event fd was initialized */
+       if (llapi_hsm_event_fd < 0)
                return 0;
 
-       if (fclose(llapi_hsm_event_fp) != 0)
+       if (close(llapi_hsm_event_fd) < 0)
                return -errno;
 
        unlink(path);
 
-       llapi_hsm_event_fp = NULL;
+       llapi_hsm_event_fd = -1;
 
        return 0;
 }
@@ -550,8 +557,8 @@ void llapi_hsm_log_error(enum llapi_message_level level, int _rc,
        va_list                         args2;
        struct llapi_json_item_list     *json_items;
 
-       /* Noop unless the event fp was initialized */
-       if (llapi_hsm_event_fp == NULL)
+       /* Noop unless the event fd was initialized */
+       if (llapi_hsm_event_fd < 0)
                return;
 
        rc = llapi_json_init_list(&json_items);