int getNumberOfCPUs() { struct var sysvars; int result; result = sysconfig(SYS_GETPARMS, &sysvars, sizeof(struct var)); if (result == -1) return 1; return sysvars.v_ncpus; }
main() { int id; struct cfg_load cl; /* * build data structure for sysconfig */ cl.path = PATHNAME_OF_THE_SYSTEMCALL; cl.libpath = NULL; /* * add the systemcall */ if( (id = sysconfig(SYS_KLOAD, &cl, sizeof(struct cfg_load))) != 0) { perror("sysconfig"); exit(1); } fprintf(stderr, "kmid= %d\n", cl.kmid); fprintf(stdout, "%d", cl.kmid); }
void test_gprs(void) { rt_bool_t res = RT_TRUE; res = sysconfig(); if(!res) return; res = poweron(); if(!res) return; rt_thread_delay(2000); res = gprsinit(); if(!res) return; res = msgsend("abc"); if(!res) return; res = msgreaddata(); if(!res) return; res = gprsconnect(); if(!res) return; res = gprssend("abc"); if(!res) return; res = gprsread(); if(!res) return; res = gprstp(); if(!res) return; res = gprstpoff(); if(!res) return; res = closeconnect(); if(!res) return; poweroff(); }
int main(int argc, char **argv) { char *logical_name, *ptr; char sstring[256]; struct CuDv cudvobj; struct PdDv pddvobj; int rc, how_many, errflg, c, majorno, minorno, unit, verbose; struct cfg_dd cfg; struct cfg_load load; int *minor_list; extern int optind; extern char *optarg; verbose = errflg = 0; logical_name = NULL; while ((c = getopt(argc,argv,"vl:")) != EOF) { switch (c) { case 'v': verbose++; break; case 'l': if (logical_name != NULL) errflg++; logical_name = optarg; break; default: errflg++; } } if (errflg) exit(E_ARGS); if (logical_name == NULL) exit(E_LNAME); if (odm_initialize() == -1) exit(E_ODMINIT); /* Get Customized Device Object for this device */ sprintf(sstring,"name = '%s'",logical_name); rc = (int) odm_get_first(CuDv_CLASS, sstring, &cudvobj); if (rc == 0) { err_exit(E_NOCuDv); } else if (rc == -1) { err_exit(E_ODMGET); } if (cudvobj.status == DEFINED) err_exit(E_OK); /* already unconf'd */ /* get device's predefined object */ sprintf(sstring,"uniquetype = '%s'", cudvobj.PdDvLn_Lvalue); rc = (int) odm_get_first(PdDv_CLASS, sstring, &pddvobj); if (rc == 0) err_exit(E_NOPdDv); else if (rc == -1) err_exit(E_ODMGET); /* * Call sysconfig() to "terminate" the device. * If fails with EBUSY, then device instance is "open", * and device cannot be "unconfigured". Any other errno * returned will be ignored since we MUST unconfigure the * device even if it reports some other error. */ /* get major number of device */ majorno = genmajor(pddvobj.DvDr); if (majorno == -1) { return(E_MAJORNO); } /* get minor number */ minor_list = getminor(majorno, &how_many, pddvobj.DvDr); if (minor_list == NULL || how_many == 0) err_exit (E_MINORNO); vprintf("how_many=%d\n", how_many); ptr = logical_name; ptr += strlen(pddvobj.prefix); unit = atoi(ptr); if (unit >= how_many) { err_exit (E_MINORNO); } minorno = minor_list[unit]; vprintf("unit %d minorno %d\n", unit, minorno); /* create devno for this device */ cfg.devno = makedev(majorno, minorno); cfg.kmid = 0; cfg.ddsptr = (caddr_t) NULL; cfg.ddslen = (int) 0; cfg.cmd = CFG_TERM; if (sysconfig(SYS_CFGDD, &cfg, sizeof(struct cfg_dd)) == -1) { if (errno == EBUSY) err_exit(E_BUSY); } cfg.kmid = loadext(pddvobj.DvDr, FALSE, FALSE); if (cfg.kmid == NULL) err_exit(E_UNLOADEXT); /* Change the status field of device to "DEFINED" */ cudvobj.status = DEFINED; if (odm_change_obj(CuDv_CLASS, &cudvobj) == -1) err_exit(E_ODMUPDATE); /* * Terminate ODM */ odm_terminate(); return (E_OK); }
static int bpf_load(char *errbuf) { long major; int *minors; int numminors, i, rc; char buf[1024]; struct stat sbuf; struct bpf_config cfg_bpf; struct cfg_load cfg_ld; struct cfg_kmod cfg_km; /* * This is very very close to what happens in the real implementation * but I've fixed some (unlikely) bug situations. */ if (bpfloadedflag) return (0); if (bpf_odminit(errbuf) != 0) return (-1); major = genmajor(BPF_NAME); if (major == -1) { snprintf(errbuf, PCAP_ERRBUF_SIZE, "bpf_load: genmajor failed: %s", pcap_strerror(errno)); return (-1); } minors = getminor(major, &numminors, BPF_NAME); if (!minors) { minors = genminor("bpf", major, 0, BPF_MINORS, 1, 1); if (!minors) { snprintf(errbuf, PCAP_ERRBUF_SIZE, "bpf_load: genminor failed: %s", pcap_strerror(errno)); return (-1); } } if (bpf_odmcleanup(errbuf)) return (-1); rc = stat(BPF_NODE "0", &sbuf); if (rc == -1 && errno != ENOENT) { snprintf(errbuf, PCAP_ERRBUF_SIZE, "bpf_load: can't stat %s: %s", BPF_NODE "0", pcap_strerror(errno)); return (-1); } if (rc == -1 || getmajor(sbuf.st_rdev) != major) { for (i = 0; i < BPF_MINORS; i++) { sprintf(buf, "%s%d", BPF_NODE, i); unlink(buf); if (mknod(buf, S_IRUSR | S_IFCHR, domakedev(major, i)) == -1) { snprintf(errbuf, PCAP_ERRBUF_SIZE, "bpf_load: can't mknod %s: %s", buf, pcap_strerror(errno)); return (-1); } } } /* Check if the driver is loaded */ memset(&cfg_ld, 0x0, sizeof(cfg_ld)); cfg_ld.path = buf; sprintf(cfg_ld.path, "%s/%s", DRIVER_PATH, BPF_NAME); if ((sysconfig(SYS_QUERYLOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) || (cfg_ld.kmid == 0)) { /* Driver isn't loaded, load it now */ if (sysconfig(SYS_SINGLELOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) { snprintf(errbuf, PCAP_ERRBUF_SIZE, "bpf_load: could not load driver: %s", strerror(errno)); return (-1); } } /* Configure the driver */ cfg_km.cmd = CFG_INIT; cfg_km.kmid = cfg_ld.kmid; cfg_km.mdilen = sizeof(cfg_bpf); cfg_km.mdiptr = (void *)&cfg_bpf; for (i = 0; i < BPF_MINORS; i++) { cfg_bpf.devno = domakedev(major, i); if (sysconfig(SYS_CFGKMOD, (void *)&cfg_km, sizeof(cfg_km)) == -1) { snprintf(errbuf, PCAP_ERRBUF_SIZE, "bpf_load: could not configure driver: %s", strerror(errno)); return (-1); } } bpfloadedflag = 1; return (0); }
size_t bu_avail_cpus(void) { int ncpu = -1; #ifdef PARALLEL # if defined(__sp3__) if (ncpu < 0) { int status; int cmd; int parmlen; struct var p; cmd = SYS_GETPARMS; parmlen = sizeof(struct var); if (sysconfig(cmd, &p, parmlen) != 0) { bu_bomb("bu_parallel(): sysconfig error for sp3"); } ncpu = p.v_ncpus; } # endif /* __sp3__ */ # ifdef __FreeBSD__ if (ncpu < 0) { int maxproc; size_t len; len = 4; if (sysctlbyname("hw.ncpu", &maxproc, &len, NULL, 0) == -1) { perror("sysctlbyname"); } else { ncpu = maxproc; } } # endif # if defined(__APPLE__) if (ncpu < 0) { size_t len; int maxproc; int mib[] = {CTL_HW, HW_AVAILCPU}; len = sizeof(maxproc); if (sysctl(mib, 2, &maxproc, &len, NULL, 0) == -1) { perror("sysctl"); } else { ncpu = maxproc; /* should be able to get sysctl to return maxproc */ } } # endif /* __ppc__ */ # if defined(HAVE_GET_NPROCS) if (ncpu < 0) { ncpu = get_nprocs(); /* GNU extension from sys/sysinfo.h */ } # endif /* * multithreading support for SunOS 5.X / Solaris 2.x */ # if defined(_SC_NPROCESSORS_ONLN) /* SUNOS and linux (and now Mac 10.6+) */ if (ncpu < 0) { ncpu = sysconf(_SC_NPROCESSORS_ONLN); if (ncpu < 0) { perror("Unable to get the number of available CPUs"); } } #endif #if defined(_SC_NPROC_ONLN) if (ncpu < 0) { ncpu = sysconf(_SC_NPROC_ONLN); if (ncpu < 0) { perror("Unable to get the number of available CPUs"); } } #endif # if defined(linux) if (ncpu < 0) { /* old linux method */ /* * Ultra-kludgey way to determine the number of cpus in a * linux box--count the number of processor entries in * /proc/cpuinfo! */ # define CPUINFO_FILE "/proc/cpuinfo" FILE *fp; char buf[128]; fp = fopen (CPUINFO_FILE, "r"); if (fp == NULL) { perror (CPUINFO_FILE); } else { ncpu = 0; while (bu_fgets(buf, 80, fp) != NULL) { if (bu_strncmp (buf, "processor", 9) == 0) { ncpu++; } } fclose (fp); } } # endif # if defined(_WIN32) /* Windows */ if (ncpu < 0) { SYSTEM_INFO sysinfo; GetSystemInfo(&sysinfo); ncpu = (int)sysinfo.dwNumberOfProcessors; } # endif #endif /* PARALLEL */ if (UNLIKELY(bu_debug & BU_DEBUG_PARALLEL)) { /* do not use bu_log() here, this can get called before semaphores are initialized */ fprintf(stderr, "bu_avail_cpus: counted %d cpus.\n", ncpu); } if (LIKELY(ncpu > 0)) { return ncpu; } /* non-PARALLEL */ return 1; }