int send_record(char *payload)
{
        struct telem_ref *t_ref;
        int ret = 0;

        if (tm_create_record(&t_ref, (uint32_t)severity,
                             opt_class, payload_version) < 0) {
                goto out1;
        }

        if (tm_set_payload(t_ref, payload) < 0) {
                goto out2;
        }

        if (tm_send_record(t_ref) >= 0) {
                ret = 1;
        }

out2:
        tm_free_record(t_ref);
out1:
        return ret;
}
示例#2
0
int main(int argc, char **argv)
{
        uint32_t severity = 1;
        uint32_t payload_version = 1;
        char classification[20] = "misc/life/hello";
        struct telem_ref *tm_handle = NULL;
        char *payload2;

        // Following vars are for arg parsing.
        int c;
        char *cfile = NULL;
        int opt_index = 0;
        int ret = 0;

        struct option opts[] = {
                { "cfile", 1, NULL, 'f' },
                { "help", 0, NULL, 'h' },
                { "version", 0, NULL, 'V' },
                { NULL, 0, NULL, 0 }
        };

        while ((c = getopt_long(argc, argv, "f:hV", opts, &opt_index)) != -1) {
                switch (c) {
                        case 'f':
                                if (asprintf(&cfile, "%s", (char *)optarg) < 0) {
                                        exit(EXIT_FAILURE);
                                }

                                struct stat buf;
                                ret = stat(cfile, &buf);

                                /* check if the file exists and is a regular file */
                                if (ret == -1 || !S_ISREG(buf.st_mode)) {
                                        exit(EXIT_FAILURE);
                                }

                                tm_set_config_file(cfile);
                                break;
                        case 'h':
                                print_usage(argv[0]);
                                exit(EXIT_SUCCESS);
                        case 'V':
                                printf(PACKAGE_VERSION "\n");
                                exit(EXIT_SUCCESS);
                        case '?':
                                exit(EXIT_FAILURE);
                }
        }

        if (asprintf(&payload2, "hello\n") < 0) {
                exit(EXIT_FAILURE);
        }

        tm_handle = tm_create_record(severity, classification, payload_version);
        tm_set_payload(tm_handle,  payload2);

        free(payload2);

        if (!tm_send_record(tm_handle)) {
                printf("Failed to send record to daemon.\n");
                return 1;
        } else {
                printf("Successfully sent record to daemon.\n");
                return 0;
        }

        tm_free_record(tm_handle);
        tm_handle = NULL;
}