geom.c (8494b7389a65f9042ecc4b2460fbeccb0c73927a) geom.c (83d165c127f2e3734d9ab58ef161a4b0e490e5c0)
1/*-
2 * Copyright (c) 2004-2009 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 62 unchanged lines hidden (view full) ---

71static void std_help(struct gctl_req *req, unsigned flags);
72static void std_list(struct gctl_req *req, unsigned flags);
73static void std_status(struct gctl_req *req, unsigned flags);
74static void std_load(struct gctl_req *req, unsigned flags);
75static void std_unload(struct gctl_req *req, unsigned flags);
76
77struct g_command std_commands[] = {
78 { "help", 0, std_help, G_NULL_OPTS, NULL },
1/*-
2 * Copyright (c) 2004-2009 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 62 unchanged lines hidden (view full) ---

71static void std_help(struct gctl_req *req, unsigned flags);
72static void std_list(struct gctl_req *req, unsigned flags);
73static void std_status(struct gctl_req *req, unsigned flags);
74static void std_load(struct gctl_req *req, unsigned flags);
75static void std_unload(struct gctl_req *req, unsigned flags);
76
77struct g_command std_commands[] = {
78 { "help", 0, std_help, G_NULL_OPTS, NULL },
79 { "list", 0, std_list, G_NULL_OPTS,
80 "[name ...]"
79 { "list", 0, std_list,
80 {
81 { 'a', "all", NULL, G_TYPE_BOOL },
82 G_OPT_SENTINEL
83 },
84 "[-a] [name ...]"
81 },
82 { "status", 0, std_status,
83 {
85 },
86 { "status", 0, std_status,
87 {
88 { 'a', "all", NULL, G_TYPE_BOOL },
89 { 'g', "geoms", NULL, G_TYPE_BOOL },
84 { 's', "script", NULL, G_TYPE_BOOL },
85 G_OPT_SENTINEL
86 },
90 { 's', "script", NULL, G_TYPE_BOOL },
91 G_OPT_SENTINEL
92 },
87 "[-s] [name ...]"
93 "[-ags] [name ...]"
88 },
89 { "load", G_FLAG_VERBOSE | G_FLAG_LOADKLD, std_load, G_NULL_OPTS,
90 NULL },
91 { "unload", G_FLAG_VERBOSE, std_unload, G_NULL_OPTS, NULL },
92 G_CMD_SENTINEL
93};
94
95static void

--- 685 unchanged lines hidden (view full) ---

781
782static void
783std_list(struct gctl_req *req, unsigned flags __unused)
784{
785 struct gmesh mesh;
786 struct gclass *classp;
787 struct ggeom *gp;
788 const char *name;
94 },
95 { "load", G_FLAG_VERBOSE | G_FLAG_LOADKLD, std_load, G_NULL_OPTS,
96 NULL },
97 { "unload", G_FLAG_VERBOSE, std_unload, G_NULL_OPTS, NULL },
98 G_CMD_SENTINEL
99};
100
101static void

--- 685 unchanged lines hidden (view full) ---

787
788static void
789std_list(struct gctl_req *req, unsigned flags __unused)
790{
791 struct gmesh mesh;
792 struct gclass *classp;
793 struct ggeom *gp;
794 const char *name;
789 int error, i, nargs;
795 int all, error, i, nargs;
790
791 error = geom_gettree(&mesh);
792 if (error != 0)
793 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
794 classp = find_class(&mesh, gclass_name);
795 if (classp == NULL) {
796 geom_deletetree(&mesh);
797 errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
798 }
799 nargs = gctl_get_int(req, "nargs");
796
797 error = geom_gettree(&mesh);
798 if (error != 0)
799 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
800 classp = find_class(&mesh, gclass_name);
801 if (classp == NULL) {
802 geom_deletetree(&mesh);
803 errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
804 }
805 nargs = gctl_get_int(req, "nargs");
806 all = gctl_get_int(req, "all");
800 if (nargs > 0) {
801 for (i = 0; i < nargs; i++) {
802 name = gctl_get_ascii(req, "arg%d", i);
803 gp = find_geom(classp, name);
807 if (nargs > 0) {
808 for (i = 0; i < nargs; i++) {
809 name = gctl_get_ascii(req, "arg%d", i);
810 gp = find_geom(classp, name);
804 if (gp != NULL)
805 list_one_geom(gp);
806 else
811 if (gp == NULL)
807 errx(EXIT_FAILURE, "No such geom: %s.", name);
812 errx(EXIT_FAILURE, "No such geom: %s.", name);
813 list_one_geom(gp);
808 }
809 } else {
810 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
814 }
815 } else {
816 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
811 if (LIST_EMPTY(&gp->lg_provider))
817 if (LIST_EMPTY(&gp->lg_provider) && !all)
812 continue;
813 list_one_geom(gp);
814 }
815 }
816 geom_deletetree(&mesh);
817}
818
819static int
820std_status_available(void)
821{
822
823 /* 'status' command is available when 'list' command is. */
824 return (std_list_available());
825}
826
827static void
828status_update_len(struct ggeom *gp, int *name_len, int *status_len)
829{
818 continue;
819 list_one_geom(gp);
820 }
821 }
822 geom_deletetree(&mesh);
823}
824
825static int
826std_status_available(void)
827{
828
829 /* 'status' command is available when 'list' command is. */
830 return (std_list_available());
831}
832
833static void
834status_update_len(struct ggeom *gp, int *name_len, int *status_len)
835{
830 struct gprovider *pp;
831 struct gconfig *conf;
832 int len;
833
834 assert(gp != NULL);
835 assert(name_len != NULL);
836 assert(status_len != NULL);
837
836 struct gconfig *conf;
837 int len;
838
839 assert(gp != NULL);
840 assert(name_len != NULL);
841 assert(status_len != NULL);
842
838 pp = LIST_FIRST(&gp->lg_provider);
839 if (pp != NULL)
840 len = strlen(pp->lg_name);
841 else
842 len = strlen(gp->lg_name);
843 len = strlen(gp->lg_name);
843 if (*name_len < len)
844 *name_len = len;
845 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
846 if (strcasecmp(conf->lg_name, "state") == 0) {
847 len = strlen(conf->lg_val);
848 if (*status_len < len)
849 *status_len = len;
850 }
851 }
852}
853
844 if (*name_len < len)
845 *name_len = len;
846 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
847 if (strcasecmp(conf->lg_name, "state") == 0) {
848 len = strlen(conf->lg_val);
849 if (*status_len < len)
850 *status_len = len;
851 }
852 }
853}
854
855static void
856status_update_len_prs(struct ggeom *gp, int *name_len, int *status_len)
857{
858 struct gprovider *pp;
859 struct gconfig *conf;
860 int len, glen;
861
862 assert(gp != NULL);
863 assert(name_len != NULL);
864 assert(status_len != NULL);
865
866 glen = 0;
867 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
868 if (strcasecmp(conf->lg_name, "state") == 0) {
869 glen = strlen(conf->lg_val);
870 break;
871 }
872 }
873 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
874 len = strlen(pp->lg_name);
875 if (*name_len < len)
876 *name_len = len;
877 len = glen;
878 LIST_FOREACH(conf, &pp->lg_config, lg_config) {
879 if (strcasecmp(conf->lg_name, "state") == 0) {
880 len = strlen(conf->lg_val);
881 break;
882 }
883 }
884 if (*status_len < len)
885 *status_len = len;
886 }
887}
888
854static char *
855status_one_consumer(struct gconsumer *cp)
856{
857 static char buf[256];
858 struct gprovider *pp;
859 struct gconfig *conf;
889static char *
890status_one_consumer(struct gconsumer *cp)
891{
892 static char buf[256];
893 struct gprovider *pp;
894 struct gconfig *conf;
895 const char *state, *syncr;
860
861 pp = cp->lg_provider;
862 if (pp == NULL)
863 return (NULL);
896
897 pp = cp->lg_provider;
898 if (pp == NULL)
899 return (NULL);
900 state = NULL;
901 syncr = NULL;
864 LIST_FOREACH(conf, &cp->lg_config, lg_config) {
902 LIST_FOREACH(conf, &cp->lg_config, lg_config) {
903 if (strcasecmp(conf->lg_name, "state") == 0)
904 state = conf->lg_val;
865 if (strcasecmp(conf->lg_name, "synchronized") == 0)
905 if (strcasecmp(conf->lg_name, "synchronized") == 0)
866 break;
906 syncr = conf->lg_val;
867 }
907 }
868 if (conf == NULL)
908 if (state == NULL && syncr == NULL)
869 snprintf(buf, sizeof(buf), "%s", pp->lg_name);
909 snprintf(buf, sizeof(buf), "%s", pp->lg_name);
870 else {
910 else if (state != NULL && syncr != NULL) {
911 snprintf(buf, sizeof(buf), "%s (%s, %s)", pp->lg_name,
912 state, syncr);
913 } else {
871 snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name,
914 snprintf(buf, sizeof(buf), "%s (%s)", pp->lg_name,
872 conf->lg_val);
915 state ? state : syncr);
873 }
874 return (buf);
875}
876
877static void
878status_one_geom(struct ggeom *gp, int script, int name_len, int status_len)
879{
916 }
917 return (buf);
918}
919
920static void
921status_one_geom(struct ggeom *gp, int script, int name_len, int status_len)
922{
880 struct gprovider *pp;
881 struct gconsumer *cp;
882 struct gconfig *conf;
883 const char *name, *status, *component;
884 int gotone;
885
923 struct gconsumer *cp;
924 struct gconfig *conf;
925 const char *name, *status, *component;
926 int gotone;
927
886 pp = LIST_FIRST(&gp->lg_provider);
887 if (pp != NULL)
888 name = pp->lg_name;
889 else
890 name = gp->lg_name;
928 name = gp->lg_name;
929 status = "N/A";
891 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
930 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
892 if (strcasecmp(conf->lg_name, "state") == 0)
931 if (strcasecmp(conf->lg_name, "state") == 0) {
932 status = conf->lg_val;
893 break;
933 break;
934 }
894 }
935 }
895 if (conf == NULL)
896 status = "N/A";
897 else
898 status = conf->lg_val;
899 gotone = 0;
900 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
901 component = status_one_consumer(cp);
902 if (component == NULL)
903 continue;
904 gotone = 1;
905 printf("%*s %*s %s\n", name_len, name, status_len, status,
906 component);
907 if (!script)
908 name = status = "";
909 }
910 if (!gotone) {
911 printf("%*s %*s %s\n", name_len, name, status_len, status,
912 "N/A");
913 }
914}
915
916static void
936 gotone = 0;
937 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
938 component = status_one_consumer(cp);
939 if (component == NULL)
940 continue;
941 gotone = 1;
942 printf("%*s %*s %s\n", name_len, name, status_len, status,
943 component);
944 if (!script)
945 name = status = "";
946 }
947 if (!gotone) {
948 printf("%*s %*s %s\n", name_len, name, status_len, status,
949 "N/A");
950 }
951}
952
953static void
954status_one_geom_prs(struct ggeom *gp, int script, int name_len, int status_len)
955{
956 struct gprovider *pp;
957 struct gconsumer *cp;
958 struct gconfig *conf;
959 const char *name, *status, *component;
960 int gotone;
961
962 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
963 name = pp->lg_name;
964 status = "N/A";
965 LIST_FOREACH(conf, &gp->lg_config, lg_config) {
966 if (strcasecmp(conf->lg_name, "state") == 0) {
967 status = conf->lg_val;
968 break;
969 }
970 }
971 LIST_FOREACH(conf, &pp->lg_config, lg_config) {
972 if (strcasecmp(conf->lg_name, "state") == 0) {
973 status = conf->lg_val;
974 break;
975 }
976 }
977 gotone = 0;
978 LIST_FOREACH(cp, &gp->lg_consumer, lg_consumer) {
979 component = status_one_consumer(cp);
980 if (component == NULL)
981 continue;
982 gotone = 1;
983 printf("%*s %*s %s\n", name_len, name,
984 status_len, status, component);
985 if (!script)
986 name = status = "";
987 }
988 if (!gotone) {
989 printf("%*s %*s %s\n", name_len, name,
990 status_len, status, "N/A");
991 }
992 }
993}
994
995static void
917std_status(struct gctl_req *req, unsigned flags __unused)
918{
919 struct gmesh mesh;
920 struct gclass *classp;
921 struct ggeom *gp;
922 const char *name;
923 int name_len, status_len;
996std_status(struct gctl_req *req, unsigned flags __unused)
997{
998 struct gmesh mesh;
999 struct gclass *classp;
1000 struct ggeom *gp;
1001 const char *name;
1002 int name_len, status_len;
924 int error, i, n, nargs, script;
1003 int all, error, geoms, i, n, nargs, script;
925
926 error = geom_gettree(&mesh);
927 if (error != 0)
928 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
929 classp = find_class(&mesh, gclass_name);
930 if (classp == NULL)
931 errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
932 nargs = gctl_get_int(req, "nargs");
1004
1005 error = geom_gettree(&mesh);
1006 if (error != 0)
1007 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
1008 classp = find_class(&mesh, gclass_name);
1009 if (classp == NULL)
1010 errx(EXIT_FAILURE, "Class %s not found.", gclass_name);
1011 nargs = gctl_get_int(req, "nargs");
1012 all = gctl_get_int(req, "all");
1013 geoms = gctl_get_int(req, "geoms");
933 script = gctl_get_int(req, "script");
1014 script = gctl_get_int(req, "script");
934 name_len = strlen("Name");
935 status_len = strlen("Status");
1015 if (script) {
1016 name_len = 0;
1017 status_len = 0;
1018 } else {
1019 name_len = strlen("Name");
1020 status_len = strlen("Status");
1021 }
936 if (nargs > 0) {
937 for (i = 0, n = 0; i < nargs; i++) {
938 name = gctl_get_ascii(req, "arg%d", i);
939 gp = find_geom(classp, name);
940 if (gp == NULL)
941 errx(EXIT_FAILURE, "No such geom: %s.", name);
1022 if (nargs > 0) {
1023 for (i = 0, n = 0; i < nargs; i++) {
1024 name = gctl_get_ascii(req, "arg%d", i);
1025 gp = find_geom(classp, name);
1026 if (gp == NULL)
1027 errx(EXIT_FAILURE, "No such geom: %s.", name);
942 else {
943 status_update_len(gp, &name_len, &status_len);
944 n++;
1028 if (geoms) {
1029 status_update_len(gp,
1030 &name_len, &status_len);
1031 } else {
1032 status_update_len_prs(gp,
1033 &name_len, &status_len);
945 }
1034 }
1035 n++;
946 }
947 if (n == 0)
948 goto end;
949 } else {
950 n = 0;
951 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1036 }
1037 if (n == 0)
1038 goto end;
1039 } else {
1040 n = 0;
1041 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
952 if (LIST_EMPTY(&gp->lg_provider))
1042 if (LIST_EMPTY(&gp->lg_provider) && !all)
953 continue;
1043 continue;
954 status_update_len(gp, &name_len, &status_len);
1044 if (geoms) {
1045 status_update_len(gp,
1046 &name_len, &status_len);
1047 } else {
1048 status_update_len_prs(gp,
1049 &name_len, &status_len);
1050 }
955 n++;
956 }
957 if (n == 0)
958 goto end;
959 }
960 if (!script) {
961 printf("%*s %*s %s\n", name_len, "Name", status_len, "Status",
962 "Components");
963 }
964 if (nargs > 0) {
965 for (i = 0; i < nargs; i++) {
966 name = gctl_get_ascii(req, "arg%d", i);
967 gp = find_geom(classp, name);
1051 n++;
1052 }
1053 if (n == 0)
1054 goto end;
1055 }
1056 if (!script) {
1057 printf("%*s %*s %s\n", name_len, "Name", status_len, "Status",
1058 "Components");
1059 }
1060 if (nargs > 0) {
1061 for (i = 0; i < nargs; i++) {
1062 name = gctl_get_ascii(req, "arg%d", i);
1063 gp = find_geom(classp, name);
968 if (gp != NULL) {
1064 if (gp == NULL)
1065 continue;
1066 if (geoms) {
969 status_one_geom(gp, script, name_len,
970 status_len);
1067 status_one_geom(gp, script, name_len,
1068 status_len);
1069 } else {
1070 status_one_geom_prs(gp, script, name_len,
1071 status_len);
971 }
972 }
973 } else {
974 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1072 }
1073 }
1074 } else {
1075 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
975 if (LIST_EMPTY(&gp->lg_provider))
1076 if (LIST_EMPTY(&gp->lg_provider) && !all)
976 continue;
1077 continue;
977 status_one_geom(gp, script, name_len, status_len);
1078 if (geoms) {
1079 status_one_geom(gp, script, name_len,
1080 status_len);
1081 } else {
1082 status_one_geom_prs(gp, script, name_len,
1083 status_len);
1084 }
978 }
979 }
980end:
981 geom_deletetree(&mesh);
982}
983
984static int
985std_load_available(void)

--- 88 unchanged lines hidden ---
1085 }
1086 }
1087end:
1088 geom_deletetree(&mesh);
1089}
1090
1091static int
1092std_load_available(void)

--- 88 unchanged lines hidden ---