+int
+module_control(int argc, char *argv[])
+{
+ const char *val = NULL;
+ uint64_t last;
+ char *str;
+
+ asl_msg_t *ctl = _asl_server_control_query();
+ if (ctl == NULL)
+ {
+ fprintf(stderr, "can't get status information from syslogd\n");
+ return -1;
+ }
+
+ argc -= 2;
+ argv += 2;
+
+ if (argc < 2)
+ {
+ int first = 1;
+
+ /* print config */
+ asl_out_module_t *m = asl_out_module_init();
+ asl_out_module_t *x = m;
+
+ while (x != NULL)
+ {
+ if ((argc == 0) || (!strcmp(argv[0], x->name)))
+ {
+ asl_msg_lookup(ctl, x->name, &val, NULL);
+
+ if (first == 0) printf("\n");
+ first = 0;
+
+ if (x->name == NULL) printf("ASL out module has no name\n");
+ else printf("ASL out module: %s %s[current status: %s]\n", x->name, (x->flags & MODULE_FLAG_LOCAL) ? "local " : "", (val == NULL) ? "unknown" : val );
+
+ asl_out_module_print(stdout, x);
+ }
+
+ x = x->next;
+ }
+
+ asl_msg_release(ctl);
+ asl_out_module_free(m);
+ return 0;
+ }
+
+ /* name enable [val] */
+ /* name disable [val] */
+ if ((!strcmp(argv[1], "enable")) || (!strcmp(argv[1], "disable")))
+ {
+ int want = -1;
+ int status = -1;
+ asl_msg_t *cm;
+ asl_client_t *ac;
+
+ if (!strcmp(argv[1], "enable"))
+ {
+ if (argc < 3) want = 1;
+ else if (!strcmp(argv[2], "1")) want = 1;
+ else if (!strcmp(argv[2], "0")) want = 0;
+ else
+ {
+ printf("invalid value %s for %s %s - expecting 0 or 1\n", argv[2], argv[0], argv[1]);
+ exit(-1);
+ }
+ }
+ else
+ {
+ if (argc < 3) want = 0;
+ else if (!strcmp(argv[2], "1")) want = 0;
+ else if (!strcmp(argv[2], "0")) want = 1;
+ else
+ {
+ printf("invalid value %s for %s %s - expecting 0 or 1\n", argv[2], argv[0], argv[1]);
+ exit(-1);
+ }
+ }
+
+ asl_msg_lookup(ctl, argv[0], &val, NULL);
+ if (val != NULL)
+ {
+ if (!strcmp(val, "enabled")) status = 1;
+ else status = 0;
+ }
+
+ asl_msg_release(ctl);
+
+ if (want < 0)
+ {
+ printf("internal error: want = -1\n");
+ exit(-1);
+ }
+
+ if (want == status)
+ {
+ printf("module %s is already %s\n", argv[0], val);
+ return 0;
+ }
+
+ cm = asl_msg_new(ASL_TYPE_MSG);
+ asprintf(&str, "@ %s enable %d", argv[0], want);
+
+ if ((cm == NULL) || (str == NULL))
+ {
+ fprintf(stderr, "can't allocate memory - exiting\n");
+ exit(-1);
+ }
+
+ ac = asl_client_open(NULL, NULL, 0);
+ asl_client_set_filter(ac, ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG));
+ asl_msg_set_key_val(cm, ASL_KEY_LEVEL, "7");
+ asl_msg_set_key_val(cm, ASL_KEY_OPTION, "control");
+ asl_msg_set_key_val(cm, ASL_KEY_MSG, str);
+ asl_client_send(ac, cm);
+
+ asl_client_release(ac);
+ asl_msg_release(cm);
+ free(str);
+ return 0;
+ }
+
+ asl_msg_release(ctl);
+
+ /* name checkpoint [file] */
+ if (!strcmp(argv[1], "checkpoint"))
+ {
+ asl_msg_list_t *q = asl_msg_list_new();
+ asl_msg_t *qm = asl_msg_new(ASL_TYPE_QUERY);
+
+ if ((q == NULL) || (qm == NULL))
+ {
+ fprintf(stderr, "can't allocate memory - exiting\n");
+ exit(-1);
+ }
+
+ asl_msg_list_append(q, qm);
+ asl_msg_release(qm);
+
+ asl_msg_set_key_val_op(qm, ASL_KEY_OPTION, "control", ASL_QUERY_OP_EQUAL);
+ asprintf(&str, "%s checkpoint%s%s", argv[0], (argc > 2) ? " " : "", (argc > 2) ? argv[2] : "");
+ asl_msg_set_key_val_op(qm, "action", str, ASL_QUERY_OP_EQUAL);
+
+ asl_msg_list_t *res = syslogd_query((asl_msg_list_t *)q, 0, 0, 1, &last);
+ free(q);
+ asl_msg_list_release(res);
+ return 0;
+ }
+
+ printf("unknown module control: %s\n", argv[1]);
+ exit(-1);
+}
+