CmsRet oal_Net_getGMACPortIfNameList(char **GMACPortIfNameList) { #ifdef CMS_BRCM_GMAC #ifdef DESKTOP_LINUX *GMACPortIfNameList = cmsMem_alloc(512, 0); strcpy(*GMACPortIfNameList, "eth1,eth3"); #else SINT32 skfd; struct ifreq ifr; if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { cmsLog_error("Error openning socket for getting the GMAC enet port list"); return CMSRET_INTERNAL_ERROR; } /* Get the name -> if_index mapping for ethswctl */ strcpy(ifr.ifr_name, "bcmsw"); if (ioctl(skfd, SIOCGIFINDEX, &ifr) < 0) { close(skfd); cmsLog_debug("bcmsw interface does not exist. Error: %d", errno); return CMSRET_INTERNAL_ERROR; } /* Allocate dynamic memory to hold max interface names (eth0,eth1,..eth10<cr>)*/ if ((*GMACPortIfNameList = cmsMem_alloc(((MAX_GMAC_ETH_PORT * (IFNAMSIZ+1)) + 2), ALLOC_ZEROIZE)) == NULL) { cmsLog_error("Fail to alloc mem in getting the GMAC enet port list"); close(skfd); return CMSRET_RESOURCE_EXCEEDED; } memset((void *) &ifr, sizeof(ifr), 0); ifr.ifr_data = *GMACPortIfNameList; if (ioctl(skfd, SIOCGGMACPORT, &ifr) < 0) { cmsLog_error("ioct error in getting the GMAC enet port list. Error: %d", errno); close(skfd); CMSMEM_FREE_BUF_AND_NULL_PTR(*GMACPortIfNameList); return CMSRET_INTERNAL_ERROR; } close(skfd); cmsLog_debug("GMACPortIfNameList=%s, strlen=%d", *GMACPortIfNameList, strlen(*GMACPortIfNameList)); #endif /* DESKTOP_LINUX */ #endif /* CMS_BRCM_GMAC */ return CMSRET_SUCCESS; }
CmsRet cmsUtl_parsePrefixAddress(const char *prefixAddr, char *address, UINT32 *plen) { CmsRet ret = CMSRET_SUCCESS; char *tmpBuf; char *separator; UINT32 len; if (prefixAddr == NULL || address == NULL || plen == NULL) { return CMSRET_INVALID_ARGUMENTS; } cmsLog_debug("prefixAddr=%s", prefixAddr); *address = '\0'; *plen = 128; len = strlen(prefixAddr); if ((tmpBuf = cmsMem_alloc(len+1, 0)) == NULL) { cmsLog_error("alloc of %d bytes failed", len); ret = CMSRET_INTERNAL_ERROR; } else { sprintf(tmpBuf, "%s", prefixAddr); separator = strchr(tmpBuf, '/'); if (separator != NULL) { /* break the string into two strings */ *separator = 0; separator++; while ((isspace(*separator)) && (*separator != 0)) { /* skip white space after comma */ separator++; } *plen = atoi(separator); cmsLog_debug("plen=%d", *plen); } cmsLog_debug("address=%s", tmpBuf); if (strlen(tmpBuf) < BUFLEN_40 && *plen <= 128) { strcpy(address, tmpBuf); } else { ret = CMSRET_INVALID_ARGUMENTS; } cmsMem_free(tmpBuf); } return ret; } /* End of cmsUtl_parsePrefixAddress() */
/** Write contents of pBuf to flash starting at start_block for total_blocks. * * @return 0 on success, -1 on failure. */ int fake_setSharedBlks(int start_block, int total_blocks, char *pBuf) { int fd, rc; int sts=-1; char path[BUFLEN_1024]; CmsRet ret; cmsLog_debug("setting block %d through %d, buf %p", start_block, start_block+total_blocks, pBuf); if (start_block != 0) { cmsLog_error("cannot handle non-zero start block yet."); return sts; } if (total_blocks > FAKE_NUM_PSP_FLASH_BLOCKS) { cmsLog_error("requested more blocks than PSP flash blocks, not handled."); return sts; } /* form path to the flash file */ if ((ret = cmsUtl_getBaseDir(path, sizeof(path))) != CMSRET_SUCCESS) { cmsLog_error("getBaseDir failed, abort func"); return sts; } else { UINT32 offset; offset = strlen(path); snprintf(&(path[offset]), sizeof(path)-offset, "/%s", FAKE_FLASH_PSP_FILENAME); } cmsLog_debug("opening fake flash file at %s", path); if ((fd = open(path, O_RDWR)) < 0) { cmsLog_error("Could not open %s", path); return sts; } rc = write(fd, pBuf, total_blocks * FAKE_FLASH_BLOCK_SIZE); if (rc != total_blocks * FAKE_FLASH_BLOCK_SIZE) { cmsLog_error("bad write, got %d expected %d", rc, total_blocks * FAKE_FLASH_BLOCK_SIZE); } else { cmsLog_debug("%d blocks (%d bytes) written", total_blocks, total_blocks * FAKE_FLASH_BLOCK_SIZE); sts = 0; } close(fd); return sts; }
UBOOL8 cmsUtl_isSubOptionPresent(const char *fullOptionString, const char *subOption) { const char *startChar, *currChar; UINT32 len=0; UBOOL8 found=FALSE; cmsLog_debug("look for subOption %s in fullOptionString=%s", subOption, fullOptionString); if (fullOptionString == NULL || subOption == NULL) { return FALSE; } startChar = fullOptionString; currChar = startChar; while (!found && *currChar != '\0') { /* get to the end of the current subOption */ while (*currChar != ' ' && *currChar != ',' && *currChar != '\0') { currChar++; len++; } /* compare the current subOption with the subOption that was specified */ if ((len == strlen(subOption)) && (0 == strncmp(subOption, startChar, len))) { found = TRUE; } /* advance to the start of the next subOption */ if (*currChar != '\0') { while (*currChar == ' ' || *currChar == ',') { currChar++; } len = 0; startChar = currChar; } } cmsLog_debug("found=%d", found); return found; }
CmsRet oalMsg_send(SINT32 fd, const CmsMsgHeader *buf) { UINT32 totalLen; SINT32 rc; CmsRet ret=CMSRET_SUCCESS; totalLen = sizeof(CmsMsgHeader) + buf->dataLength; rc = write(fd, buf, totalLen); if (rc < 0) { if (errno == EPIPE) { /* * This could happen when smd tries to write to an app that * has exited. Don't print out a scary error message. * Just return an error code and let upper layer app handle it. */ cmsLog_debug("got EPIPE, dest app is dead"); return CMSRET_DISCONNECTED; } else { cmsLog_error("write failed, errno=%d", errno); ret = CMSRET_INTERNAL_ERROR; } } else if (rc != (SINT32) totalLen) { cmsLog_error("unexpected rc %d, expected %u", rc, totalLen); ret = CMSRET_INTERNAL_ERROR; } return ret; }
CmsRet cmsUtl_prefixMacToAddress(const char *prefix, UINT8 *mac, char *addr) { struct in6_addr in6_addr; SINT32 i; cmsLog_debug("prefix<%s>", prefix); if (inet_pton(AF_INET6, prefix, &in6_addr) <= 0) { cmsLog_error("inet_pton returns error"); return CMSRET_INVALID_ARGUMENTS; } for ( i = 8; i <= 15; i++ ) { if (in6_addr.s6_addr[i] != 0) { cmsLog_error("prefix is not 0 at 64 LSB"); return CMSRET_INVALID_ARGUMENTS; }; }; /* create EUI-64 from MAC-48 */ in6_addr.s6_addr[ 8] = mac[0] ^ 0x02; in6_addr.s6_addr[ 9] = mac[1]; in6_addr.s6_addr[10] = mac[2]; in6_addr.s6_addr[11] = 0xff; in6_addr.s6_addr[12] = 0xfe; in6_addr.s6_addr[13] = mac[3]; in6_addr.s6_addr[14] = mac[4]; in6_addr.s6_addr[15] = mac[5]; if (inet_ntop(AF_INET6, &in6_addr, addr, CMS_IPADDR_LENGTH) == NULL) { cmsLog_error("inet_ntop returns error"); return CMSRET_INTERNAL_ERROR; } cmsLog_debug("addr<%s>", addr); return CMSRET_SUCCESS; }
int prctl_runCommandInShellBlocking(char *command) { SpawnedProcessInfo procInfo = {0, 0, 0, 0}; CollectProcessInfo collectInfo; CmsRet ret; if ( command == 0 ) return 1; cmsLog_debug("executing %s", command); if ((procInfo.pid = runCommandInShell(command)) < 0) { cmsLog_error("Could not execute %s", command); return 1; } /* * Now fill in info for the collect. */ collectInfo.collectMode = COLLECT_PID; /* block until we collect it */ collectInfo.pid = procInfo.pid; collectInfo.timeout = 0; /* not applicable since we are COLLECT_PID */ ret = prctl_collectProcess(&collectInfo, &procInfo); if (ret != CMSRET_SUCCESS) { cmsLog_error("prctl_collect failed, ret=%d", ret); /* mwang_todo: should we signal/kill the process? */ return -1; } else { cmsLog_debug("collected pid %d, sigNum=%d exitcode=%d", procInfo.pid, procInfo.signalNumber, procInfo.exitCode); return (procInfo.signalNumber != 0) ? procInfo.signalNumber : procInfo.exitCode; } }
/** Start the command and allow it to run for a limited amount of time. * * This function was called bcmSystemNoHang. */ int prctl_runCommandInShellWithTimeout(char *command) { SpawnedProcessInfo procInfo = {0, 0, 0, 0}; CollectProcessInfo collectInfo; CmsRet ret; if ( command == 0 ) return 1; cmsLog_debug("executing %s", command); if ((procInfo.pid = runCommandInShell(command)) < 0) { cmsLog_error("Could not execute %s", command); return 1; } /* * Now fill in info for the collect. */ collectInfo.collectMode = COLLECT_PID_TIMEOUT; /* block for up to specified timeout waiting for pid */ collectInfo.pid = procInfo.pid; collectInfo.timeout = 120 * MSECS_IN_SEC; /* orig code did usleep(20) for 20000 times. */ ret = prctl_collectProcess(&collectInfo, &procInfo); if (ret != CMSRET_SUCCESS) { cmsLog_error("prctl_collect failed, ret=%d", ret); /* mwang_todo: should we signal/kill the process? */ return -1; } else { cmsLog_debug("collected pid %d, sigNum=%d exitcode=%d", procInfo.pid, procInfo.signalNumber, procInfo.exitCode); return (procInfo.signalNumber != 0) ? procInfo.signalNumber : procInfo.exitCode; } }
static void initAllocSeq(void) { CmsTimestamp tms; if (allocSeq > 0) { return; } cmsTms_get(&tms); srand(tms.nsec); allocSeq = rand() << 16; cmsLog_debug("allocSeq=%lu\n", allocSeq); return; }
SINT32 fake_flashWrite(const char *filename, UINT8 *buf, UINT32 bufLen) { char basedir[BUFLEN_1024]={0}; SINT32 fd, rc, basedirLen; CmsRet ret; if ((ret = cmsUtl_getBaseDir(basedir, sizeof(basedir))) != CMSRET_SUCCESS) { cmsLog_error("getBasedir failed, ret=%d", ret); return ret; } basedirLen = strlen(basedir); rc = snprintf(&(basedir[basedirLen]), sizeof(basedir) - basedirLen, "/%s", filename); if (rc >= ((SINT32) sizeof(basedir)) - basedirLen) { cmsLog_error("basedir var not long enough to hold entire path"); return CMSRET_RESOURCE_EXCEEDED; } fd = open(basedir, O_RDWR|O_CREAT|O_TRUNC, 0666); if (fd < 0) { cmsLog_error("Could not open %s, errno=%d", basedir, errno); return CMSRET_INTERNAL_ERROR; } rc = write(fd, buf, bufLen); if (rc != (SINT32) bufLen) { cmsLog_error("write failed, expected %u got %d", bufLen, rc); ret = CMSRET_INTERNAL_ERROR; } else { cmsLog_debug("wrote buf len=%u to %s", bufLen, basedir); ret = CMSRET_SUCCESS; } close(fd); return ret; }
/** Ported from getLanInfo * */ CmsRet oal_getLanInfo(const char *lan_ifname, struct in_addr *lan_ip, struct in_addr *lan_subnetmask) { #ifdef DESKTOP_LINUX cmsLog_debug("fake ip info for interface %s", lan_ifname); lan_ip->s_addr = 0xc0a80100; /* 192.168.1.0 */ lan_subnetmask->s_addr = 0xffffff00; /* 255.255.255.0 */ return CMSRET_SUCCESS; #else int socketfd; struct ifreq lan; if ((socketfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { cmsLog_error("failed to open socket, errno=%d", errno); return CMSRET_INTERNAL_ERROR; } strcpy(lan.ifr_name,lan_ifname); if (ioctl(socketfd,SIOCGIFADDR,&lan) < 0) { cmsLog_error("SIOCGIFADDR failed, errno=%d", errno); close(socketfd); return CMSRET_INTERNAL_ERROR; } *lan_ip = ((struct sockaddr_in *)&(lan.ifr_addr))->sin_addr; if (ioctl(socketfd,SIOCGIFNETMASK,&lan) < 0) { cmsLog_error("SIOCGIFNETMASK failed, errno=%d", errno); close(socketfd); return CMSRET_INTERNAL_ERROR; } *lan_subnetmask = ((struct sockaddr_in *)&(lan.ifr_netmask))->sin_addr; close(socketfd); return CMSRET_SUCCESS; #endif }
CmsRet cmsUtl_parseUrl(const char *url, UrlProto *proto, char **addr, UINT16 *port, char **path) { int n = 0; char *p = NULL; char protocol[BUFLEN_16]; char host[BUFLEN_1024]; char uri[BUFLEN_1024]; if (url == NULL) { cmsLog_debug("url is NULL"); return CMSRET_INVALID_ARGUMENTS; } *port = 0; protocol[0] = host[0] = uri[0] = '\0'; /* proto */ p = (char *) url; if ((p = strchr(url, ':')) == NULL) { return CMSRET_INVALID_ARGUMENTS; } n = p - url; strncpy(protocol, url, n); protocol[n] = '\0'; if (!strcmp(protocol, "http")) { *proto = URL_PROTO_HTTP; } else if (!strcmp(protocol, "https")) { *proto = URL_PROTO_HTTPS; } else if (!strcmp(protocol, "ftp")) { *proto = URL_PROTO_FTP; } else if (!strcmp(protocol, "tftp")) { *proto = URL_PROTO_TFTP; } else { cmsLog_error("unrecognized proto in URL %s", url); return CMSRET_INVALID_ARGUMENTS; } /* skip "://" */ if (*p++ != ':') return CMSRET_INVALID_ARGUMENTS; if (*p++ != '/') return CMSRET_INVALID_ARGUMENTS; if (*p++ != '/') return CMSRET_INVALID_ARGUMENTS; /* host */ { char *pHost = host; while (*p && *p != ':' && *p != '/') { *pHost++ = *p++; } *pHost = '\0'; } if (strlen(host) != 0) { *addr = cmsMem_strdup(host); } else { cmsLog_error("unrecognized host in URL %s", url); return CMSRET_INVALID_ARGUMENTS; } /* end */ if (*p == '\0') { *path = cmsMem_strdup("/"); return CMSRET_SUCCESS; } /* port */ if (*p == ':') { char buf[BUFLEN_16]; char *pBuf = buf; p++; while (isdigit(*p)) { *pBuf++ = *p++; } *pBuf = '\0'; if (strlen(buf) == 0) { CMSMEM_FREE_BUF_AND_NULL_PTR(*addr); cmsLog_error("unrecognized port in URL %s", url); return CMSRET_INVALID_ARGUMENTS; } *port = atoi(buf); } /* path */ if (*p == '/') { char *pUri = uri; while ((*pUri++ = *p++)); *path = cmsMem_strdup(uri); } else { *path = cmsMem_strdup("/"); } return CMSRET_SUCCESS; }
CmsRet oal_spawnProcess(const SpawnProcessInfo *spawnInfo, SpawnedProcessInfo *procInfo) { char **argv=NULL; SINT32 pid, i; CmsRet ret=CMSRET_SUCCESS; if ((ret = parseArgs(spawnInfo->exe, spawnInfo->args, &argv)) != CMSRET_SUCCESS) { return ret; } pid = fork(); if (pid == 0) { SINT32 devNullFd=-1, fd; /* * This is the child. */ /* * If user gave us specific instructions on fd re-routing, * do it before execv. */ if ((spawnInfo->stdinFd == -1) || (spawnInfo->stdoutFd == -1) || (spawnInfo->stderrFd == -1)) { devNullFd = open("/dev/null", O_RDWR); if (devNullFd == -1) { cmsLog_error("open of /dev/null failed"); exit(-1); } } if (spawnInfo->stdinFd != 0) { close(0); fd = (spawnInfo->stdinFd == -1) ? devNullFd : spawnInfo->stdinFd; dup2(fd, 0); } if (spawnInfo->stdoutFd != 1) { close(1); fd = (spawnInfo->stdoutFd == -1) ? devNullFd : spawnInfo->stdoutFd; dup2(fd, 1); } if (spawnInfo->stderrFd != 2) { close(2); fd = (spawnInfo->stderrFd == -1) ? devNullFd : spawnInfo->stderrFd; dup2(fd, 2); } if (devNullFd != -1) { close(devNullFd); } /* if child has a serverFd, dup it to the fixed number */ if (spawnInfo->serverFd != -1) { close(CMS_DYNAMIC_LAUNCH_SERVER_FD); dup2(spawnInfo->serverFd, CMS_DYNAMIC_LAUNCH_SERVER_FD); } #if defined(AEI_VDSL_CUSTOMER_NCS) #ifdef SUPPORT_HTTPD_SSL /* httpd second listen fd, dup it if necessary to the fixed number also */ if (spawnInfo->eid == EID_HTTPD || spawnInfo->eid == EID_HTTPSD) { if (spawnInfo->serverFd2 != -1) { close(CMS_DYNAMIC_LAUNCH_SERVER_FD2); dup2(spawnInfo->serverFd2, CMS_DYNAMIC_LAUNCH_SERVER_FD2); } } #endif #endif /* close all of the child's other fd's */ for (i=3; i <= spawnInfo->maxFd; i++) { #if defined(AEI_VDSL_CUSTOMER_NCS) && defined(SUPPORT_HTTPD_SSL) if (i == CMS_DYNAMIC_LAUNCH_SERVER_FD || i == CMS_DYNAMIC_LAUNCH_SERVER_FD2) #else if (i == CMS_DYNAMIC_LAUNCH_SERVER_FD) #endif // if ((spawnInfo->serverFd != -1) && // (i == CMS_DYNAMIC_LAUNCH_SERVER_FD)) { continue; } close(i); } /* * Set all signal handlers back to default action, * in case the parent had set them to SIG_IGN or something. * See kernel/linux/include/asm-mips/signal.h */ signal(SIGHUP, SIG_DFL); if (spawnInfo->inheritSigint == FALSE) { /* * Note: there is a bug in pthread library in the 4.4.2 toolchain * which breaks SIG_IGN inheritance. Apps are advised to set * SIGINT handler explicitly in the app itself. */ signal(SIGINT, SIG_DFL); } signal(SIGQUIT, SIG_DFL); signal(SIGILL, SIG_DFL); signal(SIGTRAP, SIG_DFL); signal(SIGABRT, SIG_DFL); /* same as SIGIOT */ #ifndef DESKTOP_LINUX signal(SIGEMT, SIG_DFL); #endif signal(SIGFPE, SIG_DFL); signal(SIGBUS, SIG_DFL); signal(SIGSEGV, SIG_DFL); signal(SIGSYS, SIG_DFL); signal(SIGPIPE, SIG_DFL); signal(SIGALRM, SIG_DFL); signal(SIGTERM, SIG_DFL); signal(SIGUSR1, SIG_DFL); signal(SIGUSR2, SIG_DFL); signal(SIGCHLD, SIG_DFL); /* same as SIGCLD */ signal(SIGPWR, SIG_DFL); signal(SIGWINCH, SIG_DFL); signal(SIGURG, SIG_DFL); signal(SIGIO, SIG_DFL); /* same as SIGPOLL */ signal(SIGSTOP, SIG_DFL); signal(SIGTSTP, SIG_DFL); signal(SIGCONT, SIG_DFL); signal(SIGTTIN, SIG_DFL); signal(SIGTTOU, SIG_DFL); signal(SIGVTALRM, SIG_DFL); signal(SIGPROF, SIG_DFL); signal(SIGXCPU, SIG_DFL); signal(SIGXFSZ, SIG_DFL); /* overlay child executable over myself */ execv(spawnInfo->exe, argv); /* We should not reach this line. If we do, exec has failed. */ exit(-1); } /* this is the parent */ freeArgs(argv); /* don't need these anymore */ memset(procInfo, 0, sizeof(SpawnedProcessInfo)); procInfo->pid = pid; procInfo->status = PSTAT_RUNNING; if (spawnInfo->spawnMode == SPAWN_AND_WAIT) { CollectProcessInfo collectInfo; collectInfo.collectMode = COLLECT_PID_TIMEOUT; collectInfo.pid = pid; collectInfo.timeout = spawnInfo->timeout; ret = oal_collectProcess(&collectInfo, procInfo); if (ret == CMSRET_TIMED_OUT) { CmsRet r2; cmsLog_debug("pid %d has not exited in %d ms, SIGKILL", spawnInfo->timeout); r2 = oal_signalProcess(pid, SIGKILL); if (r2 != CMSRET_SUCCESS) { cmsLog_error("SIGKILL to pid %d failed with ret=%d", pid, r2); } /* try one more time to collect it */ collectInfo.collectMode = COLLECT_PID_TIMEOUT; collectInfo.pid = pid; collectInfo.timeout = COLLECT_WAIT_INTERVAL_MS; r2 = oal_collectProcess(&collectInfo, procInfo); if (r2 == CMSRET_SUCCESS) { /* we finally go it, otherwise, leave ret at CMSRET_TIMED_OUT */ ret = CMSRET_SUCCESS; } } else if (ret != CMSRET_SUCCESS) { /* some other error with collect */ cmsLog_error("Could not collect pid %d", pid); } } return ret; }
/** Give a single string, allocate and fill in an array of char *'s * each pointing to an individually malloc'd buffer containing a single * argument in the string; the array will end with a char * slot containing NULL. * * This array can be passed to execv. * This array must be freed by calling freeArgs. */ CmsRet parseArgs(const char *cmd, const char *args, char ***argv) { UINT32 numArgs=3, i, len, argIndex=0; UBOOL8 inSpace=TRUE; const char *cmdStr; char **array; len = (args == NULL) ? 0 : strlen(args); /* * First count the number of spaces to determine the number of args * there are in the string. */ for (i=0; i < len; i++) { if ((args[i] == ' ') && (!inSpace)) { numArgs++; inSpace = TRUE; } else { inSpace = FALSE; } } array = (char **) cmsMem_alloc((numArgs) * sizeof(char *), ALLOC_ZEROIZE); if (array == NULL) { return CMSRET_RESOURCE_EXCEEDED; } /* locate the command name, last part of string */ cmdStr = strrchr(cmd, '/'); if (cmdStr == NULL) { cmdStr = cmd; } else { cmdStr++; /* move past the / */ } /* copy the command into argv[0] */ array[argIndex] = cmsMem_alloc(strlen(cmdStr) + 1, ALLOC_ZEROIZE); if (array[argIndex] == NULL) { cmsLog_error("malloc of %d failed", strlen(cmdStr) + 1); freeArgs(array); return CMSRET_RESOURCE_EXCEEDED; } else { strcpy(array[argIndex], cmdStr); argIndex++; } /* * Wow, this is going to be incredibly messy. I have to malloc a buffer * for each arg and copy them in individually. */ inSpace = TRUE; for (i=0; i < len; i++) { if ((args[i] == ' ') && (!inSpace)) { numArgs++; inSpace = TRUE; } else if ((args[i] != ' ') && (inSpace)) { UINT32 startIndex, endIndex; /* * this is the first character I've seen after a space. * Figure out how many letters this arg is, malloc a buffer * to hold it, and copy it into the buffer. */ startIndex = i; endIndex = i; while ((endIndex < len) && (args[endIndex] != ' ')) { endIndex++; } array[argIndex] = cmsMem_alloc(endIndex - startIndex + 1, ALLOC_ZEROIZE); if (array[argIndex] == NULL) { cmsLog_error("malloc of %d failed", endIndex - startIndex + 1); freeArgs(array); return CMSRET_RESOURCE_EXCEEDED; } memcpy(array[argIndex], &args[startIndex], endIndex - startIndex); /* cmsLog_debug("index=%d len=%d (%s)", argIndex, endIndex - startIndex, &args[startIndex]); */ argIndex++; inSpace = FALSE; } } /* check what we did */ i = 0; while (array[i] != NULL) { cmsLog_debug("argv[%d] = %s", i, array[i]); i++; } (*argv) = array; return CMSRET_SUCCESS; }
/** Return a pointer to the beginning of the requested flash buffer. * * On DESKTOP_LINUX, this just opens the fake flash file, allocates a buffer, * read contents of fake flash file into buffer, and returns pointer to the * buffer. Persistent flash data is the only thing in the flash file (for now). * If fake flash file is not present, create one and fill it with zeros. */ char *fake_getSharedBlks(int start_block, int num_blocks) { UINT32 bufLen; char *buf=NULL; char path[BUFLEN_1024]; struct stat statbuf; int rc, fd; CmsRet ret; cmsLog_debug("reading block %d through %d", start_block, start_block+num_blocks); if (start_block != 0) { cmsLog_error("cannot handle non-zero start block yet."); return NULL; } if (num_blocks > FAKE_NUM_PSP_FLASH_BLOCKS) { cmsLog_error("requested more blocks than PSP flash blocks, not handled."); return NULL; } /* first allocate the buffer we will need for the read */ bufLen = FAKE_FLASH_BLOCK_SIZE * FAKE_NUM_PSP_FLASH_BLOCKS; if ((buf = cmsMem_alloc(bufLen, ALLOC_ZEROIZE)) == NULL) { cmsLog_error("malloc of %d bytes failed", bufLen); return NULL; } /* form path to the flash file */ if ((ret = cmsUtl_getBaseDir(path, sizeof(path))) != CMSRET_SUCCESS) { cmsLog_error("getBaseDir failed, abort func"); cmsMem_free(buf); return NULL; } else { UINT32 offset; offset = strlen(path); snprintf(&(path[offset]), sizeof(path)-offset, "/%s", FAKE_FLASH_PSP_FILENAME); } cmsLog_debug("checking for flash file at %s", path); if ((rc = stat(path, &statbuf)) < 0) { cmsLog_debug("creating fake flash file and initialize to zeros"); fd = open(path, O_CREAT|O_RDWR, 0644); if (fd < 0) { cmsLog_error("create of flash file %s failed, errno=%d", path, errno); cmsMem_free(buf); return NULL; } /* fill rest of file with zeros */ rc = write(fd, buf, bufLen); cmsLog_debug("filler write returned %d", rc); close(fd); } /* * at this point, we know there is a flash file, so just open it and read it. * Don't bother with offsets for now. Just assume PSP is at the beginning * of the flash. */ fd = open(path, O_RDWR); rc = read(fd, buf, num_blocks * FAKE_FLASH_BLOCK_SIZE); if (rc != num_blocks * FAKE_FLASH_BLOCK_SIZE) { cmsLog_error("unexpected rc %d from read, expected %d", rc, num_blocks * FAKE_FLASH_BLOCK_SIZE); CMSMEM_FREE_BUF_AND_NULL_PTR(buf); } close(fd); return buf; }
void *cmsMem_alloc(UINT32 size, UINT32 allocFlags) { void *buf; UINT32 allocSize; #ifdef CMS_MEM_LEAK_TRACING initAllocSeq(); #endif allocSize = REAL_ALLOC_SIZE(size); #ifdef MDM_SHARED_MEM if (allocFlags & ALLOC_SHARED_MEM) { #ifdef CMS_MEM_LEAK_TRACING buf = bget(allocSize, allocSeq); #else buf = bget(allocSize); #endif } else #endif { buf = oal_malloc(allocSize); if (buf) { mStats.bytesAllocd += size; mStats.numAllocs++; } } if (buf != NULL) { UINT32 *intBuf = (UINT32 *) buf; UINT32 intSize = allocSize / sizeof(UINT32); if (allocFlags & ALLOC_ZEROIZE) { memset(buf, 0, allocSize); } #ifdef CMS_MEM_POISON_ALLOC_FREE else { /* * Set alloc'ed buffer to garbage to catch use-before-init. * But we also allocate huge buffers for storing image downloads. * Don't bother writing garbage to those huge buffers. */ if (allocSize < 64 * 1024) { memset(buf, CMS_MEM_ALLOC_PATTERN, allocSize); } } #endif /* * Record the allocFlags in the first word, and the * size of user buffer in the next 2 words of the buffer. * Make 2 copies of the size in case one of the copies gets corrupted by * an underflow. Make one copy the XOR of the other so that there are * not so many 0's in size fields. */ intBuf[0] = allocFlags; intBuf[1] = size; intBuf[2] = intBuf[1] ^ 0xffffffff; buf = &(intBuf[3]); /* this gets returned to user */ #ifdef CMS_MEM_DEBUG { UINT8 *charBuf = (UINT8 *) buf; UINT32 i, roundup4Size = ROUNDUP4(size); for (i=size; i < roundup4Size; i++) { charBuf[i] = CMS_MEM_FOOTER_PATTERN & 0xff; } intBuf[intSize - 1] = CMS_MEM_FOOTER_PATTERN; intBuf[intSize - 2] = CMS_MEM_FOOTER_PATTERN; } #endif #ifdef CMS_MEM_LEAK_TRACING { AllocRecord *allocRec; if (!(allocRec = calloc(1, sizeof(AllocRecord)))) { cmsLog_error("could not malloc a record to track alloc"); } else { allocRec->bufAddr = buf; allocRec->userSize = size; allocRec->seq = allocSeq++; backtrace(allocRec->stackAddr, NUM_STACK_ENTRIES); /* * new allocs are placed at the beginning of the list, right after * the head. */ dlist_append((struct dlist_node *)allocRec, &glbAllocRec); } /* * do periodic garbage collection on the allocRecs which point * to shmBuf's that has been freed by another app. */ if ((allocSeq % 2000) == 0) { cmsLog_debug("Starting allocRec garbage collection"); garbageCollectAllocRec(); cmsLog_debug("garbage collection done"); } } #endif } return buf; }
/** Get list of all keys/tokenID's in the scratch pad. * * This function follows the logic of the same function name in * bcmdrivers/opensource/char/board/bcm963xx/impl1/bcm63xx_flash.c * * @return greater than 0 means number of bytes copied to tokBuf, * 0 means fail, * negative number means provided buffer is not big enough and the * absolute value of the negative number is the number of bytes needed. */ int fake_kerSysScratchPadList(char *tokBuf, int bufLen) { PSP_TOKEN pToken = NULL; char *pBuf = NULL; char *pShareBuf = NULL; char *startPtr = NULL; int usedLen; int tokenNameLen=0; int copiedLen=0; int needLen=0; int sts = 0; if (fInfo.flash_scratch_pad_length == 0) return sts; if( (pShareBuf = fake_getSharedBlks(fInfo.flash_scratch_pad_start_blk, (fInfo.flash_scratch_pad_start_blk + fInfo.flash_scratch_pad_number_blk))) == NULL ) { cmsLog_error("could not get sharedBlks"); return sts; } // pBuf points to SP buf pBuf = pShareBuf + fInfo.flash_scratch_pad_blk_offset; if(memcmp(((PSP_HEADER)pBuf)->SPMagicNum, MAGIC_NUMBER, MAGIC_NUM_LEN) != 0) { cmsLog_error("Scratch pad is not initialized.\n"); fake_retriedKfree(pShareBuf); return sts; } // Walk through all the tokens usedLen = sizeof(SP_HEADER); startPtr = pBuf + sizeof(SP_HEADER); pToken = (PSP_TOKEN) startPtr; while( pToken->tokenName[0] != '\0' && pToken->tokenLen > 0 && ((usedLen + pToken->tokenLen) <= fInfo.flash_scratch_pad_length)) { tokenNameLen = strlen(pToken->tokenName); needLen += tokenNameLen + 1; cmsLog_debug("found tokenName=%s copiedLen=%d needLen=%d bufLen=%d", pToken->tokenName, copiedLen, needLen, bufLen); if (needLen <= bufLen) { strcpy(&tokBuf[copiedLen], pToken->tokenName); copiedLen += tokenNameLen + 1; } usedLen += ((pToken->tokenLen + 0x03) & ~0x03); startPtr += sizeof(SP_TOKEN) + ((pToken->tokenLen + 0x03) & ~0x03); pToken = (PSP_TOKEN) startPtr; } if ( needLen > bufLen ) { // User may purposely pass in a 0 length buffer just to get // the size, so don't log this as an error. sts = needLen * (-1); } else { sts = copiedLen; } fake_retriedKfree(pShareBuf); return sts; }
/** Get a buffer from the scratch pad based on tokenId. * * This function follows the logic of the same function name in * bcmdrivers/opensource/char/board/bcm963xx/impl1/bcm63xx_flash.c * * @return greater than 0 means number of bytes copied to tokBuf, * 0 means fail, * negative number means provided buffer is not big enough and the * absolute value of the negative number is the number of bytes needed. */ int fake_kerSysScratchPadGet(char *tokenId, char *tokBuf, int bufLen) { PSP_TOKEN pToken = NULL; char *pBuf = NULL; char *pShareBuf = NULL; char *startPtr = NULL; int usedLen; int sts = 0; if (fInfo.flash_scratch_pad_length == 0) return sts; if( (pShareBuf = fake_getSharedBlks(fInfo.flash_scratch_pad_start_blk, (fInfo.flash_scratch_pad_start_blk + fInfo.flash_scratch_pad_number_blk))) == NULL ) { cmsLog_error("could not get sharedBlks"); return sts; } // pBuf points to SP buf pBuf = pShareBuf + fInfo.flash_scratch_pad_blk_offset; if(memcmp(((PSP_HEADER)pBuf)->SPMagicNum, MAGIC_NUMBER, MAGIC_NUM_LEN) != 0) { cmsLog_error("Scratch pad is not initialized.\n"); fake_retriedKfree(pShareBuf); return sts; } // search for the token usedLen = sizeof(SP_HEADER); startPtr = pBuf + sizeof(SP_HEADER); pToken = (PSP_TOKEN) startPtr; while( pToken->tokenName[0] != '\0' && pToken->tokenLen > 0 && pToken->tokenLen < fInfo.flash_scratch_pad_length && usedLen < fInfo.flash_scratch_pad_length ) { if (strncmp(pToken->tokenName, tokenId, TOKEN_NAME_LEN) == 0) { if ( pToken->tokenLen > bufLen ) { cmsLog_error("The length %d of token %s is greater than buffer len %d.", pToken->tokenLen, pToken->tokenName, bufLen); sts = pToken->tokenLen * (-1); } else { cmsLog_debug("found token %s, copying out (len=%d)", tokenId, pToken->tokenLen); memcpy(tokBuf, startPtr + sizeof(SP_TOKEN), pToken->tokenLen); sts = pToken->tokenLen; } break; } usedLen += ((pToken->tokenLen + 0x03) & ~0x03); startPtr += sizeof(SP_TOKEN) + ((pToken->tokenLen + 0x03) & ~0x03); pToken = (PSP_TOKEN) startPtr; } fake_retriedKfree(pShareBuf); return sts; }
CmsRet devCtl_boardIoctl(UINT32 boardIoctl, BOARD_IOCTL_ACTION action, char *string, SINT32 strLen, SINT32 offset, void *data) { BOARD_IOCTL_PARMS ioctlParms; SINT32 boardFd = 0; SINT32 rc; CmsRet ret=CMSRET_SUCCESS; #ifdef DESKTOP_LINUX /* don't open anything, ioctl to this fd will be faked anyways */ boardFd = 77777; #else boardFd = open(BOARD_DEVICE_NAME, O_RDWR); #endif if ( boardFd != -1 ) { ioctlParms.string = string; ioctlParms.strLen = strLen; ioctlParms.offset = offset; ioctlParms.action = action; ioctlParms.buf = data; ioctlParms.result = -1; #if defined(AEI_VDSL_CUSTOMER_NCS) if(boardIoctl == BOARD_IOCTL_FLASH_WRITE && (action ==PERSISTENT)) { FILE *fp = NULL; if((fp=fopen("/var/write_psi_lock", "w")) != NULL) fclose(fp); } #endif #ifdef DESKTOP_LINUX rc = fake_ioctl(boardIoctl, &ioctlParms); #else rc = ioctl(boardFd, boardIoctl, &ioctlParms); close(boardFd); #endif #if defined(AEI_VDSL_CUSTOMER_NCS) if(boardIoctl == BOARD_IOCTL_FLASH_WRITE && (action ==PERSISTENT)) unlink("/var/write_psi_lock"); #endif /* * When reading the scratch pad, the return value indicates the count. * Check for that condition first. */ if (((boardIoctl == BOARD_IOCTL_FLASH_READ) && (action == SCRATCH_PAD)) || ((boardIoctl == BOARD_IOCTL_FLASH_LIST) && (action == SCRATCH_PAD)) || boardIoctl == BOARD_IOCTL_BOOT_IMAGE_OPERATION) { /* * The kernel will either return the number of bytes read, * or if the user provided buffer was not big enough, a * negative number indicating the number of bytes needed. * See cms_psp.h, cmsPsp_get(). */ ret = (CmsRet) ioctlParms.result; } else { if (rc < 0) { cmsLog_debug("boardIoctl=0x%x action=%d rc=%d errno=%d", boardIoctl, action, rc, errno); ret = CMSRET_INVALID_ARGUMENTS; } /* ioctl specific return data */ if (ret == CMSRET_SUCCESS) { if ((boardIoctl == BOARD_IOCTL_GET_PSI_SIZE) || (boardIoctl == BOARD_IOCTL_GET_BACKUP_PSI_SIZE) || (boardIoctl == BOARD_IOCTL_GET_SYSLOG_SIZE) || (boardIoctl == BOARD_IOCTL_GET_CHIP_ID) || (boardIoctl == BOARD_IOCTL_GET_NUM_ENET_MACS) || (boardIoctl == BOARD_IOCTL_GET_NUM_ENET_PORTS) || #if defined(AEI_VDSL_CUSTOMER_CENTURYLINK) (boardIoctl == BOARD_IOCTL_GET_POWERLED_STATUS) || (boardIoctl == BOARD_IOCTL_GET_FLASH_TOTAL) || (boardIoctl == BOARD_IOCTL_GET_FLASH_USED) || #endif (boardIoctl == BOARD_IOCTL_GET_SDRAM_SIZE) || (boardIoctl == BOARD_IOCTL_FLASH_READ && action == FLASH_SIZE)) { if (data != NULL) { *((UINT32 *)data) = (UINT32) ioctlParms.result; } } #ifdef AEI_VDSL_CUSTOMER_NCS else if(boardIoctl == BOARD_IOCTL_GET_FS_OFFSET) { if (data != NULL) { *((unsigned long *)data) = (unsigned long) ioctlParms.result; } } #endif } } } else { cmsLog_error("Unable to open device %s", BOARD_DEVICE_NAME); ret = CMSRET_INTERNAL_ERROR; } return ret; }
/** Do first level processing of ioctl. * * This function follows the logic of * bcmdrivers/opensource/char/board/bcm963xx/impl1/board.c:board_ioctl */ int fake_ioctl(UINT32 command, BOARD_IOCTL_PARMS *ctrlParms) { int ret; switch(command) { case BOARD_IOCTL_FLASH_WRITE: switch(ctrlParms->action) { case SCRATCH_PAD: if (ctrlParms->offset == -1) ret = fake_kerSysScratchPadClearAll(); else ret = fake_kerSysScratchPadSet(ctrlParms->string, ctrlParms->buf, ctrlParms->offset); break; case BCM_IMAGE_WHOLE: cmsLog_debug("fake whole image write, buf=%p len=%d", ctrlParms->string, ctrlParms->strLen); ret = fake_kerSysBcmImageSetWhole((UINT8 *) ctrlParms->string, ctrlParms->strLen); break; case BCM_IMAGE_CFE: cmsLog_debug("fake cfe write, buf=%p len=%d", ctrlParms->string, ctrlParms->strLen); ret = fake_kerSysBcmImageSetCfe((UINT8 *) ctrlParms->string, ctrlParms->strLen); break; case BCM_IMAGE_FS: cmsLog_debug("fake fs+kernel write, buf=%p len=%d", ctrlParms->string, ctrlParms->strLen); ret = fake_flashFsKernelImage((UINT8 *) ctrlParms->string, ctrlParms->strLen); break; case PERSISTENT: cmsLog_debug("fake config write, buf=%p len=%d", ctrlParms->string, ctrlParms->strLen); ret = fake_kerSysPersistentSet((UINT8 *) ctrlParms->string, ctrlParms->strLen); break; #ifdef SUPPORT_BACKUP_PSI case BACKUP_PSI: cmsLog_debug("fake backup config write, buf=%p len=%d", ctrlParms->string, ctrlParms->strLen); ret = fake_kerSysBackupPsiSet((UINT8 *) ctrlParms->string, ctrlParms->strLen); break; #endif default: ret = -EINVAL; cmsLog_error("unhandled action %d in BOARD_IOCTL_FLASH_WRITE"); break; } ctrlParms->result = ret; break; case BOARD_IOCTL_FLASH_READ: switch (ctrlParms->action) { case SCRATCH_PAD: ret = fake_kerSysScratchPadGet(ctrlParms->string, ctrlParms->buf, ctrlParms->offset); ctrlParms->result = ret; break; case FLASH_SIZE: ctrlParms->result = 4 * 1024 * 1024; /* 4 MB? */ ret = 0; break; case PERSISTENT: cmsLog_debug("fake config read, buf=%p len=%d", ctrlParms->string, ctrlParms->strLen); ret = fake_kerSysPersistentGet((UINT8 *) ctrlParms->string, ctrlParms->strLen); break; #ifdef SUPPORT_BACKUP_PSI case BACKUP_PSI: cmsLog_debug("fake backup psi config read, buf=%p len=%d", ctrlParms->string, ctrlParms->strLen); ret = fake_kerSysBackupPsiGet((UINT8 *) ctrlParms->string, ctrlParms->strLen); break; #endif default: ret = -EINVAL; ctrlParms->result = ret; cmsLog_error("unhandled action %d in BOARD_IOCTL_FLASH_READ"); break; } break; case BOARD_IOCTL_FLASH_LIST: switch (ctrlParms->action) { case SCRATCH_PAD: ret = fake_kerSysScratchPadList(ctrlParms->buf, ctrlParms->offset); ctrlParms->result = ret; break; default: ret = -EINVAL; ctrlParms->result = ret; cmsLog_error("unhandled action %d in BOARD_IOCTL_FLASH_READ"); break; } break; case BOARD_IOCTL_GET_PSI_SIZE: ctrlParms->result = 24 * 1024; ret = 0; break; #ifdef SUPPORT_BACKUP_PSI case BOARD_IOCTL_GET_BACKUP_PSI_SIZE: ctrlParms->result = 24 * 1024; ret = 0; break; #endif case BOARD_IOCTL_GET_NUM_ENET_MACS: ctrlParms->result = 2; ret = 0; break; case BOARD_IOCTL_GET_NUM_ENET_PORTS: ctrlParms->result = 4; ret = 0; break; case BOARD_IOCTL_GET_CHIP_ID: #if defined(CHIP_6368) ctrlParms->result = 0x6368; #endif #if defined(CHIP_6362) ctrlParms->result = 0x6362; #endif #if defined(CHIP_6328) ctrlParms->result = 0x6328; #endif #if defined(CHIP_63268) ctrlParms->result = 0x63268; #endif ret = 0; break; case BOARD_IOCTL_GET_CFE_VER: if (ctrlParms->strLen < 5) { cmsLog_error("buf too short, need 5, got %d", ctrlParms->strLen); ret = -1; } else { ctrlParms->string[0] = 1; ctrlParms->string[1] = 0; ctrlParms->string[2] = 37; ctrlParms->string[3] = 9; ctrlParms->string[4] = 14; ret = 0; } break; case BOARD_IOCTL_GET_ID: /* this is get BOARD_ID */ { const char *boardIdStr="DESKTOP_LINUX"; if (ctrlParms->strLen < (SINT32) (strlen(boardIdStr) + 1)) { cmsLog_error("buf too short, need %d, got %d", strlen(boardIdStr)+1, ctrlParms->strLen); ret = -1; } else { sprintf(ctrlParms->string, "%s", boardIdStr); ret = 0; } } break; case BOARD_IOCTL_GET_BASE_MAC_ADDRESS: ctrlParms->string[0] = (char) 0; ctrlParms->string[1] = (char) 0x11; ctrlParms->string[2] = (char) 0x22; ctrlParms->string[3] = (char) 0x33; ctrlParms->string[4] = (char) 0x44; ctrlParms->string[5] = (char) 0x55; ret = 0; break; default: ret = -EINVAL; cmsLog_error("unhandled board ioctl 0x%x", command); } return ret; }
CmsRet oalMsg_receive(SINT32 fd, CmsMsgHeader **buf, UINT32 *timeout) { CmsMsgHeader *msg; SINT32 rc; CmsRet ret; if (buf == NULL) { cmsLog_error("buf is NULL!"); return CMSRET_INVALID_ARGUMENTS; } else { *buf = NULL; } if (timeout) { if ((ret = waitForDataAvailable(fd, *timeout)) != CMSRET_SUCCESS) { return ret; } } /* * Read just the header in the first read. * Do not try to read more because we might get part of * another message in the TCP socket. */ msg = (CmsMsgHeader *) cmsMem_alloc(sizeof(CmsMsgHeader), ALLOC_ZEROIZE); if (msg == NULL) { cmsLog_error("alloc of msg header failed"); return CMSRET_RESOURCE_EXCEEDED; } rc = read(fd, msg, sizeof(CmsMsgHeader)); if ((rc == 0) || ((rc == -1) && (errno == 131))) /* new 2.6.21 kernel seems to give us this before rc==0 */ { /* broken connection */ cmsMem_free(msg); return CMSRET_DISCONNECTED; } else if (rc < 0 || rc != sizeof(CmsMsgHeader)) { cmsLog_error("bad read, rc=%d errno=%d", rc, errno); cmsMem_free(msg); return CMSRET_INTERNAL_ERROR; } if (msg->dataLength > 0) { SINT32 totalReadSoFar=0; SINT32 totalRemaining=msg->dataLength; char *inBuf; /* there is additional data in the message */ msg = (CmsMsgHeader *) cmsMem_realloc(msg, sizeof(CmsMsgHeader) + msg->dataLength); if (msg == NULL) { cmsLog_error("realloc to %d bytes failed", sizeof(CmsMsgHeader) + msg->dataLength); cmsMem_free(msg); return CMSRET_RESOURCE_EXCEEDED; } inBuf = (char *) (msg + 1); while (totalReadSoFar < msg->dataLength) { cmsLog_debug("reading segment: soFar=%d total=%d", totalReadSoFar, totalRemaining); if (timeout) { if ((ret = waitForDataAvailable(fd, *timeout)) != CMSRET_SUCCESS) { cmsMem_free(msg); return ret; } } rc = read(fd, inBuf, totalRemaining); if (rc <= 0) { cmsLog_error("bad data read, rc=%d errno=%d readSoFar=%d remaining=%d", rc, errno, totalReadSoFar, totalRemaining); cmsMem_free(msg); return CMSRET_INTERNAL_ERROR; } else { inBuf += rc; totalReadSoFar += rc; totalRemaining -= rc; } } } *buf = msg; return CMSRET_SUCCESS; }
CmsRet oalMsg_init(CmsEntityId eid, void **msgHandle) { CmsMsgHandle *handle; const CmsEntityInfo *eInfo; struct sockaddr_un serverAddr; SINT32 rc; if ((eInfo = cmsEid_getEntityInfo(eid)) == NULL) { cmsLog_error("Invalid eid %d", eid); return CMSRET_INVALID_ARGUMENTS; } if ((handle = (CmsMsgHandle *) cmsMem_alloc(sizeof(CmsMsgHandle), ALLOC_ZEROIZE)) == NULL) { cmsLog_error("could not allocate storage for msg handle"); return CMSRET_RESOURCE_EXCEEDED; } /* store caller's eid */ handle->eid = eid; #ifdef DESKTOP_LINUX /* * Applications may be run without smd on desktop linux, so if we * don't see a socket for smd, don't bother connecting to it. */ { struct stat statbuf; if ((rc = stat(SMD_MESSAGE_ADDR, &statbuf)) < 0) { handle->commFd = CMS_INVALID_FD; handle->standalone = TRUE; *msgHandle = (void *) handle; cmsLog_notice("no smd server socket detected, running in standalone mode."); return CMSRET_SUCCESS; } } #endif /* DESKTOP_LINUX */ /* * Create a unix domain socket. */ handle->commFd = socket(AF_LOCAL, SOCK_STREAM, 0); if (handle->commFd < 0) { cmsLog_error("Could not create socket"); cmsMem_free(handle); return CMSRET_INTERNAL_ERROR; } /* * Set close-on-exec, even though all apps should close their * fd's before fork and exec. */ if ((rc = fcntl(handle->commFd, F_SETFD, FD_CLOEXEC)) != 0) { cmsLog_error("set close-on-exec failed, rc=%d errno=%d", rc, errno); close(handle->commFd); cmsMem_free(handle); return CMSRET_INTERNAL_ERROR; } /* * Connect to smd. */ memset(&serverAddr, 0, sizeof(serverAddr)); serverAddr.sun_family = AF_LOCAL; strncpy(serverAddr.sun_path, SMD_MESSAGE_ADDR, sizeof(serverAddr.sun_path)); rc = connect(handle->commFd, (struct sockaddr *) &serverAddr, sizeof(serverAddr)); if (rc != 0) { cmsLog_error("connect to %s failed, rc=%d errno=%d", SMD_MESSAGE_ADDR, rc, errno); close(handle->commFd); cmsMem_free(handle); return CMSRET_INTERNAL_ERROR; } else { cmsLog_debug("commFd=%d connected to smd", handle->commFd); } /* send a launched message to smd */ { CmsRet ret; CmsMsgHeader launchMsg = EMPTY_MSG_HEADER; launchMsg.type = CMS_MSG_APP_LAUNCHED; launchMsg.src = (eInfo->flags & EIF_MULTIPLE_INSTANCES) ? MAKE_SPECIFIC_EID(getpid(), eid) : eid; launchMsg.dst = EID_SMD; launchMsg.flags_event = 1; if ((ret = oalMsg_send(handle->commFd, &launchMsg)) != CMSRET_SUCCESS) { close(handle->commFd); cmsMem_free(handle); return CMSRET_INTERNAL_ERROR; } else { cmsLog_debug("sent LAUNCHED message to smd"); } } /* successful, set handle pointer */ *msgHandle = (void *) handle; return CMSRET_SUCCESS; }
static int std_rcv_pado(struct session* ses, struct pppoe_packet *p_in, struct pppoe_packet **p_out){ if( verify_packet(ses, p_in) < 0) return -1; if(ses->state != PADO_CODE ){ poe_error(ses,"Unexpected packet: %P",p_in); return 0; } if (DEB_DISC2) { poe_dbglog (ses,"PADO received: %P", p_in); } // brcm: add code to get service name and put it in the /var/fyi/wan/servicename file if (p_in->tags[0]->tag_type == PTT_SRV_NAME) { char sName[255]=""; char path[64]=""; char cmd[320]=""; memset(sName, 0, p_in->tags[0]->tag_len+1); strncpy(sName, p_in->tags[0]->tag_data, p_in->tags[0]->tag_len); #ifdef BRCM_CMS_BUILD extern char servicename[BUFLEN_264]; /* service name from the connection, defined in options.c */ cmsLog_debug("servicename=%s", sName); strncpy(servicename, sName, sizeof(servicename)); #else //printf("PPPoE Service Name: %s\n", sName); sprintf(path, "%s/%s/%s", "/proc/var/fyi/wan", session_path, "servicename"); sprintf(cmd, "echo %s > %s", sName, path); system(cmd); #endif } memcpy(&ses->remote, &p_in->addr, sizeof(struct sockaddr_ll)); memcpy( &ses->curr_pkt.addr, &ses->remote , sizeof(struct sockaddr_ll)); ses->curr_pkt.hdr->code = PADR_CODE; /* The HOST_UNIQ has been verified already... there's no "if" about this */ /* if(ses->filt->htag) */ copy_tag(&ses->curr_pkt,get_tag(p_in->hdr,PTT_HOST_UNIQ)); if (ses->filt->ntag) { ses->curr_pkt.tags[TAG_AC_NAME]=NULL; } // copy_tag(&ses->curr_pkt,get_tag(p_in->hdr,PTT_AC_NAME)); #if 1 //brcm /* Our service name has been verified against the service name tags offered by * the server in the call to verify_packet(). We can just use it. */ copy_tag(&ses->curr_pkt, ses->filt->stag); #else if(ses->filt->stag) { ses->curr_pkt.tags[TAG_SRV_NAME]=NULL; } copy_tag(&ses->curr_pkt,get_tag(p_in->hdr,PTT_SRV_NAME)); #endif copy_tag(&ses->curr_pkt,get_tag(p_in->hdr,PTT_AC_COOKIE)); copy_tag(&ses->curr_pkt,get_tag(p_in->hdr,PTT_RELAY_SID)); ses->state = PADS_CODE; create_msg(BCM_PPPOE_CLIENT_STATE_PADS, MDMVS_ERROR_NONE); syslog(LOG_CRIT,"PPP server detected.\n"); ses->retransmits = 0; send_disc(ses, &ses->curr_pkt); (*p_out) = &ses->curr_pkt; if (ses->np) return 1; return 0; }
static int myRead(char *outBuf, int inLen) { int readLen = 0; static int xmlCfgLen = 0; static int offset = 0; static CmsMsgHeader *responseMsg=NULL; CmsMsgHeader requestMsg = EMPTY_MSG_HEADER; char *cfgStart; CmsRet ret; if (responseMsg == NULL) { cmsLog_debug("first time, get config file from smd"); /* * This is the first time that we were called. * Send a message to smd to request a copy of the config file. */ requestMsg.src = EID_TFTP; requestMsg.dst = EID_SMD; requestMsg.type = CMS_MSG_GET_CONFIG_FILE; requestMsg.flags_request = 1; if ((ret = cmsMsg_send(msgHandle, &requestMsg)) != CMSRET_SUCCESS) { cmsLog_error("could not send GET_CONFIG_FILE msg to smd."); return -1; } if ((ret = cmsMsg_receive(msgHandle, &responseMsg)) != CMSRET_SUCCESS) { cmsLog_error("could not receive GET_CONFIG_FILE msg from smd."); CMSMEM_FREE_BUF_AND_NULL_PTR(responseMsg); return -1; } xmlCfgLen = (int) responseMsg->dataLength; cmsLog_debug("got config buffer len=%u", xmlCfgLen); } /* config data starts immediately after the header */ cfgStart = (char *) (responseMsg + 1); if (xmlCfgLen <= inLen) readLen = xmlCfgLen; else readLen = inLen; memcpy(outBuf, (cfgStart + offset), readLen); xmlCfgLen -= readLen; offset += readLen; glbUploadSize += readLen; if (xmlCfgLen == 0) { /* done copying all the config data out, free the message */ CMSMEM_FREE_BUF_AND_NULL_PTR(responseMsg); offset = 0; cmsLog_debug("send out entire config buf, free msg"); } return readLen; }
CmsRet cmsEid_getBitMaskFromStringNames(const char *buf, UINT16 *bitMask) { const char *start; const char *end; const CmsEntityInfo *info; CmsRet ret = CMSRET_SUCCESS; if (bitMask == NULL) { return CMSRET_INVALID_ARGUMENTS; } *bitMask = 0; if ((buf == NULL) || (strlen(buf) == 0)) { /* null or empty string means no bits are set */ return CMSRET_SUCCESS; } start = buf; end = strchr(start, ','); while (end != NULL) { char name[BUFLEN_256]; /* this should be long enough to hold string names */ cmsAst_assert((end - start + 1) < (SINT32) sizeof(name)); snprintf(name, end - start + 1, "%s", start); info = cmsEid_getEntityInfoByStringName(name); if (info == NULL) { cmsLog_debug("ignoring name %s", name); ret = CMSRET_SUCCESS_UNRECOGNIZED_DATA_IGNORED; } else { (*bitMask) |= info->accessBit; } start = end+1; while ((*start == ' ') && (*start != 0)) { start++; } if (*start != 0) { end = strchr(start, ','); } else { end = NULL; } } /* there is one more name at the end (with no trailing comma) */ info = cmsEid_getEntityInfoByStringName(start); if (info == NULL) { cmsLog_debug("ignoring name %s", start); ret = CMSRET_SUCCESS_UNRECOGNIZED_DATA_IGNORED; } else { (*bitMask) |= info->accessBit; } return ret; }
CmsRet cmsUtl_parseDNS(const char *inDsnServers, char *outDnsPrimary, char *outDnsSecondary) { CmsRet ret = CMSRET_SUCCESS; char *tmpBuf; char *separator; UINT32 len; if (inDsnServers == NULL) { return CMSRET_INVALID_ARGUMENTS; } cmsLog_debug("entered: DDNSservers=>%s<=", inDsnServers); if (outDnsPrimary) { strcpy(outDnsPrimary, "0.0.0.0"); } if (outDnsSecondary) { strcpy(outDnsSecondary, "0.0.0.0"); } len = strlen(inDsnServers); if ((tmpBuf = cmsMem_alloc(len+1, 0)) == NULL) { cmsLog_error("alloc of %d bytes failed", len); ret = CMSRET_INTERNAL_ERROR; } else { sprintf(tmpBuf, "%s", inDsnServers); separator = strstr(tmpBuf, ","); if (separator != NULL) { /* break the string into two strings */ *separator = 0; separator++; while ((isspace(*separator)) && (*separator != 0)) { /* skip white space after comma */ separator++; } if (outDnsSecondary != NULL) { if (cmsUtl_isValidIpv4Address(separator)) { strcpy(outDnsSecondary, separator); } cmsLog_debug("dnsSecondary=%s", outDnsSecondary); } } if (outDnsPrimary != NULL) { if (cmsUtl_isValidIpv4Address(tmpBuf)) { strcpy(outDnsPrimary, tmpBuf); } cmsLog_debug("dnsPrimary=%s", outDnsPrimary); } cmsMem_free(tmpBuf); } return ret; }
void do_fwUpdate(void) { int byteRd = 0; char *curPtr = NULL; unsigned int totalAllocatedSize = 0; char *buffer; int max; fd_set rfds; struct timeval tv; UBOOL8 isConfigFile; /* reset all of our globals before starting another download */ imageFormat = CMS_IMAGE_FORMAT_INVALID; if (imagePtr) free(imagePtr); imagePtr = NULL; uploadSize = 0; if (dataconn()) return; alarm(0); if ((buffer = malloc(xfer_bufsize)) == NULL) { displayMessage(UPLOAD_FAIL_NO_MEM); return; } max = (sock > fileno(stdin) ? sock : fileno(stdin)) + 1; for (;;) { FD_ZERO(&rfds); FD_SET(sock, &rfds); FD_SET(fileno(stdin), &rfds); tv.tv_sec = data_timeout; tv.tv_usec = 0; if (!select(max, &rfds, NULL, NULL, &tv)) { close(sock); control_printf(SL_FAILURE, "426 Kicked due to data transmission timeout."); if (imagePtr) free(imagePtr); free(buffer); displayMessage(UPLOAD_FAIL_FTP); return; // exit ? } if (!((byteRd = recv(sock, buffer, xfer_bufsize, 0)))) break; if (curPtr == NULL) { // Also look in tftpd.c, which does about the same thing isConfigFile = cmsImg_isConfigFileLikely(buffer); cmsLog_debug("isConfigFile = %d", isConfigFile); if (isConfigFile) { totalAllocatedSize = cmsImg_getConfigFlashSize(); } else { totalAllocatedSize = cmsImg_getImageFlashSize() + cmsImg_getBroadcomImageTagSize(); // let smd know that we are about to start a big download cmsImg_sendLoadStartingMsg(msgHandle, connIfName); } if ((curPtr = (char *) malloc(totalAllocatedSize)) == NULL) { cmsLog_error("Not enough memory (%d bytes needed)", totalAllocatedSize); free(buffer); cmsImg_sendLoadDoneMsg(msgHandle); return; } printf("%d bytes allocated for image\n", totalAllocatedSize); imagePtr = curPtr; } if (uploadSize + byteRd < totalAllocatedSize) { memcpy(curPtr, buffer, byteRd); curPtr += byteRd; uploadSize += byteRd; } else { printf("Image could not fit into %d byte buffer.\n", totalAllocatedSize); free(buffer); free(imagePtr); imagePtr = NULL; cmsImg_sendLoadDoneMsg(msgHandle); return; } } // end for loop to read in complete image free(buffer); /* * Now we have the entire image. Validate it. */ if ((imageFormat = cmsImg_validateImage(imagePtr, uploadSize, msgHandle)) == CMS_IMAGE_FORMAT_INVALID) { displayMessage(UPLOAD_FAIL_ILLEGAL_IMAGE); free(imagePtr); imagePtr = NULL; cmsImg_sendLoadDoneMsg(msgHandle); } else { printf("Image validated, size=%u format=%d, waiting for quit before flashing.\n", uploadSize, imageFormat); displayMessage(UPLOAD_OK); // flash image will be done when user types bye or OK } close(sock); // this tells the ftp client that the transfer is complete alarm(control_timeout); }