int pclDeinitLibrary(void) { int rval = 1; pthread_mutex_lock(&gInitMutex); if(gPclInitCounter == 1) { DLT_LOG(gPclDLTContext, DLT_LOG_DEBUG, DLT_STRING("pclDeinitLibrary - DEINIT client lib - "), DLT_STRING(gAppId), DLT_STRING("- init counter: "), DLT_INT(gPclInitCounter)); rval = private_pclDeinitLibrary(); gPclInitCounter--; // decrement init counter DLT_UNREGISTER_CONTEXT(gPclDLTContext); } else if(gPclInitCounter > 1) { DLT_LOG(gPclDLTContext, DLT_LOG_DEBUG, DLT_STRING("pclDeinitLibrary - DEINIT client lib - "), DLT_STRING(gAppId), DLT_STRING("- ONLY DECREMENT init counter: "), DLT_INT(gPclInitCounter)); gPclInitCounter--; // decrement init counter } else { DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("pclDeinitLibrary - DEINIT client lib - "), DLT_STRING(gAppId), DLT_STRING("- NOT INITIALIZED: ")); rval = EPERS_NOT_INITIALIZED; } pthread_mutex_unlock(&gInitMutex); return rval; }
void send_file(LogFileOptions fileopt, int n) { DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("dlt-system-logfile, sending file.")); FILE * pFile; DltContext context = logfileContext[n]; char buffer[1024]; int bytes; int seq = 1; pFile = fopen(fileopt.Filename[n],"r"); if(pFile != NULL) { while (!feof(pFile)) { bytes = fread(buffer,1,sizeof(buffer)-1,pFile); if(bytes>=0) buffer[bytes] = 0; else buffer[0] = 0; if(feof(pFile)) { DLT_LOG(context, DLT_LOG_INFO, DLT_INT(seq*-1), DLT_STRING(buffer)); break; } else { DLT_LOG(context, DLT_LOG_INFO, DLT_INT(seq++), DLT_STRING(buffer)); } } fclose(pFile); } else { DLT_LOG(dltsystem, DLT_LOG_ERROR, DLT_STRING("dlt-system-logfile, failed to open file."), DLT_STRING(fileopt.Filename[n])); } }
/** * la_handler_service_deregister_consumers: * @service: A #LAHandlerService. * * Unregisters every #ShutdownClient from the Node State Manager. * This method is typically used when the #LAHandlerService is about to shut down. */ void la_handler_service_deregister_consumers (LAHandlerService *service) { GHashTableIter iter; ShutdownClient *client; const gchar *bus_name; const gchar *object_path; const gchar *unit; GError *error = NULL; gint error_code; gint shutdown_mode; g_return_if_fail (LA_HANDLER_IS_SERVICE (service)); g_hash_table_iter_init (&iter, service->clients_to_units); while (g_hash_table_iter_next (&iter, (gpointer *)&client, (gpointer *)&unit)) { /* extract data from the current client */ bus_name = shutdown_client_get_bus_name (client); object_path = shutdown_client_get_object_path (client); shutdown_mode = shutdown_client_get_shutdown_mode (client); /* unregister the shutdown client */ nsm_consumer_call_un_register_shutdown_client_sync (service->nsm_consumer, bus_name, object_path, shutdown_mode, &error_code, NULL, &error); if (error != NULL) { DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING ("Failed to unregister shutdown client:"), DLT_STRING ("object path"), DLT_STRING (object_path), DLT_STRING ("unit"), DLT_STRING (unit), DLT_STRING ("error message"), DLT_STRING (error->message)); g_error_free (error); } else if (error_code != NSM_ERROR_STATUS_OK) { DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING ("Failed to unregister shutdown client:"), DLT_STRING ("object path"), DLT_STRING (object_path), DLT_STRING ("unit"), DLT_STRING (unit), DLT_STRING ("error code"), DLT_INT (error_code)); } } }
int dlt_shell_injection_callback(uint32_t service_id, void *data, uint32_t length) { DLT_LOG(dltsystem,DLT_LOG_DEBUG, DLT_STRING("dlt-system-shell, injection callback")); char text[1024]; int syserr = 0; strncpy(text,data,length); DLT_LOG(dltsystem,DLT_LOG_DEBUG, DLT_STRING("dlt-system-shell, injection injection id:"), DLT_UINT32(service_id)); DLT_LOG(dltsystem,DLT_LOG_DEBUG, DLT_STRING("dlt-system-shell, injection data:"), DLT_STRING(text)); switch(service_id) { case 0x1001: if((syserr = system(text)) != 0) { DLT_LOG(dltsystem,DLT_LOG_ERROR, DLT_STRING("dlt-system-shell, abnormal exit status."), DLT_STRING(text), DLT_INT(syserr)); } break; default: DLT_LOG(dltsystem,DLT_LOG_ERROR, DLT_STRING("dlt-system-shell, unknown command received."), DLT_UINT32(service_id), DLT_STRING(text)); break; } return 0; }
int pclInitLibrary(const char* appName, int shutdownMode) { int rval = 1; pthread_mutex_lock(&gInitMutex); if(gPclInitCounter == 0) { DLT_REGISTER_CONTEXT(gPclDLTContext,"PCL","Context for persistence client library logging"); DLT_LOG(gPclDLTContext, DLT_LOG_DEBUG, DLT_STRING("pclInitLibrary => INIT PCL - "), DLT_STRING(appName), DLT_STRING("- init counter: "), DLT_INT(gPclInitCounter) ); rval = private_pclInitLibrary(appName, shutdownMode); } else { DLT_LOG(gPclDLTContext, DLT_LOG_DEBUG, DLT_STRING("pclInitLibrary - INIT PCL - "), DLT_STRING(gAppId), DLT_STRING("- ONLY INCREMENT init counter: "), DLT_INT(gPclInitCounter) ); } gPclInitCounter++; // increment after private init, otherwise atomic access is too early pthread_mutex_unlock(&gInitMutex); return rval; }
int wait_for_files(FiletransferOptions const *opts) { #ifdef linux DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("dlt-system-filetransfer, waiting for files.")); static char buf[INOTIFY_LEN]; ssize_t len = read(ino.handle, buf, INOTIFY_LEN); if(len < 0) { DLT_LOG(filetransferContext, DLT_LOG_ERROR, DLT_STRING("Error while waiting for files in dlt-system file transfer.")); return -1; } unsigned int i = 0; while(i < (len-INOTIFY_SZ)) { struct inotify_event *ie = (struct inotify_event *)&buf[i]; if(ie->len > 0) { if((ie->mask & IN_CLOSE_WRITE) || (ie->mask & IN_MOVED_TO)) { int j; for(j = 0;j < opts->Count;j++) { if(ie->wd == ino.fd[j]) { DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("dlt-system-filetransfer, found new file.")); int length = strlen(opts->Directory[j])+ie->len+1; if (length > PATH_MAX) { DLT_LOG(filetransferContext, DLT_LOG_ERROR, DLT_STRING("dlt-system-filetransfer: Very long path for file transfer. Cancelling transfer! Length is: "),DLT_INT(length)); return -1; } char *tosend = malloc(length); snprintf(tosend,length, "%s/%s", opts->Directory[j], ie->name); send_one(tosend, opts, j); free(tosend); } } } } i += INOTIFY_SZ + ie->len; } #endif return 0; }
/**On startup, the inotifiy handlers are created, and existing files shall be sent into DLT stream * @param opts FiletransferOptions * @return Returns 0 if everything was okay. If there was a failure a value < 0 will be returned. */ int init_filetransfer_dirs(FiletransferOptions const *opts) { DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("dlt-system-filetransfer, initializing inotify on directories.")); int i; #ifdef linux ino.handle = inotify_init(); if(ino.handle < 0) { DLT_LOG(filetransferContext, DLT_LOG_FATAL, DLT_STRING("Failed to initialize inotify in dlt-system file transfer.")); return -1; } #endif for(i = 0;i < opts->Count;i++) { //create subdirectories for processing the files char *subdirpath; int len = strlen(opts->Directory[i])+strlen(SUBDIR_COMPRESS)+2; subdirpath= malloc (len); MALLOC_ASSERT(subdirpath); snprintf(subdirpath,len,"%s/%s",opts->Directory[i],SUBDIR_COMPRESS); int ret = mkdir(subdirpath,0777); if (0 != ret && EEXIST != errno){ DLT_LOG(dltsystem, DLT_LOG_ERROR, DLT_STRING("dlt-system-filetransfer, error creating subdirectory: "),DLT_STRING(subdirpath),DLT_STRING(" Errorcode: "),DLT_INT(errno)); free (subdirpath); return -1; } free(subdirpath); len = strlen(opts->Directory[i])+strlen(SUBDIR_TOSEND)+2; subdirpath= malloc (len); MALLOC_ASSERT(subdirpath); snprintf(subdirpath,len,"%s/%s",opts->Directory[i],SUBDIR_TOSEND); ret = mkdir(subdirpath,0777); if (0 != ret && EEXIST != errno){ DLT_LOG(dltsystem, DLT_LOG_ERROR, DLT_STRING("dlt-system-filetransfer, error creating subdirectory: "),DLT_STRING(subdirpath),DLT_STRING(" Errorcode: "),DLT_INT(errno)); free (subdirpath); return -1; } free(subdirpath); #ifdef linux ino.fd[i] = inotify_add_watch(ino.handle, opts->Directory[i], IN_CLOSE_WRITE|IN_MOVED_TO); if(ino.fd[i] < 0) { char buf[1024]; snprintf(buf, 1024, "Failed to add inotify watch to directory %s in dlt-system file transfer.", opts->Directory[i]); DLT_LOG(filetransferContext, DLT_LOG_FATAL, DLT_STRING(buf)); return -1; } #endif flush_dir(opts, i); } return 0; }
int pclLifecycleSet(int shutdown) { int rval = 0; if(gShutdownMode == PCL_SHUTDOWN_TYPE_NONE) { if(shutdown == PCL_SHUTDOWN) { DLT_LOG(gPclDLTContext, DLT_LOG_DEBUG, DLT_STRING("lifecycleSet - PCL_SHUTDOWN -"), DLT_STRING(gAppId)); process_prepare_shutdown(Shutdown_Partial); gCancelCounter++; } else if(shutdown == PCL_SHUTDOWN_CANCEL) { DLT_LOG(gPclDLTContext, DLT_LOG_DEBUG, DLT_STRING("lifecycleSet - PCL_SHUTDOWN_CANCEL -"), DLT_STRING(gAppId), DLT_STRING(" Cancel Counter - "), DLT_INT(gCancelCounter)); if(gCancelCounter < Shutdown_MaxCount) { pers_unlock_access(); } else { rval = EPERS_SHUTDOWN_MAX_CANCEL; } } else { rval = EPERS_COMMON; } } else { rval = EPERS_SHUTDOWN_NO_PERMIT; } return rval; }
static void la_handler_service_handle_consumer_lifecycle_request_finish (JobManager *manager, const gchar *unit, const gchar *result, GError *error, gpointer user_data) { LAHandlerServiceData *data = (LAHandlerServiceData *)user_data; GError *err = NULL; gint error_status = NSM_ERROR_STATUS_OK; gint status = NSM_ERROR_STATUS_OK; g_return_if_fail (IS_JOB_MANAGER (manager)); g_return_if_fail (unit != NULL && *unit != '\0'); g_return_if_fail (result != NULL && *result != '\0'); g_return_if_fail (data != NULL); /* log that we are completing a lifecycle request */ DLT_LOG (la_handler_context, DLT_LOG_INFO, DLT_STRING ("Completing a lifecycle request:"), DLT_STRING ("request id"), DLT_UINT (data->request_id)); /* log an error if shutting down the consumer has failed */ if (error != NULL) { DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING ("Failed to shut down a shutdown consumer:"), DLT_STRING (error->message)); /* send an error back to the NSM */ status = NSM_ERROR_STATUS_ERROR; } /* log an error if systemd failed to stop the consumer */ if (g_strcmp0 (result, "failed") == 0) { DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING ("Failed to shutdown a shutdown consumer")); /* send an error back to the NSM */ status = NSM_ERROR_STATUS_ERROR; } /* let the NSM know that we have handled the lifecycle request */ if (!nsm_consumer_call_lifecycle_request_complete_sync (data->service->nsm_consumer, data->request_id, status, &error_status, NULL, &err)) { DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING ("Failed to notify NSM about completed lifecycle request:"), DLT_STRING ("request id"), DLT_UINT (data->request_id), DLT_STRING ("error message"), DLT_STRING (err->message)); g_error_free (err); } else if (error_status == NSM_ERROR_STATUS_OK) { DLT_LOG (la_handler_context, DLT_LOG_INFO, DLT_STRING ("Successfully notified NSM about completed " "lifecycle request:"), DLT_STRING ("request id"), DLT_UINT (data->request_id)); } else { DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING ("Failed to notify NSM about completed lifecycle request:"), DLT_STRING ("request id"), DLT_UINT (data->request_id), DLT_STRING ("error status"), DLT_INT (error_status)); } la_handler_service_data_unref (data); }
static gboolean la_handler_service_handle_register (LAHandler *interface, GDBusMethodInvocation *invocation, const gchar *unit, NSMShutdownType shutdown_mode, guint timeout, LAHandlerService *service) { ShutdownConsumer *consumer; ShutdownClient *client; GError *error = NULL; const gchar *existing_bus_name; const gchar *existing_object_path; gchar *bus_name; gchar *object_path; g_return_val_if_fail (IS_LA_HANDLER (interface), FALSE); g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), FALSE); g_return_val_if_fail (unit != NULL && *unit != '\0', FALSE); g_return_val_if_fail (LA_HANDLER_IS_SERVICE (service), FALSE); if (shutdown_mode != NSM_SHUTDOWN_TYPE_NORMAL && shutdown_mode != NSM_SHUTDOWN_TYPE_FAST && shutdown_mode != (NSM_SHUTDOWN_TYPE_NORMAL | NSM_SHUTDOWN_TYPE_FAST)) { /* the shutdown mode is invalid */ DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING ("Failed to register legacy application: " "invalid shutdown mode"), DLT_INT (shutdown_mode)); la_handler_complete_register (interface, invocation); return TRUE; } /* find out if we have a shutdown client for this unit already */ client = g_hash_table_lookup (service->units_to_clients, unit); if (client != NULL) { /* there already is a shutdown client for the unit, so simply * re-register its client with the new shutdown mode and timeout */ /* extract information from the client */ existing_bus_name = shutdown_client_get_bus_name (client); existing_object_path = shutdown_client_get_object_path (client); /* temporarily store a reference to the legacy app handler service object * in the invocation object */ g_object_set_data_full (G_OBJECT (invocation), "la-handler-service", g_object_ref (service), (GDestroyNotify) g_object_unref); /* re-register the shutdown consumer with the NSM Consumer */ nsm_consumer_call_register_shutdown_client (service->nsm_consumer, existing_bus_name, existing_object_path, shutdown_mode, timeout, NULL, la_handler_service_handle_register_finish, invocation); } else { /* create a new shutdown client and consumer for the unit */ bus_name = "org.genivi.NodeStartupController1"; object_path = g_strdup_printf ("%s/%u", service->prefix, service->index); client = shutdown_client_new (bus_name, object_path, shutdown_mode, timeout); consumer = shutdown_consumer_skeleton_new (); shutdown_client_set_consumer (client, consumer); /* remember the legacy app handler service object in shutdown client */ g_object_set_data_full (G_OBJECT (client), "la-handler-service", g_object_ref (service), (GDestroyNotify) g_object_unref); /* implement the LifecycleRequest method of the shutdown consumer */ g_signal_connect (consumer, "handle-lifecycle-request", G_CALLBACK (la_handler_service_handle_consumer_lifecycle_request), client); /* associate the shutdown client with the unit name */ g_hash_table_insert (service->units_to_clients, g_strdup (unit), g_object_ref (client)); g_hash_table_insert (service->clients_to_units, g_object_ref (client), g_strdup (unit)); /* export the shutdown consumer on the bus */ g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (consumer), service->connection, object_path, &error); if (error != NULL) { DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING ("Failed to export shutdown consumer on the bus:"), DLT_STRING (error->message)); g_error_free (error); } /* temporarily store a reference to the legacy app handler service object * in the invocation object */ g_object_set_data_full (G_OBJECT (invocation), "la-handler-service", g_object_ref (service), (GDestroyNotify) g_object_unref); /* register the shutdown consumer with the NSM Consumer */ nsm_consumer_call_register_shutdown_client (service->nsm_consumer, bus_name, object_path, shutdown_mode, timeout, NULL, la_handler_service_handle_register_finish, invocation); /* free strings and release the shutdown consumer */ g_free (object_path); g_object_unref (consumer); /* increment the counter for our shutdown consumer object paths */ service->index++; } return TRUE; }