📄 utils.c
字号:
for (i = 0; (protp = protocols[i]) != NULL; ++i) if (proto == (protp->protocol & ~0x8000)) break; if (protp != 0 && protp->data_name != 0) { printer(arg, "[%s data]", protp->data_name); if (len > 8) printer(arg, "%.8B ...", p); else printer(arg, "%.*B", len, p); len = 0; } else printer(arg, "[proto=0x%x]", proto); } } if (len > 32) printer(arg, "%.32B ...", p); else printer(arg, "%.*B", len, p);}static voidpr_log __V((void *arg, char *fmt, ...)){ int n; va_list pvar; char buf[256];#if defined(__STDC__) va_start(pvar, fmt);#else void *arg; char *fmt; va_start(pvar); arg = va_arg(pvar, void *); fmt = va_arg(pvar, char *);#endif n = vslprintf(buf, sizeof(buf), fmt, pvar); va_end(pvar); if (linep + n + 1 > line + sizeof(line)) { linep = line; } strlcpy(linep, buf, line + sizeof(line) - linep); linep += n;}/* * print_string - print a readable representation of a string using * printer. */voidprint_string(p, len, printer, arg) char *p; int len; void (*printer) __P((void *, char *, ...)); void *arg;{ int c; printer(arg, "\""); for (; len > 0; --len) { c = *p++; if (' ' <= c && c <= '~') { if (c == '\\' || c == '"') printer(arg, "\\"); printer(arg, "%c", c); } else { switch (c) { case '\n': printer(arg, "\\n"); break; case '\r': printer(arg, "\\r"); break; case '\t': printer(arg, "\\t"); break; default: printer(arg, "\\%.3o", c); } } } printer(arg, "\"");}/* * logit - does the hard work for fatal et al. */static voidlogit(level, fmt, args) int level; char *fmt; va_list args;{ int n; char buf[256]; n = vslprintf(buf, sizeof(buf), fmt, args);/* if (log_to_fd >= 0 && (level != LOG_DEBUG || debug)) { */ if (log_to_fd >= 0 && (debug)) { if (buf[n-1] != '\n') buf[n++] = '\n'; if (write(log_to_fd, buf, n) != n) log_to_fd = -1; }}/* * fatal - log an error message and die horribly. */voidpppd_fatal __V((char *fmt, ...)){ va_list pvar;#if defined(__STDC__) va_start(pvar, fmt);#else char *fmt; va_start(pvar); fmt = va_arg(pvar, char *);#endif logit(LOG_ERR, fmt, pvar); va_end(pvar); die(1); /* as promised */}/* * error - log an error message. */voidpppd_error __V((char *fmt, ...)){ va_list pvar;#if defined(__STDC__) va_start(pvar, fmt);#else char *fmt; va_start(pvar); fmt = va_arg(pvar, char *);#endif logit(LOG_ERR, fmt, pvar); va_end(pvar);}/* * warn - log a warning message. */voidpppd_warn __V((char *fmt, ...)){ va_list pvar;#if defined(__STDC__) va_start(pvar, fmt);#else char *fmt; va_start(pvar); fmt = va_arg(pvar, char *);#endif logit(LOG_WARNING, fmt, pvar); va_end(pvar);}/* * notice - log a notice-level message. */voidpppd_notice __V((char *fmt, ...)){ va_list pvar;#if defined(__STDC__) va_start(pvar, fmt);#else char *fmt; va_start(pvar); fmt = va_arg(pvar, char *);#endif logit(LOG_NOTICE, fmt, pvar); va_end(pvar);}/* * info - log an informational message. */voidpppd_info __V((char *fmt, ...)){ va_list pvar;#if defined(__STDC__) va_start(pvar, fmt);#else char *fmt; va_start(pvar); fmt = va_arg(pvar, char *);#endif logit(LOG_INFO, fmt, pvar); va_end(pvar);}/* * dbglog - log a debug message. */voidpppd_dbglog __V((char *fmt, ...)){ va_list pvar;#if defined(__STDC__) va_start(pvar, fmt);#else char *fmt; va_start(pvar); fmt = va_arg(pvar, char *);#endif logit(LOG_DEBUG, fmt, pvar); va_end(pvar);}/* Procedures for locking the serial device using a lock file. */#ifndef LOCK_DIR#ifdef _linux_#define LOCK_DIR "/var/lock"#else#ifdef SVR4#define LOCK_DIR "/var/spool/locks"#else#define LOCK_DIR "/var/spool/lock"#endif#endif#endif /* LOCK_DIR */static char lock_file[MAXPATHLEN];/* * lock - create a lock file for the named device */intlock(dev) char *dev;{#ifdef LOCKLIB int result; result = mklock (dev, (void *) 0); if (result == 0) { strlcpy(lock_file, sizeof(lock_file), dev); return 0; } if (result > 0) notice("Device %s is locked by pid %d", dev, result); else error("Can't create lock file %s", lock_file); return -1;#else /* LOCKLIB */ char lock_buffer[12]; int fd, pid, n;#ifdef SVR4 struct stat sbuf; if (stat(dev, &sbuf) < 0) { error("Can't get device number for %s: %m", dev); return -1; } if ((sbuf.st_mode & S_IFMT) != S_IFCHR) { error("Can't lock %s: not a character device", dev); return -1; } slprintf(lock_file, sizeof(lock_file), "%s/LK.%03d.%03d.%03d", LOCK_DIR, major(sbuf.st_dev), major(sbuf.st_rdev), minor(sbuf.st_rdev));#else char *p; if ((p = strrchr(dev, '/')) != NULL) dev = p + 1; slprintf(lock_file, sizeof(lock_file), "%s/LCK..%s", LOCK_DIR, dev);#endif while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) { if (errno != EEXIST) { error("Can't create lock file %s: %m", lock_file); break; } /* Read the lock file to find out who has the device locked. */ fd = open(lock_file, O_RDONLY, 0); if (fd < 0) { if (errno == ENOENT) /* This is just a timing problem. */ continue; error("Can't open existing lock file %s: %m", lock_file); break; }#ifndef LOCK_BINARY n = read(fd, lock_buffer, 11);#else n = read(fd, &pid, sizeof(pid));#endif /* LOCK_BINARY */ close(fd); fd = -1; if (n <= 0) { error("Can't read pid from lock file %s", lock_file); break; } /* See if the process still exists. */#ifndef LOCK_BINARY lock_buffer[n] = 0; pid = atoi(lock_buffer);#endif /* LOCK_BINARY */ if (pid == getpid()) return 1; /* somebody else locked it for us */ if (pid == 0 || (kill(pid, 0) == -1 && errno == ESRCH)) { if (unlink (lock_file) == 0) { notice("Removed stale lock on %s (pid %d)", dev, pid); continue; } warn("Couldn't remove stale lock on %s", dev); } else notice("Device %s is locked by pid %d", dev, pid); break; } if (fd < 0) { lock_file[0] = 0; return -1; } pid = getpid();#ifndef LOCK_BINARY slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid); write (fd, lock_buffer, 11);#else write(fd, &pid, sizeof (pid));#endif close(fd); return 0;#endif}/* * relock - called to update our lockfile when we are about to detach, * thus changing our pid (we fork, the child carries on, and the parent dies). * Note that this is called by the parent, with pid equal to the pid * of the child. This avoids a potential race which would exist if * we had the child rewrite the lockfile (the parent might die first, * and another process could think the lock was stale if it checked * between when the parent died and the child rewrote the lockfile). */intrelock(pid) int pid;{#ifdef LOCKLIB /* XXX is there a way to do this? */ return -1;#else /* LOCKLIB */ int fd; char lock_buffer[12]; if (lock_file[0] == 0) return -1; fd = open(lock_file, O_WRONLY, 0); if (fd < 0) { error("Couldn't reopen lock file %s: %m", lock_file); lock_file[0] = 0; return -1; }#ifndef LOCK_BINARY slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid); write (fd, lock_buffer, 11);#else write(fd, &pid, sizeof(pid));#endif /* LOCK_BINARY */ close(fd); return 0;#endif /* LOCKLIB */}/* * unlock - remove our lockfile */voidunlock(){ if (lock_file[0]) {#ifdef LOCKLIB (void) rmlock(lock_file, (void *) 0);#else unlink(lock_file);#endif lock_file[0] = 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -