int32_t stapi_open(void) { uint32_t ErrorCode; DIR *dirp; struct dirent entry, *dp = NULL; struct stat buf; int32_t i; char pfad[80]; stapi_on = 1; int32_t stapi_priority = 0; dirp = opendir(PROCDIR); if(!dirp) { cs_log("opendir failed (errno=%d %s)", errno, strerror(errno)); return 0; } memset(dev_list, 0, sizeof(struct STDEVICE)*PTINUM); if(dvbapi_priority) { struct s_dvbapi_priority *p; for(p = dvbapi_priority; p != NULL; p = p->next) { if(p->type == 's') { stapi_priority = 1; break; } } } if(!stapi_priority) { cs_log("WARNING: no PTI devices defined, stapi disabled"); return 0; } oscam_stapi5_GetRevision(); oscam_sttkd_GetRevision(); i = 0; while(!cs_readdir_r(dirp, &entry, &dp)) { if(!dp) { break; } snprintf(pfad, sizeof(pfad), "%s%s", PROCDIR, dp->d_name); if(stat(pfad, &buf) != 0) { continue; } if(!(buf.st_mode & S_IFDIR && strncmp(dp->d_name, ".", 1) != 0)) { continue; } int32_t do_open = 0; struct s_dvbapi_priority *p; for(p = dvbapi_priority; p != NULL; p = p->next) { if(p->type != 's') { continue; } if(strcmp(dp->d_name, p->devname) == 0) { do_open = 1; break; } } if(!do_open) { cs_log("PTI: %s skipped", dp->d_name); continue; } ErrorCode = oscam_stapi5_Open(dp->d_name, &dev_list[i].SessionHandle); if(ErrorCode != 0) { cs_log("STPTI_Open ErrorCode: %d", ErrorCode); continue; } //debug //oscam_stapi_Capability(dp->d_name); cs_strncpy(dev_list[i].name, dp->d_name, sizeof(dev_list[i].name)); cs_log("PTI: %s open %d", dp->d_name, i); ErrorCode = oscam_stapi5_SignalAllocate(dev_list[i].SessionHandle, &dev_list[i].SignalHandle); if(ErrorCode != 0) { cs_log("SignalAllocate: ErrorCode: %d SignalHandle: %x", ErrorCode, dev_list[i].SignalHandle); } i++; if(i >= PTINUM) { break; } } closedir(dirp); if(i == 0) { return 0; } uint8_t TKD_InstanceID = 0; memset(&tkd_desc_info, 0, sizeof(tkd_desc_info[0]) * MAX_DESCRAMBLER); for(TKD_InstanceID = 0; TKD_InstanceID < TKD_MAX_NUMBER; TKD_InstanceID++) { /* Generate the device name dynamically based upon the Instance ID */ snprintf(TKD_DeviceName[TKD_InstanceID], sizeof(TKD_DeviceName), "TKD_%02d", TKD_InstanceID); ErrorCode = oscam_sttkd_Open(TKD_DeviceName[TKD_InstanceID], &TKDHandle[TKD_InstanceID]); if(ErrorCode != 0) cs_log("oscam_sttkd_Open: DeviceName: %s, TKDHandle: 0x%08X, ErrorCode: %d", TKD_DeviceName[TKD_InstanceID], TKDHandle[TKD_InstanceID], ErrorCode); } SAFE_MUTEX_INIT(&filter_lock, NULL); for(i = 0; i < PTINUM; i++) { if(dev_list[i].SessionHandle == 0) { continue; } struct read_thread_param *para; if(!cs_malloc(¶, sizeof(struct read_thread_param))) { return 0; } para->id = i; para->cli = cur_client(); int32_t ret = start_thread("stapi read", stapi_read_thread, (void *)para, &dev_list[i].thread, 1, 0); if(ret) { return 0; } } atexit(stapi_off); cs_log("liboscam_stapi5 v.%s initialized", oscam_stapi5_LibVersion()); return 1; }
int32_t stapi_open(void) { uint32_t ErrorCode; DIR *dirp; struct dirent entry, *dp = NULL; struct stat buf; int32_t i; char pfad[80]; stapi_on=1; int32_t stapi_priority=0; dirp = opendir(PROCDIR); if (!dirp) { cs_log("opendir failed (errno=%d %s)", errno, strerror(errno)); return 0; } memset(dev_list, 0, sizeof(struct STDEVICE)*PTINUM); if (dvbapi_priority) { struct s_dvbapi_priority *p; for (p=dvbapi_priority; p != NULL; p=p->next) { if (p->type=='s') { stapi_priority=1; break; } } } if (!stapi_priority) { cs_log("WARNING: no PTI devices defined, stapi disabled"); return 0; } oscam_stapi_CheckVersion(); i=0; while (!cs_readdir_r(dirp, &entry, &dp)) { if (!dp) break; snprintf(pfad, sizeof(pfad), "%s%s", PROCDIR, dp->d_name); if (stat(pfad,&buf) != 0) continue; if (!(buf.st_mode & S_IFDIR && strncmp(dp->d_name, ".", 1)!=0)) continue; int32_t do_open=0; struct s_dvbapi_priority *p; for (p=dvbapi_priority; p != NULL; p=p->next) { if (p->type!='s') continue; if(strcmp(dp->d_name, p->devname)==0) { do_open=1; break; } } if (!do_open) { cs_log("PTI: %s skipped", dp->d_name); continue; } ErrorCode= oscam_stapi_Open(dp->d_name, &dev_list[i].SessionHandle); if (ErrorCode != 0) { cs_log("STPTI_Open ErrorCode: %d", ErrorCode); continue; } //debug //oscam_stapi_Capability(dp->d_name); cs_strncpy(dev_list[i].name,dp->d_name, sizeof(dev_list[i].name)); cs_log("PTI: %s open %d", dp->d_name, i); ErrorCode = oscam_stapi_SignalAllocate(dev_list[i].SessionHandle, &dev_list[i].SignalHandle); if (ErrorCode != 0) cs_log("SignalAllocate: ErrorCode: %d SignalHandle: %x", ErrorCode, dev_list[i].SignalHandle); i++; if (i>=PTINUM) break; } closedir(dirp); if (i==0) return 0; pthread_mutex_init(&filter_lock, NULL); for (i=0;i<PTINUM;i++) { if (dev_list[i].SessionHandle==0) continue; struct read_thread_param *para; if (!cs_malloc(¶, sizeof(struct read_thread_param))) return 0; para->id=i; para->cli=cur_client(); int32_t ret = pthread_create(&dev_list[i].thread, NULL, stapi_read_thread, (void *)para); if(ret){ cs_log("ERROR: can't create stapi read thread (errno=%d %s)", ret, strerror(ret)); return 0; } else pthread_detach(dev_list[i].thread); } atexit(stapi_off); cs_log("liboscam_stapi v.%s initialized", oscam_stapi_LibVersion()); return 1; }
/* * read /proc data into the passed struct pstat * returns 0 on success, -1 on error */ int8_t get_stats_linux(const pid_t pid, struct pstat* result) { // convert pid to string char pid_s[20]; snprintf(pid_s, sizeof(pid_s), "%d", pid); char stat_filepath[30] = "/proc/"; strncat(stat_filepath, pid_s, sizeof(stat_filepath) - strlen(stat_filepath) -1); strncat(stat_filepath, "/stat", sizeof(stat_filepath) - strlen(stat_filepath) -1); FILE *f_pstat = fopen(stat_filepath, "r"); if (f_pstat == NULL) { cs_log("FOPEN ERROR %s",stat_filepath); return -1; } FILE *f_stat = fopen("/proc/stat", "r"); if (!f_stat) { fclose(f_pstat); cs_log("ERROR: Can't open /proc/stat for reading: %s", strerror(errno)); return -1; } // read values from /proc/pid/stat uint64_t rss; if (fscanf(f_pstat, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %" SCNd64 "%" SCNd64 "%" SCNd64 "%" SCNd64 "%*d %*d %*d %*d %*u %" SCNu64 "%" SCNu64, &result->utime_ticks,&result->stime_ticks, &result->cutime_ticks,&result->cstime_ticks,&result->vsize, &rss) == EOF) { fclose(f_pstat); fclose(f_stat); return -1; } fclose(f_pstat); result->rss = rss * getpagesize(); // read+calc cpu total time from /proc/stat int64_t cpu_time[10] = {0,0,0,0,0,0,0,0,0,0}; if (fscanf(f_stat, "%*s %" SCNd64 "%" SCNd64 "%" SCNd64 "%" SCNd64 "%" SCNd64 "%" SCNd64 "%" SCNd64 "%" SCNd64 "%" SCNd64 "%" SCNd64, &cpu_time[0], &cpu_time[1], &cpu_time[2], &cpu_time[3], &cpu_time[4], &cpu_time[5], &cpu_time[6], &cpu_time[7], &cpu_time[8], &cpu_time[9]) == EOF) { fclose(f_stat); return -1; } fclose(f_stat); int8_t i; result->cpu_total_time = 0; // FIXME: On 32 Bit platforms, the single cpu times can overflow quite easily (clock_t from /proc/stat normally refers to a int32 here) resulting in useless calculation results! for(i = 0; i < 10; i++) { result->cpu_total_time += cpu_time[i]; } // read cached from /proc/meminfo uint64_t meminfo_cached = 0; FILE *fd = fopen("/proc/meminfo", "r"); if (fd ) { char line[256]; while(fgets(line, sizeof(line), fd)) { if(sscanf(line, "Cached: %" PRId64" \n kB", &meminfo_cached) == 1){ break; } } } fclose(fd); // read processes from /proc uint info_procs = 0; DIR *hdir; if((hdir = opendir("/proc")) != NULL){ struct dirent entry; struct dirent *dirresult; while(cs_readdir_r(hdir, &entry, &dirresult) == 0 && dirresult != NULL) { if (entry.d_name[0] > '0' && entry.d_name[0] <= '9') { info_procs++; } } closedir(hdir); } // read cpu/meminfo from sysinfo() struct sysinfo info; float shiftfloat = (float)(1 << SI_LOAD_SHIFT); if (!sysinfo(&info)) { // processes result->info_procs = info_procs; // cpu load result->cpu_avg[0] = (float) info.loads[0] / shiftfloat; result->cpu_avg[1] = (float) info.loads[1] / shiftfloat; result->cpu_avg[2] = (float) info.loads[2] / shiftfloat; // meminfo result->mem_total = info.totalram * info.mem_unit; result->mem_free = info.freeram * info.mem_unit; result->mem_used = result->mem_total - result->mem_free; result->mem_buff = info.bufferram * info.mem_unit; result->mem_cached = meminfo_cached * 1024; result->mem_freem = result->mem_free + result->mem_buff + result->mem_cached; result->mem_share = info.sharedram * info.mem_unit; result->mem_total_swap = info.totalswap * info.mem_unit; result->mem_free_swap = info.freeswap * info.mem_unit; result->mem_used_swap = result->mem_total_swap - result->mem_free_swap; } // set timestamp for function call cs_ftime(&result->time_started); return 0; }