Whamcloud - gitweb
LU-3025 hsm-llapi: bad type cast in llapi_hsm_copytool_fini()
[fs/lustre-release.git] / lustre / tests / copytool.c
index a74fac3..b877e5b 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
@@ -26,7 +24,7 @@
  * GPL HEADER END
  */
 /*
- * Copyright  2009 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  */
 /*
  * The copytool acts on action requests from Lustre to copy files to and from
  * an HSM archive system.
  *
- * Note: under Linux, until llapi_copytool_fini is called (or the program is
+ * Note: under Linux, until llapi_hsm_copytool_fini is called (or the program is
  * killed), the libcfs module will be referenced and unremovable,
  * even after Lustre services stop.
  */
 
 #include <stdio.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <signal.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <unistd.h>
+
 #include <libcfs/libcfs.h>
-#include <lustre/lustre_user.h>
-#include <lustre/liblustreapi.h>
+#include <lustre/lustreapi.h>
+
+struct hsm_copytool_private *ctdata;
+
+void handler(int signal ) {
+        psignal(signal, "exiting");
+        /* If we don't clean up upon interrupt, umount thinks there's a ref
+         * and doesn't remove us from mtab (EINPROGRESS). The lustre client
+         * does successfully unmount and the mount is actually gone, but the
+         * mtab entry remains. So this just makes mtab happier. */
+       llapi_hsm_copytool_fini(&ctdata);
+        exit(1);
+}
 
-int main() {
-        void *ctdata;
-        int archive_nums[] = {1}; /* which archive numbers we care about */
+int main(int argc, char **argv) {
+        int c, test = 0;
+        struct option long_opts[] = {
+                {"test", no_argument, 0, 't'},
+                {0, 0, 0, 0}
+        };
+        int archives[] = {1}; /* which archives we care about */
         int rc;
 
-        rc = llapi_copytool_start(&ctdata, 0, ARRAY_SIZE(archive_nums),
-                                  archive_nums);
+        optind = 0;
+        while ((c = getopt_long(argc, argv, "t", long_opts, NULL)) != -1) {
+                switch (c) {
+                case 't':
+                        test++;
+                        break;
+                default:
+                        fprintf(stderr, "error: %s: option '%s' unrecognized\n",
+                                argv[0], argv[optind - 1]);
+                        return EINVAL;
+                }
+        }
+
+        if (optind != argc - 1) {
+                fprintf(stderr, "Usage: %s <fsname>\n", argv[0]);
+                return -EINVAL;
+        }
+
+       rc = llapi_hsm_copytool_start(&ctdata, argv[optind], 0,
+                                  ARRAY_SIZE(archives), archives);
         if (rc < 0) {
                 fprintf(stderr, "Can't start copytool interface: %s\n",
                         strerror(-rc));
-                return rc;
+                return -rc;
         }
 
+        if (test)
+               return -llapi_hsm_copytool_fini(&ctdata);
+
         printf("Waiting for message from kernel (pid=%d)\n", getpid());
 
+        signal(SIGINT, handler);
+
         while(1) {
-                struct hsm_action_list *hal;
-                struct hsm_action_item *hai;
-                int msgsize, i = 0;
+               struct hsm_action_list *hal;
+               struct hsm_action_item *hai;
+               int msgsize, i = 0;
 
-                rc = llapi_copytool_recv(ctdata, &hal, &msgsize);
-                if (rc == -ESHUTDOWN) {
-                        fprintf(stderr, "shutting down");
-                        break;
-                }
-                if (rc < 0) {
-                        fprintf(stderr, "Message receive: %s", strerror(-rc));
-                        break;
-                }
-                if (msgsize == 0)
-                        continue; /* msg not for us */
-
-                printf("Copytool fs=%s archive#=%d item_count=%d\n",
-                       hal->hal_fsname, hal->hal_archive_num, hal->hal_count);
-
-                hai = hai_zero(hal);
-                while (++i <= hal->hal_count) {
-                        printf("Item %d: action %d reclen %d\n", i,
-                               hai->hai_action, hai->hai_len);
-                        printf(" "DFID" gid="LPU64" cookie="LPU64"\n",
-                               PFID(&hai->hai_fid), hai->hai_gid,
-                               hai->hai_cookie);
-                        hai = hai_next(hai);
-                }
+               rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize);
+               if (rc == -ESHUTDOWN) {
+                       fprintf(stderr, "shutting down");
+                       break;
+               }
+               if (rc < 0) {
+                       fprintf(stderr, "Message receive: %s", strerror(-rc));
+                       break;
+               }
+               if (msgsize == 0)
+                       continue; /* msg not for us */
+
+               printf("Copytool fs=%s archive#=%d item_count=%d\n",
+                       hal->hal_fsname, hal->hal_archive_id, hal->hal_count);
+
+               hai = hai_zero(hal);
+               while (++i <= hal->hal_count) {
+                       printf("Item %d: action %d reclen %d\n", i,
+                               hai->hai_action, hai->hai_len);
+                       printf(" "DFID" gid="LPU64" cookie="LPU64"\n",
+                               PFID(&hai->hai_fid), hai->hai_gid,
+                               hai->hai_cookie);
+                       hai = hai_next(hai);
+               }
 
-                llapi_copytool_free(&hal);
+               llapi_hsm_copytool_free(&hal);
         }
 
-        llapi_copytool_fini(&ctdata);
+       llapi_hsm_copytool_fini(&ctdata);
 
-        return 0;
+        return -rc;
 }