📄 utils.c
字号:
if (len > 32) printer(arg, "%.32B ...", p); else printer(arg, "%.*B", len, p);}/* * init_pr_log, end_pr_log - initialize and finish use of pr_log. */static char line[256]; /* line to be logged accumulated here */static char *linep; /* current pointer within line */static int llevel; /* level for logging */voidinit_pr_log(prefix, level) char *prefix; int level;{ linep = line; if (prefix != NULL) { strlcpy(line, prefix, sizeof(line)); linep = line + strlen(line); } llevel = level;}voidend_pr_log(){ if (linep != line) { *linep = 0; log_write(llevel, line); }}/* * pr_log - printer routine for outputting to syslog */voidpr_log __V((void *arg, char *fmt, ...)){ int l, n; va_list pvar; char *p, *eol; 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); p = buf; eol = strchr(buf, '\n'); if (linep != line) { l = (eol == NULL)? n: eol - buf; if (linep + l < line + sizeof(line)) { if (l > 0) { memcpy(linep, buf, l); linep += l; } if (eol == NULL) return; p = eol + 1; eol = strchr(p, '\n'); } *linep = 0; log_write(llevel, line); linep = line; } while (eol != NULL) { *eol = 0; log_write(llevel, p); p = eol + 1; eol = strchr(p, '\n'); } /* assumes sizeof(buf) <= sizeof(line) */ l = buf + n - p; if (l > 0) { memcpy(line, p, n); linep = line + l; }}/* * 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[1024]; n = vslprintf(buf, sizeof(buf), fmt, args); log_write(level, buf);}static voidlog_write(level, buf) int level; char *buf;{ syslog(level, "%s", buf); if (log_to_fd >= 0 && (level != LOG_DEBUG || debug)) { int n = strlen(buf); if (n > 0 && buf[n-1] == '\n') --n; if (write(log_to_fd, buf, n) != n || write(log_to_fd, "\n", 1) != 1) log_to_fd = -1; }}/* * fatal - log an error message and die horribly. */voidfatal __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. */voiderror __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. */voidwarn __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. */voidnotice __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. */voidinfo __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. */voiddbglog __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 + -