int main(int argc, char* argv[]) { char* event, *stream, **list, *desc, *start; int i, c; int verbosity = NC_VERB_ERROR; int listing = 0; int corrupted = 0; int time_start = 0, time_end = -1; xmlDocPtr eventDoc; while ((c = getopt(argc, argv, ARGUMENTS)) != -1) { switch (c) { case 'h': /* Show help */ usage(argv[0]); return EXIT_SUCCESS; case 'l': /* list the streams */ listing = 1; break; case 's': /* time range - start */ time_start = nc_datetime2time(optarg); break; case 'e': /* time range - end */ time_end = nc_datetime2time(optarg); break; case 'v': /* Verbose operation */ verbosity = atoi(optarg); if (verbosity < NC_VERB_ERROR) { verbosity = NC_VERB_ERROR; } else if (verbosity > NC_VERB_DEBUG) { verbosity = NC_VERB_DEBUG; } break; default: fprintf(stderr, "unknown argument -%c", optopt); break; } } if (!listing && (argc - optind) != 1) { if ((argc - optind) < 1) { fprintf(stderr, "Missing stream name\n\n"); } else { /* (argc - optind) > 1 */ fprintf(stderr, "Only a single stream name is allowed\n\n"); } usage(argv[0]); return (EXIT_FAILURE); } stream = argv[optind]; nc_verbosity(verbosity); nc_callback_print(clb_print); c = nc_init(NC_INIT_NOTIF); if (c == -1) { fprintf(stderr, "libnetconf initiation failed."); return (EXIT_FAILURE); } if (listing) { list = ncntf_stream_list(); if (list == NULL || list[0] == NULL) { fprintf(stderr, "There is no NETCONF Event Stream.\n"); return (EXIT_FAILURE); } fprintf(stdout, "NETCONF Event Stream list:\n"); for (i = 0; list[i] != NULL; i++) { if (ncntf_stream_info(list[i], &desc, &start) == 0) { fprintf(stdout, "\t%s\n\t\t%s\n\t\t%s\n", list[i], desc, start); free(desc); free(start); } free(list[i]); } fprintf(stdout, "\n"); free(list); return(EXIT_SUCCESS); } i = 0; ncntf_stream_iter_start(stream); while((event = ncntf_stream_iter_next(stream, time_start, time_end, NULL)) != NULL) { if ((eventDoc = xmlReadMemory(event, strlen(event), NULL, NULL, 0)) != NULL) { fprintf(stdout, "Event:\n"); xmlDocFormatDump(stdout, eventDoc, 1); xmlFreeDoc(eventDoc); i++; } else { fprintf(stdout, "Invalid event format.\n"); corrupted = 1; } free(event); } ncntf_stream_iter_finish(stream); /* print summary */ fprintf(stdout, "\nSummary:\n\tNumber of records: %d\n", i); if (corrupted) { fprintf(stdout, "\tSTREAM FILE IS CORRUPTED!\n"); } ncntf_close(); return (EXIT_SUCCESS); }
API int nc_close(void) { int retval = 0, i, fd; char my_comm[NC_APPS_COMM_MAX+1]; #ifndef DISABLE_LIBSSH if (nc_init_flags & NC_INIT_LIBSSH_PTHREAD) { ssh_finalize(); } #endif if (nc_init_flags & NC_INIT_CLIENT) { return (retval); } /* get my comm */ my_comm[0] = '\0'; fd = open("/proc/self/comm", O_RDONLY); if (fd != -1) { i = read(fd, my_comm, NC_APPS_COMM_MAX); close(fd); if (i > 0) { if (my_comm[i-1] == '\n') { my_comm[i-1] = '\0'; } else { my_comm[i] = '\0'; } } } nc_init_flags |= NC_INIT_CLOSING; /* nc_apps_check() also deletes this process's info from the shared memory */ if (nc_info != NULL) { /* LOCK */ pthread_rwlock_wrlock(&(nc_info->lock)); if (nc_apps_check(my_comm, &(nc_info->apps)) == 1 && nc_init_flags & NC_INIT_MULTILAYER) { /* UNLOCK */ pthread_rwlock_unlock(&(nc_info->lock)); /* delete the shared memory */ retval = nc_shared_cleanup(1); } else { nc_info->stats.participants--; /* UNLOCK */ pthread_rwlock_unlock(&(nc_info->lock)); } #ifdef POSIX_SHM munmap(nc_info, sizeof(struct nc_shared_info)); #else shmdt(nc_info); #endif /* #ifdef POSIX_SHM */ nc_info = NULL; } if (retval == -1) { nc_init_flags &= ~NC_INIT_CLOSING; return (retval); } /* close NETCONF session statistics */ if (nc_init_flags & NC_INIT_MONITORING) { nc_session_monitoring_close(); } /* close all remaining datastores */ if (nc_init_flags & NC_INIT_DATASTORES) { ncds_cleanall(); } #ifndef DISABLE_NOTIFICATIONS /* close Notification subsystem */ if (nc_init_flags & NC_INIT_NOTIF) { ncntf_close(); } #endif /* close Access Control subsystem */ if (nc_init_flags & NC_INIT_NACM) { nacm_close(); } xsltCleanupGlobals(); xmlCleanupParser(); nc_init_flags = 0; return (retval); }