--- embedaddon/mpd/src/util.c 2012/02/21 23:32:47 1.1.1.1 +++ embedaddon/mpd/src/util.c 2013/07/22 08:44:29 1.1.1.2 @@ -10,7 +10,9 @@ #include "ppp.h" #include "util.h" #include +#include +#include #include #include #include @@ -32,6 +34,7 @@ * INTERNAL VARIABLES */ +#ifndef USE_NG_PRED1 static const u_int16_t Crc16Table[256] = { /* 00 */ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, /* 08 */ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, @@ -66,6 +69,7 @@ static const u_int16_t Crc16Table[256] = { /* f0 */ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, /* f8 */ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 }; +#endif static FILE *lockFp = NULL; @@ -73,9 +77,6 @@ static const u_int16_t Crc16Table[256] = { * INTERNAL FUNCTIONS */ - static int UuLock(const char *devname); - static int UuUnlock(const char *devname); - static void Escape(char *line); static char *ReadLine(FILE *fp, int *lineNum, char *result, int resultsize); @@ -130,7 +131,7 @@ ExecCmd(int log, const char *label, const char *fmt, . /* Hide any stdout output of command */ - snprintf(cmdn + strlen(cmdn), sizeof(cmdn) - strlen(cmdn), " >/dev/null 2>&1"); + snprintf(cmdn + strlen(cmdn), sizeof(cmdn) - strlen(cmdn), " >%s 2>&1", _PATH_DEVNULL); /* Do command */ @@ -202,9 +203,9 @@ ExecCmdNosh(int log, const char *label, const char *fm (void)sigaction(SIGQUIT, &quitact, NULL); (void)sigprocmask(SIG_SETMASK, &oldsigblock, NULL); close(1); - open("/dev/null", O_WRONLY); + open(_PATH_DEVNULL, O_WRONLY); close(2); - open("/dev/null", O_WRONLY); + open(_PATH_DEVNULL, O_WRONLY); execv(argv[0], argv); exit(127); default: /* parent */ @@ -666,6 +667,7 @@ ReadLine(FILE *fp, int *lineNum, char *result, int res return(result); } +#ifdef PHYSTYPE_MODEM /* * OpenSerialDevice() * @@ -737,12 +739,12 @@ ExclusiveOpenDevice(const char *label, const char *pat /* Lock device UUCP style, if it resides in /dev */ - if (!strncmp(pathname, "/dev/", 5)) + if (!strncmp(pathname, _PATH_DEV, 5)) { + int res; ttyname = pathname + 5; - if (UuLock(ttyname) < 0) - { - Log(LG_ERR, ("[%s] can't lock device %s", label, ttyname)); + if ((res = uu_lock(ttyname)) != UU_LOCK_OK) { + Log(LG_ERR, ("[%s] uu_lock(%s): %s", label, ttyname, uu_lockerr(res))); return(-1); } locked = TRUE; @@ -757,7 +759,7 @@ ExclusiveOpenDevice(const char *label, const char *pat { Perror("[%s] can't open %s", label, pathname); if (locked) - UuUnlock(ttyname); + uu_unlock(ttyname); return(-1); } @@ -768,7 +770,7 @@ ExclusiveOpenDevice(const char *label, const char *pat Log(LG_ERR, ("[%s] can't open %s after %d secs", label, pathname, MAX_OPEN_DELAY)); if (locked) - UuUnlock(ttyname); + uu_unlock(ttyname); return(-1); } (void) fcntl(fd, F_SETFD, 1); @@ -810,93 +812,17 @@ ExclusiveCloseDevice(const char *label, int fd, const /* Remove lock */ - if (!strncmp(pathname, "/dev/", 5)) + if (!strncmp(pathname, _PATH_DEV, 5)) { ttyname = pathname + 5; - if (UuUnlock(ttyname) < 0) + if (uu_unlock(ttyname) < 0) Perror("[%s] can't unlock %s", label, ttyname); } } +#endif /* PHYSTYPE_MODEM */ -/* - * UuLock() - * - * Try to atomically create lockfile. Returns negative if failed. - */ -static int -UuLock(const char *ttyname) -{ - int fd, pid; - char tbuf[sizeof(PATH_LOCKFILENAME) + MAX_FILENAME]; - char pid_buf[64]; - - snprintf(tbuf, sizeof(tbuf), PATH_LOCKFILENAME, ttyname); - if ((fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL, 0664)) < 0) - { - - /* File is already locked; Check to see if the process - * holding the lock still exists */ - - if ((fd = open(tbuf, O_RDWR, 0)) < 0) - { - Perror("%s: open(%s)", __FUNCTION__, tbuf); - return(-1); - } - - if (read(fd, pid_buf, sizeof(pid_buf)) <= 0) - { - (void)close(fd); - Perror("%s: read", __FUNCTION__); - return(-1); - } - - pid = atoi(pid_buf); - - if (kill(pid, 0) == 0 || errno != ESRCH) - { - (void)close(fd); /* process is still running */ - return(-1); - } - - /* The process that locked the file isn't running, so we'll lock it */ - - if (lseek(fd, (off_t) 0, L_SET) < 0) - { - (void)close(fd); - Perror("%s: lseek", __FUNCTION__); - return(-1); - } - } - -/* Finish the locking process */ - - sprintf(pid_buf, "%10u\n", (int) gPid); - if (write(fd, pid_buf, strlen(pid_buf)) != strlen(pid_buf)) - { - (void)close(fd); - (void)unlink(tbuf); - Perror("%s: write", __FUNCTION__); - return(-1); - } - (void)close(fd); - return(0); -} - /* - * UuUnlock() - */ - -static int -UuUnlock(const char *ttyname) -{ - char tbuf[sizeof(PATH_LOCKFILENAME) + MAX_FILENAME]; - - (void) sprintf(tbuf, PATH_LOCKFILENAME, ttyname); - return(unlink(tbuf)); -} - -/* * GenerateMagic() * * Generate random number which will be used as magic number. @@ -1186,12 +1112,19 @@ Bin2Hex(const unsigned char *bin, size_t len) size_t i, j; char *buf; - buf = Malloc(MB_UTIL, len * 2 + 1); - for (i = j = 0; i < len; i++) { - buf[j++] = hexconvtab[bin[i] >> 4]; - buf[j++] = hexconvtab[bin[i] & 15]; + if (len > 0) { + buf = Malloc(MB_UTIL, len * 2 + 1); + for (i = j = 0; i < len; i++) { + buf[j++] = hexconvtab[bin[i] >> 4]; + buf[j++] = hexconvtab[bin[i] & 15]; + } + buf[j] = 0; + } else { + buf = Malloc(MB_UTIL, 3); + buf[0] = '0'; + buf[1] = '0'; + buf[2] = 0; } - buf[j] = 0; return buf; } @@ -1228,6 +1161,7 @@ HexVal(char c) } } +#ifndef USE_NG_PRED1 /* * Crc16() * @@ -1242,6 +1176,7 @@ Crc16(u_short crc, u_char *cp, int len) crc = (crc >> 8) ^ Crc16Table[(crc ^ *cp++) & 0xff]; return(crc); } +#endif /* * GetAnyIpAddress() @@ -1434,8 +1369,10 @@ GetEther(struct u_addr *addr, struct sockaddr_dl *hwad ifreq.ifr_addr = ifr->ifr_addr; /* Check that the interface is up, and not point-to-point or loopback */ - if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0) + if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0) { + Log(LG_IFACE2, ("ioctl(SIOCGIFFLAGS, %s): %d", ifr->ifr_name, errno)); continue; + } if ((ifreq.ifr_flags & (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP)) != (IFF_UP|IFF_BROADCAST)) @@ -1514,7 +1451,7 @@ GetPeerEther(struct u_addr *addr, struct sockaddr_dl * mib[5] = 0; #endif if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { - Log(LG_ERR, ("route-sysctl-estimate")); + Perror("route-sysctl-estimate"); return (0); } if (needed == 0) /* empty table */