int setup_linklocal_addr(int sock, struct Interface *iface) { struct ifconf ifconf; unsigned int nlen; uint8_t *p, *end; int index = 0; /* just allocate 8192 bytes, should be more than enough.. */ if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len = (32 << 8)))) { flog(LOG_CRIT, "malloc failed: %s", strerror(errno)); goto ret; } if (ioctl(sock, SIOCGIFCONF, &ifconf) < 0) { flog(LOG_ERR, "ioctl(SIOCGIFCONF) failed: %s(%d)", strerror(errno), errno); goto ret; } p = (uint8_t *)ifconf.ifc_buf; end = p + ifconf.ifc_len; nlen = strlen(iface->Name); while(p < end) { p += IFNAMSIZ; if ((p + 2) >= end) break; if ((p + *p) >= end) break; if ((*(p + 1) == AF_LINK) && (((struct sockaddr_dl *)p)->sdl_nlen == nlen) && (!memcmp(iface->Name, ((struct sockaddr_dl *)p)->sdl_data, nlen))) { index = ((struct sockaddr_dl *)p)->sdl_index; } if (index && (*(p + 1) == AF_INET6)) if (!memcmp(&((struct sockaddr_in6 *)p)->sin6_addr, ll_prefix, sizeof(ll_prefix))) { memcpy(&iface->if_addr, &((struct sockaddr_in6 *)p)->sin6_addr, sizeof(struct in6_addr)); iface->if_index = index; free(ifconf.ifc_buf); return 0; } p += *p; } ret: flog(LOG_ERR, "no linklocal address configured for %s", iface->Name); free(ifconf.ifc_buf); return -1; }
int netlink_socket(void) { int rc, sock; unsigned int val = 1; struct sockaddr_nl snl; sock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); if (sock == -1) { flog(LOG_ERR, "Unable to open netlink socket: %s", strerror(errno)); } else if (setsockopt(sock, SOL_NETLINK, NETLINK_NO_ENOBUFS, &val, sizeof(val)) < 0 ) { flog(LOG_ERR, "Unable to setsockopt NETLINK_NO_ENOBUFS: %s", strerror(errno)); } memset(&snl, 0, sizeof(snl)); snl.nl_family = AF_NETLINK; snl.nl_groups = RTMGRP_LINK; rc = bind(sock, (struct sockaddr*)&snl, sizeof(snl)); if (rc == -1) { flog(LOG_ERR, "Unable to bind netlink socket: %s", strerror(errno)); close(sock); sock = -1; } return sock; }
/***************************************************************************** * storeListEntry * * Inputs: * in6_addr *Target - this is the newly seen target to check * * Outputs: * lRoot has a new item added if the address was new. * * Return: * Void */ void storeListEntry(struct in6_addr *newEntry) { struct in6_addr *ptr; // Take a permanenet copy of the target ptr = (struct in6_addr *)malloc(sizeof(struct in6_addr) ); if (!ptr) { flog(LOG_ERR, "Malloc failed. Ignoring."); return; } memcpy(ptr, newEntry, sizeof(struct in6_addr) ); if ( tfind( (void *)ptr, &lRoot, tCompare) == NULL ) { // New entry flog(LOG_DEBUG2, "New list entry"); if ( tsearch( (void *)ptr, &lRoot, tCompare) == NULL) { flog(LOG_ERR, "tsearch failed. Cannot record entry."); return; } } else { flog(LOG_ERR, "Dupe list entry. Ignoring."); } }
/***************************************************************************** * getLinkaddress * Get the link-level address (i.e. MAC address) for an interface. * * Inputs: * char * iface * String containg the interface name (e.g. "eth1") * * Outputs: * unsigned char * link * String of the form "00:12:34:56:78:90" * * Return: * 0 is successful, * 1 if error. */ int getLinkaddress( char * iface, unsigned char * link) { int sockfd, io; struct ifreq ifr; strncpy( ifr.ifr_name, iface, INTERFACE_STRLEN ); sockfd = socket(AF_INET, SOCK_STREAM, 0); if(sockfd < 0) { flog(LOG_ERR, "failed to open a test SOCK_STREAM."); return 1; } io = ioctl(sockfd, SIOCGIFHWADDR, (char *)&ifr); if(io < 0) { close(sockfd); flog(LOG_ERR, "octl failed to obtain link address"); return 1; } memcpy(link, (unsigned char *)ifr.ifr_ifru.ifru_hwaddr.sa_data, 6); flog(LOG_ERR, "getLinkaddress :%x:%x:%x:%x:%x:%x",link[0], link[1],link[2],link[3],link[4],link[5]); close(sockfd); return 0; }
/* try to register inotify watches, unregistering them if not compeltely successful hold an open fd during the attempt, to prevent unmount during the process */ static int try_reg_watches(const char *qpath, const char *rqpath) { int mpfd, ret = 0; struct stat st; /* unregister existing inotify watches */ unreg_watches(); /* try to quickly open a fd (expect read access on qpath) */ if ((mpfd = open(qpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC)) != -1) { if (lstat(qpath, &st) == -1 || !S_ISDIR(st.st_mode)) flog(LOG_NOTICE, "%s is not a directory, waiting...", qpath); else if (lstat(rqpath, &st) == -1 || !S_ISDIR(st.st_mode)) flog(LOG_NOTICE, "%s is not a directory, waiting...", rqpath); /* if registering inotify watches is unsuccessful, immediately unregister */ else if (reg_watches(qpath, rqpath)) ret = 1; else unreg_watches(); /* free the pin fd */ if (close(mpfd)) warning("could not close pin directory"); } else flog(LOG_NOTICE, "failed to pin %s, waiting...", qpath); return ret; }
void xainc(wchar_t *file, char *an, off_t inc) { char buf[32]; ssize_t al; off_t val; char *fn; if(file[0] != L'/') return; if((fn = icswcstombs(file, NULL, NULL)) == NULL) { flog(LOG_WARNING, "could not convert filename %ls into local charset: %s", file, strerror(errno)); return; } if((al = getxattr(fn, an, buf, sizeof(buf) - 1)) < 0) { if(errno != ENOATTR) { flog(LOG_WARNING, "could not get xattr %s on %s: %s", an, fn, strerror(errno)); return; } val = 0; } else { buf[al] = 0; val = strtoll(buf, NULL, 10); } val += inc; al = snprintf(buf, sizeof(buf), "%ji", (intmax_t)val); if(setxattr(fn, an, buf, al, 0) < 0) flog(LOG_WARNING, "could not set xattr %s on %s: %s", an, fn, strerror(errno)); }
static void check_pid_file(char const * daemon_pid_file_ident) { FILE * pidfile = fopen(daemon_pid_file_ident, "r"); if (!pidfile) { flog(LOG_ERR, "unable to open pid file, %s: %s", daemon_pid_file_ident, strerror(errno)); exit(-1); } pid_t pid = -1; int rc = fscanf(pidfile, "%d", &pid); fclose(pidfile); if (rc != 1) { flog(LOG_ERR, "unable to read pid from pid file: %s", daemon_pid_file_ident); exit(-1); } if (pid != getpid()) { flog(LOG_ERR, "pid in file, %s, doesn't match getpid(): %d != %d", daemon_pid_file_ident, pid, getpid()); exit(-1); } dlog(LOG_DEBUG, 4, "validated pid file, %s: %d", daemon_pid_file_ident, pid); }
/***************************************************************************** * prefixset * Take prefix in the form 1111:2222:3333:4444: and pad it to the * full length * * Inputs: * char * px * String representation, fully padded. * * Outputs: * As input * * Return: * -1 on error, else bit length of unpadded prefix. * * Note: * IMPORTANT! px *must* be big enough to hold full ipv6 string */ int prefixset(char px[]) { size_t len; int missing, c1, c2; // First we must ensure fully padded with leading 0s for(c1=0; c1<INET6_ADDRSTRLEN; c1+=5 ) { len = strlen(px); for(c2 = 0; c2 < 4; c2++) { if (px[c1+c2] != ':') continue; else break; } missing = abs(c2-4); if (missing != 0) { char suffix[INET6_ADDRSTRLEN]; strcpy( suffix, &px[c1]); // Grab the tail memset(&px[c1], '0', missing); // pad it with missing zeros px[c1+missing] = '\0'; // Reterminate strcat(px, suffix); // Add the tail back } } len = strlen(px); switch (len) { case 5: strcat(px, "0000:0000:0000:0000:0000:0000:0000"); return 16; case 10: strcat(px, "0000:0000:0000:0000:0000:0000"); return 32; case 15: strcat(px, "0000:0000:0000:0000:0000"); return 48; case 20: strcat(px, "0000:0000:0000:0000"); return 64; case 25: strcat(px, "0000:0000:0000"); return 80; case 30: strcat(px, "0000:0000"); return 96; case 35: strcat(px, "0000"); return 112; case 39: flog(LOG_ERR, "Full 128-bits defined as the configured prefix. Sure???"); return 128; default: flog(LOG_ERR, "configured prefix not correctly formatted (len = %d)", len); return -1; } }
/** * Turns this program into a daemon. In doing so, we fork() and kill the * parent process. Note too that stdout, stdin, and stderr are closed in * daemon mode, and a file descriptor for a log file is opened. * @param logp Path of the log file, NULL if no log file was specified * @param logf Syslog facility, 0 if no facility was specified * @param daemon False to turn off daemon mode (no fork, leave FDs open) * @param logv Log verbosity, 2 is the default, 0 = no logging, 5 = everything * @return 0 on success, > 0 on error */ int cgre_start_daemon(const char *logp, const int logf, const unsigned char daemon, const int logv) { /* PID returned from the fork() */ pid_t pid; /* Fork and die. */ if (daemon) { pid = fork(); if (pid < 0) { openlog("CGRE", LOG_CONS, LOG_DAEMON|LOG_WARNING); syslog(LOG_DAEMON|LOG_WARNING, "Failed to fork," " error: %s", strerror(errno)); closelog(); fprintf(stderr, "Failed to fork(), %s\n", strerror(errno)); return 1; } else if (pid > 0) { exit(EXIT_SUCCESS); } /* Change the file mode mask. */ umask(0); } else { cgroup_dbg("Not using daemon mode.\n"); pid = getpid(); } cgre_start_log(logp, logf, logv); if (!daemon) { /* We can skip the rest, since we're not becoming a daemon. */ flog(LOG_INFO, "Proceeding with PID %d", getpid()); return 0; } else { /* Get a new SID for the child. */ if (setsid() < 0) { flog(LOG_ERR, "Failed to get a new SID, error: %s", strerror(errno)); return 2; } /* Change to the root directory. */ if (chdir("/") < 0) { flog(LOG_ERR, "Failed to chdir to /, error: %s", strerror(errno)); return 3; } /* Close standard file descriptors. */ close(STDIN_FILENO); if (logfile != stdout) close(STDOUT_FILENO); close(STDERR_FILENO); } /* If we make it this far, we're a real daemon! Or we chose not to. */ flog(LOG_INFO, "Proceeding with PID %d", getpid()); return 0; }
int check_ip6_forwarding(void) { int forw_sysctl[] = { SYSCTL_IP6_FORWARDING }; int value; size_t size = sizeof(value); FILE *fp = NULL; #ifdef __linux__ fp = fopen(PROC_SYS_IP6_FORWARDING, "r"); if (fp) { fscanf(fp, "%d", &value); fclose(fp); } else flog(LOG_DEBUG, "Correct IPv6 forwarding procfs entry not found, " "perhaps the procfs is disabled, " "or the kernel interface has changed?"); #endif /* __linux__ */ if (!fp && sysctl(forw_sysctl, sizeof(forw_sysctl)/sizeof(forw_sysctl[0]), &value, &size, NULL, 0) < 0) { flog(LOG_DEBUG, "Correct IPv6 forwarding sysctl branch not found, " "perhaps the kernel interface has changed?"); return(0); /* this is of advisory value only */ } if (value != 1) { flog(LOG_DEBUG, "IPv6 forwarding setting is: %u, should be 1", value); return(-1); } return(0); }
static int cgre_store_unchanged_process(pid_t pid, int flags) { int i; for (i = 0; i < array_unch.index; i++) { if (array_unch.proc[i].pid != pid) continue; /* pid is stored already. */ return 0; } if (array_unch.index >= array_unch.num_allocation) { int alloc = array_unch.num_allocation + NUM_PER_REALLOCATIOM; void *new_array = realloc(array_unch.proc, sizeof(unchanged_pid_t) * alloc); if (!new_array) { flog(LOG_WARNING, "Failed to allocate memory"); return 1; } array_unch.proc = new_array; array_unch.num_allocation = alloc; } array_unch.proc[array_unch.index].pid = pid; array_unch.proc[array_unch.index].flags = flags; array_unch.index++; flog(LOG_DEBUG, "Store the unchanged process (PID: %d, FLAGS: %d)", pid, flags); return 0; }
int check_device(struct Interface *iface) { struct ifreq ifr; strncpy(ifr.ifr_name, iface->Name, IFNAMSIZ - 1); ifr.ifr_name[IFNAMSIZ - 1] = '\0'; if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { if (!iface->IgnoreIfMissing) flog(LOG_ERR, "ioctl(SIOCGIFFLAGS) failed for %s: %s", iface->Name, strerror(errno)); return -1; } if (!(ifr.ifr_flags & IFF_UP)) { if (!iface->IgnoreIfMissing) flog(LOG_ERR, "interface %s is not UP", iface->Name); return -1; } if (!(ifr.ifr_flags & IFF_RUNNING)) { if (!iface->IgnoreIfMissing) flog(LOG_ERR, "interface %s is not RUNNING", iface->Name); return -1; } if (!iface->UnicastOnly && !(ifr.ifr_flags & IFF_MULTICAST)) { flog(LOG_INFO, "interface %s does not support multicast, forcing UnicastOnly", iface->Name); iface->UnicastOnly = 1; } return 0; }
int get_v4addr(const char *ifn, unsigned int *dst) { struct ifreq ifr; struct sockaddr_in *addr; int fd; if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { flog(LOG_ERR, "create socket for IPv4 ioctl failed for %s: %s", ifn, strerror(errno)); return -1; } memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, ifn, IFNAMSIZ - 1); ifr.ifr_name[IFNAMSIZ - 1] = '\0'; ifr.ifr_addr.sa_family = AF_INET; if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) { flog(LOG_ERR, "ioctl(SIOCGIFADDR) failed for %s: %s", ifn, strerror(errno)); close(fd); return -1; } addr = (struct sockaddr_in *)(&ifr.ifr_addr); dlog(LOG_DEBUG, 3, "IPv4 address for %s is %s", ifn, inet_ntoa(addr->sin_addr)); *dst = addr->sin_addr.s_addr; close(fd); return 0; }
int distribute_file(char* name, char* contents, int numBlocks, int send_sockfd, int recv_sockfd, int port) { flog("Going to distribute %s with %d blocks", name, numBlocks); if(numBlocks > 0) { int i; struct block curBlock; memset(&curBlock, 0, sizeof(curBlock)); strncpy(curBlock.filename, name, sizeof(name)); curBlock.total_blocks = numBlocks; for(i = 0; i < numBlocks; i++) { strncpy(curBlock.payload, contents+i*BLOCKSIZE, BLOCKSIZE); curBlock.paysize = strlen(curBlock.payload); //convert the block to network order struct block net_blk; block_local_to_network(&curBlock, &net_blk); //send the block to 2 hosts hostRecord* firstHost = sendBlockToAHost(NULL, net_blk, send_sockfd, recv_sockfd, port); hostRecord* secondHost = sendBlockToAHost(firstHost, net_blk, send_sockfd, recv_sockfd, port); flog("Set block %d to hosts %s and %s", i, firstHost->hostAddr, secondHost->hostAddr); } } flog("Done distributing %s", name); return TRUE; }
void process_netlink_msg(int sock) { int len; char buf[4096]; struct iovec iov = { buf, sizeof(buf) }; struct sockaddr_nl sa; struct msghdr msg = { (void *)&sa, sizeof(sa), &iov, 1, NULL, 0, 0 }; struct nlmsghdr *nh; struct ifinfomsg * ifinfo; char ifname[IF_NAMESIZE] = {""}; len = recvmsg (sock, &msg, 0); if (len == -1) { flog(LOG_ERR, "recvmsg failed: %s", strerror(errno)); } for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len); nh = NLMSG_NEXT (nh, len)) { /* The end of multipart message. */ if (nh->nlmsg_type == NLMSG_DONE) return; if (nh->nlmsg_type == NLMSG_ERROR) { flog(LOG_ERR, "%s:%d Some type of netlink error.\n", __FILE__, __LINE__); abort(); } /* if (nh->nlmsg_type == RTM_NEWPREFIX) { ifinfo = NLMSG_DATA(nh); if_indextoname(ifinfo->ifi_index, ifname); dlog(LOG_DEBUG, 3, "%s receive new prefix.\n", ifname); char tether_interface[PROPERTY_VALUE_MAX] = {'\0'}; if (!property_get("net.ipv6.tether", tether_interface, NULL)) { flog(LOG_ERR, "netlink get tether interface failed!"); abort(); } if(!strncmp(tether_interface, ifname, strlen(ifname))){ dlog(LOG_DEBUG, 2, "reload config for new prefix.\n"); reload_config(); } return ; } */ /* Continue with parsing payload. */ ifinfo = NLMSG_DATA(nh); if_indextoname(ifinfo->ifi_index, ifname); if (ifinfo->ifi_flags & IFF_RUNNING) { dlog(LOG_DEBUG, 3, "%s, ifindex %d, flags is running", ifname, ifinfo->ifi_index); } else { dlog(LOG_DEBUG, 3, "%s, ifindex %d, flags is *NOT* running", ifname, ifinfo->ifi_index); set_last_prefix(ifname); } reload_config(); } }
/* run loop for given queue type and msgid[.del]; msgid is a volatile string */ static void run_loop(const char *qtype, const char *msgid, const char *looppath) { const char *args[] = { looppath, qtype, msgid, NULL }; if (run_process(MAX_PROC, WAIT_PROC, args)) flog(LOG_INFO, "processing: %s %s", qtype, msgid); else flog(LOG_WARNING, "failed to launch: %s %s", qtype, msgid); }
void _ClearPIDMap( DShowCaptureInfo *pCapInfo ) { HRESULT hr; CComPtr <IPin> pPin; if ( pCapInfo->pBDADemux == NULL ) return; if ( SUCCEEDED(hr = graphTools.FindPin( pCapInfo->pBDADemux, L"1", &pPin.p, REQUESTED_PINDIR_OUTPUT )) ) { IMPEG2PIDMap *pIPidMap = NULL; hr = pPin->QueryInterface(IID_IMPEG2PIDMap, (void**)&pIPidMap); if (SUCCEEDED(hr)) { ULONG i,pid_total = 0; ULONG* unmap_pids=NULL; IEnumPIDMap *pIEnumPIDMap; if (SUCCEEDED(pIPidMap->EnumPIDMap(&pIEnumPIDMap))) { ULONG num; PID_MAP pPidMap; //count pid; while( pIEnumPIDMap->Next(1, &pPidMap, &num) == S_OK ) pid_total++; unmap_pids = new ULONG[pid_total+1]; //get pids i = 0; pIEnumPIDMap->Reset(); while(pIEnumPIDMap->Next(1, &pPidMap, &num) == S_OK) { unmap_pids[i++] = pPidMap.ulPID; } SAFE_RELEASE( pIEnumPIDMap ); //unmap pid if ( FAILED( hr = pIPidMap->UnmapPID( pid_total, unmap_pids ) ) ) { //flog( ("native.log", "Failed to UnmapPID on TIF pin hr=0x%x \r\n", hr ) ); } else { flog( ("native.log", "UnmapPID TIF Pin total:%d \r\n", pid_total ) ); } delete unmap_pids; } else flog( ("native.log", "Failed to get EnumPIDMap on TIF Pin of Dumexlexer hr=0x%x \r\n", hr ) ); SAFE_RELEASE( pIPidMap ); } else { flog( ("native.log", "Failed to get IMPEG2PIDMap on Pin-1 to map pid hr=0x%x \r\n", hr ) ); } } else { flog( ("native.log", "Failed to get Pin-1 to map pid hr=0x%x \r\n", hr ) ); } }
static void process_rs(struct Interface *iface, unsigned char *msg, int len, struct sockaddr_in6 *addr) { double delay; double next; struct timespec ts; uint8_t *opt_str; /* validation */ len -= sizeof(struct nd_router_solicit); opt_str = (uint8_t *) (msg + sizeof(struct nd_router_solicit)); while (len > 0) { int optlen; if (len < 2) { flog(LOG_WARNING, "trailing garbage in RS"); return; } optlen = (opt_str[1] << 3); if (optlen == 0) { flog(LOG_WARNING, "zero length option in RS"); return; } else if (optlen > len) { flog(LOG_WARNING, "option length greater than total length in RS"); return; } if (*opt_str == ND_OPT_SOURCE_LINKADDR && IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr)) { flog(LOG_WARNING, "received icmpv6 RS packet with unspecified source address and there is a lladdr option"); return; } len -= optlen; opt_str += optlen; } clock_gettime(CLOCK_MONOTONIC, &ts); delay = MAX_RA_DELAY_TIME * rand() / (RAND_MAX + 1.0); if (iface->UnicastOnly) { send_ra_forall(iface, &addr->sin6_addr); } else if (timespecdiff(&ts, &iface->last_multicast) / 1000.0 < iface->MinDelayBetweenRAs) { /* last RA was sent only a few moments ago, don't send another immediately. */ next = iface->MinDelayBetweenRAs - (ts.tv_sec + ts.tv_nsec / 1000000000.0) + (iface->last_multicast.tv_sec + iface->last_multicast.tv_nsec / 1000000000.0) + delay / 1000.0; iface->next_multicast = next_timespec(next); } else { /* no RA sent in a while, send a multicast reply */ send_ra_forall(iface, NULL); next = rand_between(iface->MinRtrAdvInterval, iface->MaxRtrAdvInterval); iface->next_multicast = next_timespec(next); } }
HRESULT tuneATSCQAMChannel( DShowCaptureInfo* pCapInfo, long lPhysicalChannel, unsigned long lFrequency, long lModulation ) { HRESULT hr = S_OK; if ( pCapInfo->pDebugSrcSink != NULL ) //when debugsrc is on, always bypass tune. return 1; //its's a virtual tuner, skip; if (capMask(pCapInfo->captureConfig, sage_DShowCaptureDevice_BDA_VIRTUAL_TUNER_MASK )) return 1; lFrequency /= 1000; // create tune request CComPtr <ITuneRequest> pTuneRequest; if (FAILED(hr = graphTools.CreateQAMTuneRequest( pCapInfo->pTuningSpace, pTuneRequest, lPhysicalChannel, lFrequency, lModulation ))) { flog( ("native.log", "Failed to create a QAM Tune Request for channe %d frq:%d mod:%d hr=0x%x.\n", lPhysicalChannel, lFrequency, lModulation, hr ) ); return -1; } try { if (FAILED(hr = pCapInfo->pTuner->Validate(pTuneRequest))) { flog( ("native.log", "Validate QAM tune request for channel %d, frq:%d mod:%d failed hr=0x%x.\n", lPhysicalChannel, lFrequency, lModulation, hr ) ); } if (FAILED(hr = pCapInfo->pTuner->put_TuneRequest(pTuneRequest))) { flog( ("native.log", "Cannot submit QAM tune request for channel %d, frq:%d mod:%d hr=0x%x.\n", lPhysicalChannel, lFrequency, lModulation, hr ) ); return -1; } else { //IBDA_DeviceControl* BDA_DeviceControl; //hr = pCapInfo->pBDATuner->QueryInterface( IID_IBDA_DeviceControl, (void **) &BDA_DeviceControl); //if ( hr == S_OK ) //{ // hr = BDA_DeviceControl->CheckChanges(); // if ( hr == S_OK ) // hr = BDA_DeviceControl->CommitChanges(); //} flog( ("native.log", "Changed QAM Channel %d frq:%d mod:%d \n", lPhysicalChannel, lFrequency, lModulation ) ); pCapInfo->dwTuneState = 1; } } catch (...) { flog( ("native.log", "Exception for changing QAM Channel %d, hr=0x%x\n", lPhysicalChannel, hr ) ); return -1; } return 1; }
/* get one block out of storage */ static void storage_get(int cblock, char *what) { flog("storage_get(%d,...)",cblock); if (!storage_used[cblock]) { flog("attempt to get unused cblock %d\n",cblock); } int offset = cblock*BLOCKSIZE; int i; for (i=0; i<BLOCKSIZE; i++) { what[i]=storage[offset++]; } }
/** * Start logging. Opens syslog and/or log file and sets log level. * @param logp Path of the log file, NULL if no log file was specified * @param logf Syslog facility, NULL if no facility was specified * @param logv Log verbosity, 2 is the default, 0 = no logging, 4 = everything */ static void cgre_start_log(const char *logp, int logf, int logv) { /* Current system time */ time_t tm; /* Log levels */ int loglevels[] = { LOG_EMERG, /* -qq */ LOG_ERR, /* -q */ LOG_NOTICE, /* default */ LOG_INFO, /* -v */ LOG_DEBUG /* -vv */ }; /* Set default logging destination if nothing was specified */ if (!logp && !logf) logf = LOG_DAEMON; /* Open log file */ if (logp) { if (strcmp("-", logp) == 0) { logfile = stdout; } else { logfile = fopen(logp, "a"); if (!logfile) { fprintf(stderr, "Failed to open log file %s," " error: %s. Continuing anyway.\n", logp, strerror(errno)); logfile = stdout; } } } else logfile = NULL; /* Open syslog */ if (logf) { openlog("CGRE", LOG_CONS | LOG_PID, logf); logfacility = logf; } else logfacility = 0; /* Set the log level */ if (logv < 0) logv = 0; if (logv >= sizeof(loglevels)/sizeof(int)) logv = sizeof(loglevels)/sizeof(int)-1; loglevel = loglevels[logv]; flog(LOG_DEBUG, "CGroup Rules Engine Daemon log started"); tm = time(0); flog(LOG_DEBUG, "Current time: %s", ctime(&tm)); flog(LOG_DEBUG, "Opened log file: %s, log facility: %d, log level: %d", logp, logfacility, loglevel); }
int lumberjack_connect(struct lumberjack *lumberjack) { /* TODO(sissel): support ipv6, if anyone ever uses that in production ;) */ insist(lumberjack != NULL, "lumberjack must not be NULL"); int rc; rc = lumberjack_tcp_connect(lumberjack); if (rc < 0) { return -1; } rc = lumberjack_ssl_handshake(lumberjack); if (rc < 0) { flog(stdout, "ssl handshake failed"); lumberjack_disconnect(lumberjack); return -1; } /* If we get here, tcp connect + ssl handshake has succeeded */ lumberjack->connected = 1; /* Always truncate the output buffer on a new connection. * This prevents accidental buffer leaks from an old/dead connection * into this new one. * * The symptom of such a leak is that, upon a new connection, * the oldest non-ack'd event is not the first event seen by the receiver. */ str_truncate(lumberjack->io_buffer); /* Send our window size */ rc = lumberjack_write_window_size(lumberjack); if (rc < 0) { flog(stdout, "lumberjack_write_window_size failed: %d", rc); lumberjack_disconnect(lumberjack); return -1; } /* Retransmit anything currently in the ring (unacknowledged data frames) * This is a no-op if there's nothing in the ring. */ rc = lumberjack_retransmit_all(lumberjack); if (rc < 0) { flog(stdout, "lumberjack_retransmit_all failed"); /* Retransmit failed, which means a write failed during retransmit, * disconnect and claim a connection failure. */ lumberjack_disconnect(lumberjack); return -1; } insist(lumberjack->fd > 0, "lumberjack->fd must be > 0 after a connect, was %d", lumberjack->fd); insist(lumberjack->ssl != NULL, "lumberjack->ssl must not be NULL after a connect"); return 0; } /* lumberjack_connect */
// render the volumes void minilod::render(float ex,float ey,float ez, float farp, float fovy,float aspect, float t, int phase) { int i,j; minibrickdata *brk; miniloddata *vol; minibrick *brick; float dist; int lod; // configure bricks for (i=0; i<BNUM; i++) for (j=0; j<BRICKS[i].lods; j++) BRICKS[i].brick[j].configure_stripeoffset(CONFIGURE_BRICKOFFSET); // render volumes for (i=MINIBRICK_FIRST_RENDER_PHASE; i<=MINIBRICK_LAST_RENDER_PHASE; i++) if (phase==MINIBRICK_ONE_RENDER_PHASE || i==phase) for (j=0; j<VNUM; j++) { // get actual volume vol=&VOLS[j]; // get indexed brick brk=&BRICKS[vol->index]; // calculate distance dist=fsqr((vol->x+OFFSETLON)*SCALEX-ex)+fsqr((vol->y+OFFSETLAT)*SCALEY-ez)+fsqr(vol->e*SCALEELEV-ey); dist/=fsqr(brk->brad); // calculate lod if (dist<=1.0f) lod=0; else lod=ftrc(flog(dist)/flog(brk->stagger)/2.0f+0.5f); if (lod>=brk->lods) lod=brk->lods-1; // get actual brick lod brick=&brk->brick[lod]; // set position brick->resetpos(vol->x,vol->y,vol->e,vol->dx,vol->dy,vol->de); // set color brick->addiso(0.5f,vol->r,vol->g,vol->b,vol->a); // render mesh brick->render(ex,ey,ez,0.0f,farp,fovy,aspect,0.0f,i); } }
HRESULT tuneATSCChannel( DShowCaptureInfo* pCapInfo, ATSC_FREQ* atsc, int bDryRun ) { HRESULT hr = S_OK; long lPhysicalChannel; long lFrequency; if ( pCapInfo->pDebugSrcSink != NULL ) //when debugsrc is on, always bypass tune. return 1; //its's a virtual tuner, skip; if (capMask(pCapInfo->captureConfig, sage_DShowCaptureDevice_BDA_VIRTUAL_TUNER_MASK )) { pCapInfo->dwTuneState = 1; return 1; } if ( bDryRun ) return 1; lPhysicalChannel = atsc->physical_ch; lFrequency = atsc->frequency/1000; // create tune request CComPtr <ITuneRequest> pTuneRequest; if (FAILED(hr = graphTools.CreateATSCTuneRequest( pCapInfo->pTuningSpace, pTuneRequest, -1, -1, lPhysicalChannel, lFrequency ))) { flog( ("native.log", "Failed to create a ATSC Tune Request for channe %d, hr=0x%x.\n", lPhysicalChannel, hr ) ); return -1; } try { if (FAILED(hr = pCapInfo->pTuner->put_TuneRequest(pTuneRequest))) { flog( ("native.log", "Cannot submit ATSC tune request for channel %d, hr=0x%x.\n", lPhysicalChannel, hr ) ); return -1; } else { flog( ("native.log", "Changed ATSC Channel %d (%d)\n", lPhysicalChannel, lFrequency ) ); pCapInfo->dwTuneState = 1; } } catch (...) { flog( ("native.log", "Exception for changing ATSC Channel %d, hr=0x%x\n", lPhysicalChannel, hr ) ); return -1; } return 1; }
hostRecord* chooseRandomHost() { int randNum; hostRecord* curChoice; flog("Starting chooseRandomHost()\n"); do { if(getNumValidHosts() <= 1) { flog("1 or fewer hosts available\n"); } randNum = rand() % MAX_STORED_HOSTS; } while((curChoice = getHostRecordAt(randNum)) == NULL); flog("Done with chooseRandomHost(). Return %s\n", curChoice->hostAddr); return curChoice; }
int tuneDVBTFrequency( DShowCaptureInfo* pCapInfo, DVB_T_FREQ* dvbt, int bDryRun ) { HRESULT hr = S_OK; long freq, band; freq = dvbt->frequency/1000; //BDA driver in KHz band = dvbt->bandwidth; if ( pCapInfo->pDebugSrcSink != NULL ) //when debugsrc is on, always bypass tune. return 1; //its's a virtual tuner, skip; if (capMask(pCapInfo->captureConfig, sage_DShowCaptureDevice_BDA_VIRTUAL_TUNER_MASK )) { pCapInfo->dwTuneState = 1; return 1; } if ( bDryRun ) return 1; // create tune request CComPtr <ITuneRequest> pTuneRequest; if (FAILED(hr = graphTools.CreateDVBTTuneRequest( pCapInfo->pTuningSpace, pTuneRequest, freq, band ))) { flog( ("native.log", "Failed to create a DVB-T Tune Request for freq %d band :%d, hr=0x%x.\n", freq, band, hr ) ); return -1; } try { SetupTunerFreq( pCapInfo, freq*1000, band, -1 ); if (FAILED(hr = pCapInfo->pTuner->put_TuneRequest(pTuneRequest))) { flog( ("native.log", "Cannot submit DVB-T tune request for freq %d band :%d, hr=0x%x.\n", freq, band, hr ) ); return -1; } else { flog( ("native.log", "Sucessful Changing DVB-T freq %d band :%d \n", freq, band ) ); pCapInfo->dwTuneState = 1; } } catch (...) { flog( ("native.log", "Exception for changing DVB-T freq %d band :%d, hr=0x%x\n", freq, band, hr ) ); return -1; } return 1; }
/* Fork to create privileged process connected by a pipe */ int privsep_init(void) { int pipefds[2]; pid_t pid; if (privsep_enabled()) return 0; if (pipe(pipefds) != 0) { flog(LOG_ERR, "Couldn't create privsep pipe."); return (-1); } pid = fork(); if (pid == -1) { flog(LOG_ERR, "Couldn't fork for privsep."); return (-1); } if (pid == 0) { int nullfd; /* This will be the privileged child */ close(pipefds[1]); pfd = pipefds[0]; /* Detach from stdio */ nullfd = open("/dev/null", O_RDONLY); if (nullfd < 0) { perror("/dev/null"); close(pfd); _exit(1); } dup2(nullfd, 0); dup2(nullfd, 1); /* XXX: we'll keep stderr open in debug mode for better logging */ if (get_debuglevel() == 0) dup2(nullfd, 2); privsep_read_loop(); close(pfd); _exit(0); } /* Continue execution (will drop privileges soon) */ close(pipefds[0]); pfd = pipefds[1]; return 0; }
/* forget all records for a file */ int forget_file(const char *name) { Content *f; if (strlen(name)>255) { flog("name %s too long",name); return FALSE; } f = content_get(name); if (f==NULL) { flog("no file '%s' to forget",name); return FALSE; } clist_forget(f); return TRUE; }
static void logret(char *pre, Fsstate *fss, int ret) { switch(ret){ default: flog("%s: code %d", pre, ret); break; case RpcErrstr: flog("%s: error %r", pre); break; case RpcFailure: flog("%s: failure %s", pre, fss->err); break; case RpcNeedkey: flog("%s: needkey %s", pre, fss->keyinfo); break; case RpcOk: flog("%s: ok", pre); break; case RpcToosmall: flog("%s: toosmall %d", pre, fss->rpc.nwant); break; case RpcPhase: flog("%s: phase: %r", pre); break; case RpcConfirm: flog("%s: waiting for confirm", pre); break; } }
static int drop_root_privileges(const char *username) { struct passwd *pw = getpwnam(username); if (pw) { if (initgroups(username, pw->pw_gid) != 0 || setgid(pw->pw_gid) != 0 || setuid(pw->pw_uid) != 0) { flog(LOG_ERR, "Couldn't change to '%.32s' uid=%d gid=%d", username, pw->pw_uid, pw->pw_gid); return -1; } } else { flog(LOG_ERR, "Couldn't find user '%.32s'", username); return -1; } return 0; }