extern int logger_main(int argc, char **argv) { int pri = LOG_USER | LOG_NOTICE; int option = 0; int c, i, len, opt; char *message=NULL, buf[1024], name[128]; /* Fill out the name string early (may be overwritten later) */ my_getpwuid(name, geteuid()); /* Parse any options */ while ((opt = getopt(argc, argv, "p:st:")) > 0) { switch (opt) { case 's': option |= LOG_PERROR; break; case 'p': pri = pencode(optarg); break; case 't': strncpy(name, optarg, sizeof(name)); break; default: show_usage(); } } openlog(name, option, (pri | LOG_FACMASK)); if (optind == argc) { do { /* read from stdin */ i = 0; while ((c = getc(stdin)) != EOF && c != '\n' && i < (sizeof(buf)-1)) { buf[i++] = c; } if (i > 0) { buf[i++] = '\0'; syslog(pri, "%s", buf); } } while (c != EOF); } else { len = 1; /* for the '\0' */ message=xcalloc(1, 1); for (i = optind; i < argc; i++) { len += strlen(argv[i]); len += 1; /* for the space between the args */ message = xrealloc(message, len); strcat(message, argv[i]); strcat(message, " "); } message[strlen(message)-1] = '\0'; syslog(pri, "%s", message); } closelog(); return EXIT_SUCCESS; }
int logger_main(int argc UNUSED_PARAM, char **argv) { char *str_p, *str_t; int opt; int i = 0; FILE *f = NULL; /* Fill out the name string early (may be overwritten later) */ str_t = uid2uname_utoa(geteuid()); /* Parse any options */ opt = getopt32(argv, "p:st:c", &str_p, &str_t); if (opt & 0x2) /* -s */ i |= LOG_PERROR; if (opt & 0x8) { /* -c */ f = fopen_for_write(DEV_CONSOLE); if (!f) bb_error_msg("can't open console: %d %s\n", errno, strerror(errno)); } //if (opt & 0x4) /* -t */ openlog(str_t, i, 0); i = LOG_USER | LOG_WARNING; if (opt & 0x1) /* -p */ i = pencode(str_p); argv += optind; if (!argv[0]) { while (fgets(strbuf, COMMON_BUFSIZE, stdin)) { if (strbuf[0] && NOT_LONE_CHAR(strbuf, '\n') ) { /* Neither "" nor "\n" */ syslog(i, "%s", strbuf); if (f) fprintf(f, "%s", strbuf); } } } else { char *message = NULL; int len = 0; int pos = 0; do { len += strlen(*argv) + 1; message = xrealloc(message, len + 1); sprintf(message + pos, " %s", *argv), pos = len; } while (*++argv); syslog(i, "%s", message + 1); /* skip leading " " */ if (f) fprintf(f, "%s", message + 1); } closelog(); if (f) fclose(f); return EXIT_SUCCESS; }
int logger_main(int argc, char **argv) { char *str_p, *str_t; int i = 0; char name[80]; /* Fill out the name string early (may be overwritten later) */ bb_getpwuid(name, sizeof(name), geteuid()); str_t = name; /* Parse any options */ getopt32(argv, "p:st:", &str_p, &str_t); if (option_mask32 & 0x2) /* -s */ i |= LOG_PERROR; //if (option_mask32 & 0x4) /* -t */ openlog(str_t, i, 0); i = LOG_USER | LOG_NOTICE; if (option_mask32 & 0x1) /* -p */ i = pencode(str_p); argc -= optind; argv += optind; if (!argc) { #define strbuf bb_common_bufsiz1 while (fgets(strbuf, COMMON_BUFSIZE, stdin)) { if (strbuf[0] && NOT_LONE_CHAR(strbuf, '\n') ) { /* Neither "" nor "\n" */ syslog(i, "%s", strbuf); } } } else { char *message = NULL; int len = 0; int pos = 0; do { len += strlen(*argv) + 1; message = xrealloc(message, len + 1); sprintf(message + pos, " %s", *argv), pos = len; } while (*++argv); syslog(i, "%s", message + 1); /* skip leading " " */ } closelog(); return EXIT_SUCCESS; }
/* authenticate: create the oauth authentication header for a http request */ std::string tw::Reader::authenticate(const std::string &api, Authenticator::param_vec hparams) { std::string auth_str; struct tw::Authenticator::oauth_data data; /* generate an oauth singature and receive data */ tw_auth->siggen("GET", api, hparams, {}); data = tw_auth->auth_data(); /* add all components to string */ auth_str += "OAuth "; auth_str += "oauth_consumer_key=\"" + pencode(data.c_key) + "\", "; auth_str += "oauth_nonce=\"" + pencode(data.nonce) + "\", "; auth_str += "oauth_signature=\"" + pencode(data.sig) + "\", "; auth_str += "oauth_signature_method=\"" + pencode(data.sig_method) + "\", "; auth_str += "oauth_timestamp=\"" + pencode(data.timestamp) + "\", "; auth_str += "oauth_token=\"" + pencode(data.token) + "\", "; auth_str += "oauth_version=\"" + pencode(data.version) + "\""; return auth_str; }
/* * logger -- read and log utility * * Reads from an input and arranges to write the result on the system * log. */ int main(int argc, char **argv) { struct logger_ctl ctl = { .fd = -1, .pid = 0, .pri = LOG_USER | LOG_NOTICE, .prio_prefix = 0, .tag = NULL, .unix_socket = NULL, .unix_socket_errors = 0, .server = NULL, .port = NULL, .hdr = NULL, .msgid = NULL, .socket_type = ALL_TYPES, .max_message_size = 1024, .rfc5424_time = 1, .rfc5424_tq = 1, .rfc5424_host = 1, .skip_empty_lines = 0 }; int ch; int stdout_reopened = 0; int unix_socket_errors_mode = AF_UNIX_ERRORS_AUTO; #ifdef HAVE_LIBSYSTEMD FILE *jfd = NULL; #endif static const struct option longopts[] = { { "id", optional_argument, 0, OPT_ID }, { "stderr", no_argument, 0, 's' }, { "file", required_argument, 0, 'f' }, { "no-act", no_argument, 0, OPT_NOACT, }, { "priority", required_argument, 0, 'p' }, { "tag", required_argument, 0, 't' }, { "socket", required_argument, 0, 'u' }, { "socket-errors", required_argument, 0, OPT_SOCKET_ERRORS }, { "udp", no_argument, 0, 'd' }, { "tcp", no_argument, 0, 'T' }, { "server", required_argument, 0, 'n' }, { "port", required_argument, 0, 'P' }, { "version", no_argument, 0, 'V' }, { "help", no_argument, 0, 'h' }, { "octet-count", no_argument, 0, OPT_OCTET_COUNT }, { "prio-prefix", no_argument, 0, OPT_PRIO_PREFIX }, { "rfc3164", no_argument, 0, OPT_RFC3164 }, { "rfc5424", optional_argument, 0, OPT_RFC5424 }, { "size", required_argument, 0, 'S' }, { "msgid", required_argument, 0, OPT_MSGID }, { "skip-empty", no_argument, 0, 'e' }, #ifdef HAVE_LIBSYSTEMD { "journald", optional_argument, 0, OPT_JOURNALD }, #endif { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((ch = getopt_long(argc, argv, "ef:ip:S:st:u:dTn:P:Vh", longopts, NULL)) != -1) { switch (ch) { case 'f': /* file to log */ if (freopen(optarg, "r", stdin) == NULL) err(EXIT_FAILURE, _("file %s"), optarg); stdout_reopened = 1; break; case 'e': ctl.skip_empty_lines = 1; break; case 'i': /* log process id also */ ctl.pid = logger_getpid(); break; case OPT_ID: if (optarg) { const char *p = optarg; if (*p == '=') p++; ctl.pid = strtoul_or_err(optarg, _("failed to parse id")); } else ctl.pid = logger_getpid(); break; case 'p': /* priority */ ctl.pri = pencode(optarg); break; case 's': /* log to standard error */ ctl.stderr_printout = 1; break; case 't': /* tag */ ctl.tag = optarg; break; case 'u': /* unix socket */ ctl.unix_socket = optarg; break; case 'S': /* max message size */ ctl.max_message_size = strtosize_or_err(optarg, _("failed to parse message size")); break; case 'd': ctl.socket_type = TYPE_UDP; break; case 'T': ctl.socket_type = TYPE_TCP; break; case 'n': ctl.server = optarg; break; case 'P': ctl.port = optarg; break; case 'V': printf(UTIL_LINUX_VERSION); exit(EXIT_SUCCESS); case 'h': usage(stdout); case OPT_OCTET_COUNT: ctl.octet_count = 1; break; case OPT_PRIO_PREFIX: ctl.prio_prefix = 1; break; case OPT_RFC3164: ctl.syslogfp = syslog_rfc3164_header; break; case OPT_RFC5424: ctl.syslogfp = syslog_rfc5424_header; if (optarg) parse_rfc5424_flags(&ctl, optarg); break; case OPT_MSGID: if (strchr(optarg, ' ')) errx(EXIT_FAILURE, _("--msgid cannot contain space")); ctl.msgid = optarg; break; #ifdef HAVE_LIBSYSTEMD case OPT_JOURNALD: if (optarg) { jfd = fopen(optarg, "r"); if (!jfd) err(EXIT_FAILURE, _("cannot open %s"), optarg); } else jfd = stdin; break; #endif case OPT_SOCKET_ERRORS: unix_socket_errors_mode = parse_unix_socket_errors_flags(optarg); break; case OPT_NOACT: ctl.noact = 1; break; case '?': default: usage(stderr); } } argc -= optind; argv += optind; if (stdout_reopened && argc) warnx(_("--file <file> and <message> are mutually exclusive, message is ignored")); #ifdef HAVE_LIBSYSTEMD if (jfd) { int ret = journald_entry(&ctl, jfd); if (stdin != jfd) fclose(jfd); if (ret) errx(EXIT_FAILURE, _("journald entry could not be written")); return EXIT_SUCCESS; } #endif switch (unix_socket_errors_mode) { case AF_UNIX_ERRORS_OFF: ctl.unix_socket_errors = 0; break; case AF_UNIX_ERRORS_ON: ctl.unix_socket_errors = 1; break; case AF_UNIX_ERRORS_AUTO: ctl.unix_socket_errors = ctl.noact || ctl.stderr_printout; #ifdef HAVE_LIBSYSTEMD ctl.unix_socket_errors |= !!sd_booted(); #endif break; default: abort(); } logger_open(&ctl); if (0 < argc) logger_command_line(&ctl, argv); else /* Note. --file <arg> reopens stdin making the below * function to be used for file inputs. */ logger_stdin(&ctl); logger_close(&ctl); return EXIT_SUCCESS; }
/* * logger -- read and log utility * * Reads from an input and arranges to write the result on the system * log. */ int main(int argc, char **argv) { int ch, logflags, pri; char *tag, buf[1024]; char *usock = NULL; char *udpserver = NULL; char *udpport = NULL; int LogSock = -1; static const struct option longopts[] = { { "id", no_argument, 0, 'i' }, { "stderr", no_argument, 0, 's' }, { "file", required_argument, 0, 'f' }, { "priority", required_argument, 0, 'p' }, { "tag", required_argument, 0, 't' }, { "socket", required_argument, 0, 'u' }, { "udp", no_argument, 0, 'd' }, { "server", required_argument, 0, 'n' }, { "port", required_argument, 0, 'P' }, { "version", no_argument, 0, 'V' }, { "help", no_argument, 0, 'h' }, { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); tag = NULL; pri = LOG_NOTICE; logflags = 0; while ((ch = getopt_long(argc, argv, "f:ip:st:u:dn:P:Vh", longopts, NULL)) != -1) { switch((char)ch) { case 'f': /* file to log */ if (freopen(optarg, "r", stdin) == NULL) err(EXIT_FAILURE, _("file %s"), optarg); break; case 'i': /* log process id also */ logflags |= LOG_PID; break; case 'p': /* priority */ pri = pencode(optarg); break; case 's': /* log to standard error */ logflags |= LOG_PERROR; break; case 't': /* tag */ tag = optarg; break; case 'u': /* unix socket */ usock = optarg; break; case 'd': optd = 1; /* use datagrams */ break; case 'n': /* udp socket */ optd = 1; /* use datagrams because udp */ udpserver = optarg; break; case 'P': /* change udp port */ udpport = optarg; break; case 'V': printf(_("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING); exit(EXIT_SUCCESS); case 'h': usage(stdout); case '?': default: usage(stderr); } } argc -= optind; argv += optind; /* setup for logging */ if (!usock && !udpserver) openlog(tag ? tag : getlogin(), logflags, 0); else if (udpserver) LogSock = udpopenlog(udpserver,udpport); else LogSock = myopenlog(usock); /* log input line if appropriate */ if (argc > 0) { register char *p, *endp; size_t len; for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) { len = strlen(*argv); if (p + len > endp && p > buf) { if (!usock && !udpserver) syslog(pri, "%s", buf); else mysyslog(LogSock, logflags, pri, tag, buf); p = buf; } if (len > sizeof(buf) - 1) { if (!usock && !udpserver) syslog(pri, "%s", *argv++); else mysyslog(LogSock, logflags, pri, tag, *argv++); } else { if (p != buf) *p++ = ' '; memmove(p, *argv++, len); *(p += len) = '\0'; } } if (p != buf) { if (!usock && !udpserver) syslog(pri, "%s", buf); else mysyslog(LogSock, logflags, pri, tag, buf); } } else { while (fgets(buf, sizeof(buf), stdin) != NULL) { /* glibc is buggy and adds an additional newline, so we have to remove it here until glibc is fixed */ int len = strlen(buf); if (len > 0 && buf[len - 1] == '\n') buf[len - 1] = '\0'; if (!usock && !udpserver) syslog(pri, "%s", buf); else mysyslog(LogSock, logflags, pri, tag, buf); } } if (!usock && !udpserver) closelog(); else close(LogSock); return EXIT_SUCCESS; }
/* * logger -- read and log utility * * Reads from an input and arranges to write the result on the system * log. */ int main(int argc, char **argv) { int ch, logflags, pri, prio_prefix; char *tag, buf[1024]; char *usock = NULL; char *server = NULL; char *port = NULL; int LogSock = -1, socket_type = ALL_TYPES; #ifdef HAVE_LIBSYSTEMD FILE *jfd = NULL; #endif static const struct option longopts[] = { { "id", no_argument, 0, 'i' }, { "stderr", no_argument, 0, 's' }, { "file", required_argument, 0, 'f' }, { "priority", required_argument, 0, 'p' }, { "tag", required_argument, 0, 't' }, { "socket", required_argument, 0, 'u' }, { "udp", no_argument, 0, 'd' }, { "tcp", no_argument, 0, 'T' }, { "server", required_argument, 0, 'n' }, { "port", required_argument, 0, 'P' }, { "version", no_argument, 0, 'V' }, { "help", no_argument, 0, 'h' }, { "prio-prefix", no_argument, 0, OPT_PRIO_PREFIX }, #ifdef HAVE_LIBSYSTEMD { "journald", optional_argument, 0, OPT_JOURNALD }, #endif { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); tag = NULL; pri = LOG_NOTICE; logflags = 0; prio_prefix = 0; while ((ch = getopt_long(argc, argv, "f:ip:st:u:dTn:P:Vh", longopts, NULL)) != -1) { switch (ch) { case 'f': /* file to log */ if (freopen(optarg, "r", stdin) == NULL) err(EXIT_FAILURE, _("file %s"), optarg); break; case 'i': /* log process id also */ logflags |= LOG_PID; break; case 'p': /* priority */ pri = pencode(optarg); break; case 's': /* log to standard error */ logflags |= LOG_PERROR; break; case 't': /* tag */ tag = optarg; break; case 'u': /* unix socket */ usock = optarg; break; case 'd': socket_type = TYPE_UDP; break; case 'T': socket_type = TYPE_TCP; break; case 'n': server = optarg; break; case 'P': port = optarg; break; case 'V': printf(UTIL_LINUX_VERSION); exit(EXIT_SUCCESS); case 'h': usage(stdout); case OPT_PRIO_PREFIX: prio_prefix = 1; break; #ifdef HAVE_LIBSYSTEMD case OPT_JOURNALD: if (optarg) { jfd = fopen(optarg, "r"); if (!jfd) err(EXIT_FAILURE, _("cannot open %s"), optarg); } else jfd = stdin; break; #endif case '?': default: usage(stderr); } } argc -= optind; argv += optind; /* setup for logging */ #ifdef HAVE_LIBSYSTEMD if (jfd) { int ret = journald_entry(jfd); if (stdin != jfd) fclose(jfd); return ret ? EXIT_FAILURE : EXIT_SUCCESS; } #endif if (server) LogSock = inet_socket(server, port, socket_type); else if (usock) LogSock = unix_socket(usock, socket_type); else openlog(tag ? tag : getlogin(), logflags, 0); /* log input line if appropriate */ if (argc > 0) { register char *p, *endp; size_t len; for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) { len = strlen(*argv); if (p + len > endp && p > buf) { if (!usock && !server) syslog(pri, "%s", buf); else mysyslog(LogSock, logflags, pri, tag, buf); p = buf; } if (len > sizeof(buf) - 1) { if (!usock && !server) syslog(pri, "%s", *argv++); else mysyslog(LogSock, logflags, pri, tag, *argv++); } else { if (p != buf) *p++ = ' '; memmove(p, *argv++, len); *(p += len) = '\0'; } } if (p != buf) { if (!usock && !server) syslog(pri, "%s", buf); else mysyslog(LogSock, logflags, pri, tag, buf); } } else { char *msg; int default_priority = pri; while (fgets(buf, sizeof(buf), stdin) != NULL) { /* glibc is buggy and adds an additional newline, so we have to remove it here until glibc is fixed */ int len = strlen(buf); if (len > 0 && buf[len - 1] == '\n') buf[len - 1] = '\0'; msg = buf; pri = default_priority; if (prio_prefix && msg[0] == '<') msg = get_prio_prefix(msg, &pri); if (!usock && !server) syslog(pri, "%s", msg); else mysyslog(LogSock, logflags, pri, tag, msg); } } if (!usock && !server) closelog(); else close(LogSock); return EXIT_SUCCESS; }
int logger_main(int argc UNUSED_PARAM, char **argv) { char *str_p, *str_t; int opt; int i = 0; int fd = -1; setup_common_bufsiz(); /* Fill out the name string early (may be overwritten later) */ str_t = uid2uname_utoa(geteuid()); /* Parse any options */ opt = getopt32(argv, "p:st:c", &str_p, &str_t); if (opt & 0x2) /* -s */ i |= LOG_PERROR; //if (opt & 0x4) /* -t */ openlog(str_t, i, 0); if (opt & 0x8) { /* -c */ fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK); if (fd < 0) bb_error_msg("can't open console"); } i = LOG_USER | LOG_WARNING; if (opt & 0x1) /* -p */ i = pencode(str_p); argv += optind; if (!argv[0]) { while (fgets(strbuf, COMMON_BUFSIZE, stdin)) { if (strbuf[0] && NOT_LONE_CHAR(strbuf, '\n') ) { /* Neither "" nor "\n" */ syslog(i, "%s", strbuf); if (fd >= 0) { fdprintf(fd, "%s: %s%s", str_t, strbuf, strchr(strbuf, '\n') ? "" : "\n"); } } } } else { char *message = NULL; int len = 0; int pos = 0; do { len += strlen(*argv) + 1; message = xrealloc(message, len + 1); sprintf(message + pos, " %s", *argv), pos = len; } while (*++argv); syslog(i, "%s", message + 1); /* skip leading " " */ if (fd >= 0 && len) { fdprintf(fd, "%s:%s%s", str_t, message, message[len - 1] == '\n' ? "" : "\n"); } } closelog(); if (fd >= 0) close(fd); return EXIT_SUCCESS; }
/* * logger -- read and log utility * * Reads from an input and arranges to write the result on the system * log. */ int main(int argc, char **argv) { struct logger_ctl ctl = { .fd = -1, .pid = 0, .pri = LOG_NOTICE, .prio_prefix = 0, .tag = NULL, .unix_socket = NULL, .server = NULL, .port = NULL, .socket_type = ALL_TYPES, .rfc5424_time = 1, .rfc5424_tq = 1, .rfc5424_host = 1, }; int ch; int stdout_reopened = 0; #ifdef HAVE_LIBSYSTEMD FILE *jfd = NULL; #endif static const struct option longopts[] = { { "id", optional_argument, 0, 'i' }, { "stderr", no_argument, 0, 's' }, { "file", required_argument, 0, 'f' }, { "priority", required_argument, 0, 'p' }, { "tag", required_argument, 0, 't' }, { "socket", required_argument, 0, 'u' }, { "udp", no_argument, 0, 'd' }, { "tcp", no_argument, 0, 'T' }, { "server", required_argument, 0, 'n' }, { "port", required_argument, 0, 'P' }, { "version", no_argument, 0, 'V' }, { "help", no_argument, 0, 'h' }, { "prio-prefix", no_argument, 0, OPT_PRIO_PREFIX }, { "rfc3164", no_argument, 0, OPT_RFC3164 }, { "rfc5424", optional_argument, 0, OPT_RFC5424 }, #ifdef HAVE_LIBSYSTEMD { "journald", optional_argument, 0, OPT_JOURNALD }, #endif { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((ch = getopt_long(argc, argv, "f:i::p:st:u:dTn:P:Vh", longopts, NULL)) != -1) { switch (ch) { case 'f': /* file to log */ if (freopen(optarg, "r", stdin) == NULL) err(EXIT_FAILURE, _("file %s"), optarg); stdout_reopened = 1; break; case 'i': /* log process id also */ if (optarg) { const char *p = optarg; if (*p == '=') p++; ctl.pid = strtoul_or_err(optarg, _("failed to parse id")); } else ctl.pid = getpid(); break; case 'p': /* priority */ ctl.pri = pencode(optarg); break; case 's': /* log to standard error */ ctl.stderr_printout = 1; break; case 't': /* tag */ ctl.tag = optarg; break; case 'u': /* unix socket */ ctl.unix_socket = optarg; break; case 'd': ctl.socket_type = TYPE_UDP; break; case 'T': ctl.socket_type = TYPE_TCP; break; case 'n': ctl.server = optarg; break; case 'P': ctl.port = optarg; break; case 'V': printf(UTIL_LINUX_VERSION); exit(EXIT_SUCCESS); case 'h': usage(stdout); case OPT_PRIO_PREFIX: ctl.prio_prefix = 1; break; case OPT_RFC3164: ctl.syslogfp = syslog_rfc3164; break; case OPT_RFC5424: ctl.syslogfp = syslog_rfc5424; if (optarg) parse_rfc5424_flags(&ctl, optarg); break; #ifdef HAVE_LIBSYSTEMD case OPT_JOURNALD: if (optarg) { jfd = fopen(optarg, "r"); if (!jfd) err(EXIT_FAILURE, _("cannot open %s"), optarg); } else jfd = stdin; break; #endif case '?': default: usage(stderr); } } argc -= optind; argv += optind; if (stdout_reopened && argc) warnx(_("--file <file> and <message> are mutually exclusive, message is ignored")); #ifdef HAVE_LIBSYSTEMD if (jfd) { int ret = journald_entry(jfd); if (stdin != jfd) fclose(jfd); if (ret) errx(EXIT_FAILURE, _("journald entry could not be wrote")); return EXIT_SUCCESS; } #endif logger_open(&ctl); if (0 < argc) logger_command_line(&ctl, argv); else /* Note. --file <arg> reopens stdin making the below * function to be used for file inputs. */ logger_stdin(&ctl); logger_close(&ctl); return EXIT_SUCCESS; }
/* * logger -- read and log utility * * Reads from an input and arranges to write the result on the system * log. */ int main(int argc, char *argv[]) { int ch, logflags, pri; char *tag, *host, buf[1024]; const char *svcname; tag = NULL; host = NULL; svcname = "syslog"; pri = LOG_USER | LOG_NOTICE; logflags = 0; unsetenv("TZ"); while ((ch = getopt(argc, argv, "46Af:h:iP:p:st:")) != -1) switch((char)ch) { case '4': family = PF_INET; break; #ifdef INET6 case '6': family = PF_INET6; break; #endif case 'A': send_to_all++; break; case 'f': /* file to log */ if (freopen(optarg, "r", stdin) == NULL) err(1, "%s", optarg); setvbuf(stdin, 0, _IONBF, 0); break; case 'h': /* hostname to deliver to */ host = optarg; break; case 'i': /* log process id also */ logflags |= LOG_PID; break; case 'P': /* service name or port number */ svcname = optarg; break; case 'p': /* priority */ pri = pencode(optarg); break; case 's': /* log to standard error */ logflags |= LOG_PERROR; break; case 't': /* tag */ tag = optarg; break; case '?': default: usage(); } argc -= optind; argv += optind; if (tag == NULL) tag = getlogin(); /* setup for logging */ if (host == NULL) openlog(tag, logflags, 0); (void) fclose(stdout); /* log input line if appropriate */ if (argc > 0) { char *p, *endp; size_t len; for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) { len = strlen(*argv); if (p + len > endp && p > buf) { logmessage(pri, tag, host, svcname, buf); p = buf; } if (len > sizeof(buf) - 1) logmessage(pri, tag, host, svcname, *argv++); else { if (p != buf) *p++ = ' '; bcopy(*argv++, p, len); *(p += len) = '\0'; } } if (p != buf) logmessage(pri, tag, host, svcname, buf); } else while (fgets(buf, sizeof(buf), stdin) != NULL) logmessage(pri, tag, host, svcname, buf); exit(0); }
/* * logger -- read and log utility * * Reads from an input and arranges to write the result on the system * log. */ int main(int argc, char *argv[]) { int ch, logflags, pri; const char *tag; const char *sd = "-"; const char *msgid = "-"; char buf[1024]; tag = NULL; pri = LOG_NOTICE; logflags = 0; while ((ch = getopt(argc, argv, "d:f:im:p:st:")) != -1) switch((char)ch) { case 'd': /* structured data field */ sd = optarg; break; case 'f': /* file to log */ if (freopen(optarg, "r", stdin) == NULL) err(EXIT_FAILURE, "%s", optarg); break; case 'i': /* log process id also */ logflags |= LOG_PID; break; case 'm': /* msgid field */ msgid = optarg; break; case 'p': /* priority */ pri = pencode(optarg); break; case 's': /* log to standard error */ logflags |= LOG_PERROR; break; case 't': /* tag */ tag = optarg; break; case '?': default: usage(); } argc -= optind; argv += optind; /* setup for logging */ openlog(tag != NULL ? tag : getlogin(), logflags, 0); (void)fclose(stdout); /* log input line if appropriate */ if (argc > 0) { char *p, *endp; int len; for (p = buf, endp = buf + sizeof(buf) - 2; *argv != NULL;) { len = strlen(*argv); if (p + len > endp && p > buf) { syslogp(pri, "%s", "%s", "%s", msgid, sd, buf); p = buf; } if (len > sizeof(buf) - 1) syslogp(pri, "%s", "%s", "%s", msgid, sd, *argv++); else { if (p != buf) *p++ = ' '; memmove(p, *argv++, len); *(p += len) = '\0'; } } if (p != buf) syslogp(pri, "%s", "%s", "%s", msgid, sd, buf); } else /* TODO: allow syslog-protocol messages from file/stdin * but that will require parsing the line to split * it into three fields. */ while (fgets(buf, sizeof(buf), stdin) != NULL) syslogp(pri, "%s", "%s", "%s", msgid, sd, buf); exit(EXIT_SUCCESS); /* NOTREACHED */ }
/* * logger -- read and log utility * Reads from an input and arranges to write the result on the system log. */ int main (int argc, char **argv) { int ch, logflags, pri; char *tag, buf[MAX_LINE]; char *usock = NULL; long timeout_ms = 100; long timeout_sec; long timeout_usec; int indent_mode = 0; char *udpserver = NULL; int LogSock = -1; long tmpport; static const struct option longopts[] = { { "id", no_argument, 0, 'i' }, { "stderr", no_argument, 0, 's' }, { "file", required_argument, 0, 'f' }, { "priority", required_argument, 0, 'p' }, { "tag", required_argument, 0, 't' }, { "socket", required_argument, 0, 'u' }, { "udp", no_argument, 0, 'd' }, { "server", required_argument, 0, 'n' }, { "port", required_argument, 0, 'P' }, { "indent", required_argument, 0, 'I' }, { "version", no_argument, 0, 'V' }, { "help", no_argument, 0, 'h' }, { NULL, 0, 0, 0 } }; tag = NULL; pri = LOG_NOTICE; logflags = 0; while ((ch = getopt_long(argc, argv, "f:ip:st:u:dI:n:P:Vh", longopts, NULL)) != -1) { switch((char) ch) { case 'f': /* file to log */ if (freopen(optarg, "r", stdin) == NULL) err(EXIT_FAILURE, "file %s", optarg); break; case 'i': /* log process id also */ logflags |= LOG_PID; break; case 'p': /* priority */ pri = pencode(optarg); break; case 's': /* log to standard error */ logflags |= LOG_PERROR; break; case 't': /* tag */ tag = optarg; break; case 'u': /* unix socket */ usock = optarg; break; case 'd': optd = 1; /* use datagrams */ break; case 'n': /* udp socket */ optd = 1; /* use datagrams because udp */ udpserver = optarg; break; case 'P': /* change udp port */ tmpport = strtol_or_err(optarg, "failed to parse port number"); if (tmpport < 0 || 65535 < tmpport) { errx(EXIT_FAILURE, "port `%ld' out of range", tmpport); } udpport = (int) tmpport; break; case 'V': printf("%s %s\n", PROGRAM_NAME, PROGRAM_VERSION); exit(EXIT_SUCCESS); case 'I': indent_mode = 1; timeout_ms = strtol_or_err(optarg, "failed to parse timeout number"); if (timeout_ms < 1) { errx(EXIT_FAILURE, "Invalid value for timeout %li", timeout_ms); } break; case 'h': usage(stdout); case '?': default: usage(stderr); } } argc -= optind; argv += optind; /* setup for logging */ if (!usock && !udpserver) { openlog(tag ? tag : getlogin(), logflags, 0); } else if (udpserver) { LogSock = udpopenlog(udpserver, udpport); } else { LogSock = myopenlog(usock); } (void) fclose(stdout); /* log input line if appropriate */ if (argc > 0) { char *p, *endp; size_t len; for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) { len = strlen(*argv); if (p + len > endp && p > buf) { if (!usock && !udpserver) { syslog(pri, "%s", buf); } else { mysyslog(LogSock, logflags, pri, tag, buf); } p = buf; } if (len > sizeof(buf) - 1) { if (!usock && !udpserver) { syslog(pri, "%s", *argv++); } else { mysyslog(LogSock, logflags, pri, tag, *argv++); } } else { if (p != buf) { *p++ = ' '; } memmove(p, *argv++, len); *(p += len) = '\0'; } } if (p != buf) { if (!usock && !udpserver) { syslog(pri, "%s", buf); } else { mysyslog(LogSock, logflags, pri, tag, buf); } } } else if (indent_mode) { int len; timeout_sec = timeout_ms / 1000; timeout_usec = (timeout_ms % 1000) * 1000; while ((len = readBlock(buf, MAX_LINE, timeout_sec, timeout_usec)) != EOF) { //fprintf(stderr, "Got buf %i\n", len); if (len > 0 && buf[len - 1] == '\n') { buf[len - 1] = '\0'; } if (!usock && !udpserver) { syslog(pri, "%s", buf); } else { mysyslog(LogSock, logflags, pri, tag, buf); } } } else { while (fgets(buf, sizeof(buf), stdin) != NULL) { /* glibc is buggy and adds an additional newline, so we have to remove it here until glibc is fixed */ int len = strlen(buf); if (len > 0 && buf[len - 1] == '\n') { buf[len - 1] = '\0'; } if (!usock && !udpserver) { syslog(pri, "%s", buf); } else { mysyslog(LogSock, logflags, pri, tag, buf); } } } if (!usock && !udpserver) { closelog(); } else { close(LogSock); } return EXIT_SUCCESS; }
int main(int argc, char **argv) { char tmp[23]; char *tag = NULL; char *infile = NULL; char *buf = NULL; size_t buflen; int pri = LOG_NOTICE; int logflags = 0; int opt; int pid_len = 0; struct passwd *pw; uid_t u; char fmt_uid[16]; char *p, *endp; size_t len; ptrdiff_t offset = 0; int status = 0; (void) setlocale(LC_ALL, ""); #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ #endif (void) textdomain(TEXT_DOMAIN); /* initialize */ while ((opt = getopt(argc, argv, "it:p:f:")) != EOF) switch (opt) { case 't': /* tag */ tag = optarg; break; case 'p': /* priority */ pri = pencode(optarg); break; case 'i': /* log process id also */ logflags |= LOG_PID; pid_len = sprintf(tmp, "%ld", (long)getpid()); pid_len = (pid_len <= 0) ? 0 : pid_len +2; break; case 'f': /* file to log */ if (strcmp(optarg, "-") == 0) break; infile = optarg; if (freopen(infile, "r", stdin) == NULL) { (void) fprintf(stderr, gettext("logger: ")); perror(infile); exit(1); } break; default: usage(); } argc -= optind; argv = &argv[optind]; if ((tag == NULL) && ((tag = getlogin()) == NULL)) { u = getuid(); if ((pw = getpwuid(u)) == NULL) { (void) sprintf(fmt_uid, "%u", u); tag = fmt_uid; } else tag = pw->pw_name; } /* setup for logging */ openlog(tag, logflags, 0); (void) fclose(stdout); /* log input line if appropriate */ if (argc > 0) { /* * Log arguments from command line */ int i; len = 0; for (i = 0; i < argc; i++) { len += strlen(argv[i]) + 1; /* add 1 for <space> */ } if ((buf = malloc(len + 1)) == NULL) { perror("logger"); exit(1); } buf[0] = '\0'; for (i = 0; i < argc; i++) { if (i != 0) { (void) strcat(buf, " "); } (void) strcat(buf, argv[i]); } #ifdef DEBUG (void) fprintf(stderr, "len=%d, buf >%s<\n", len, buf); #endif syslog(pri, "%s", buf); } else { /* * Log arguments from stdin (or input file). * When reading from stdin, logger grows its buffer if * needed, to handle long lines. */ if ((buf = malloc(LOGGER_BUFLEN)) == NULL) { perror("logger"); exit(1); } buflen = LOGGER_BUFLEN; p = buf; endp = buf + buflen; offset = 0; while (fgets(p, endp - p, stdin) != NULL) { len = strlen(p); if (p[len - 1] == '\n') { #ifdef DEBUG (void) fprintf(stderr, "p-buf =%d, len=%d, buflen=%d, buf >%s<\n", p-buf, len, buflen, buf); #endif syslog(pri, "%s", buf); p = buf; offset = 0; } else if (len < endp - p - 1) { /* short read or line with no <newline> */ p += len; offset += len; #ifdef DEBUG (void) fprintf(stderr, "p-buf=%d, len=%d, buflen=%d, buf >%s<\n", p-buf, len, buflen, buf); #endif continue; } else { /* line longer than buflen, so get larger buf */ buflen += LOGGER_BUFLEN; offset += len; #ifdef DEBUG (void) fprintf(stderr, "Realloc endp-p=%d, len=%d, offset=%d, " "buflen %d\n", endp - p, len, offset, buflen); #endif if ((buf = realloc(buf, buflen)) == NULL) { perror("logger"); exit(1); } p = buf + offset; endp = buf + buflen; } } /* while */ if (feof(stdin)) { if (p > buf) { /* the last line did not end with newline */ #ifdef DEBUG (void) fprintf(stderr, "(2) p-buf=%d, len=%d, buflen=%d, " "buf >%s<\n", p-buf, len, buflen, buf); #endif syslog(pri, "%s", buf); } } else { /* * fgets() encountered an error. Log unlogged data * from earlier fgets() (if any). Write null byte * after last full read, in case the fgets() that * encountered error removed it and failed to null * terminate. */ perror("logger"); if (p > buf) { *p = '\0'; syslog(pri, "%s", buf); } status = 1; } } /* else !(argc > 0) */ free(buf); return (status); }
/* * logger -- read and log utility * * Reads from an input and arranges to write the result on the system * log. */ int main(int argc, char **argv) { int LogSock; int ch, pri = LOG_NOTICE; char *tag = NULL; uint16_t port = 0; char buf[MAX_LNE_LEN]; int prilen, hdrlen; static const struct option longopts[] = { { "priority", required_argument, 0, 'p' }, { "tag", required_argument, 0, 't' }, { "port", required_argument, 0, 'P' }, { "help", no_argument, 0, 'h' }, { NULL, 0, 0, 0 } }; while ((ch = getopt_long(argc, argv, "p:st:P:Vh", longopts, NULL)) != -1) { int iport = 0; switch (ch) { case 'p': /* priority */ pri = pencode(optarg); break; case 't': /* tag */ tag = optarg; if(strlen(tag) > MAX_TAG_LEN) errx(EXIT_FAILURE, "tag must be no longer than %d", MAX_TAG_LEN); break; case 'P': iport = atoi(optarg); if(iport > (uint16_t)-1 || iport <= 0) errx(EXIT_FAILURE, "Invalid port"); port = iport; break; case 'h': usage(stdout); exit(EXIT_SUCCESS); case '?': default: usage(stderr); exit(EXIT_FAILURE); } } argc -= optind; argv += optind; if(port == 0) errx(EXIT_FAILURE, "port is required."); if(tag == NULL) errx(EXIT_FAILURE, "tag is required."); if (argc > 0) errx(EXIT_FAILURE, "extra command line arguments."); /* setup for logging */ LogSock = inet_socket(port); gen_header(buf, pri, tag, &prilen, &hdrlen); log_from_stdin(LogSock, buf, prilen, hdrlen); close(LogSock); return EXIT_SUCCESS; }
/* ** Name: int main(int argc, char **argv); ** Function: Main entry for logger. */ int main(int argc, char **argv) { int pri = LOG_NOTICE; int ch, logflags = 0; char *tag, buf[200]; static const char usage[] = "[-i] [-f file] [-p pri] [-t tag] [ message ... ]"; tag = NULL; while ((ch = getopt(argc, argv, "f:ip:t:")) != EOF) { switch ((char) ch) { case 'f': /* file to log */ if (freopen(optarg, "r", stdin) == NULL) { bailout(strerror(errno), optarg); } break; case 'i': /* log process id also */ logflags |= LOG_PID; break; case 'p': /* priority */ pri = pencode(optarg); break; case 't': /* tag */ tag = optarg; break; case '?': default: bailout(usage, ""); break; } } argc -= optind; argv += optind; /* Setup for logging */ openlog(tag ? tag : getlogin(), logflags, 0); fclose(stdout); if (argc > 0) { /* Log input line if appropriate */ char *p, *endp; int len; for (p = buf, endp = buf + sizeof(buf) - 1;;) { len = strlen(*argv); if (p + len < endp && p > buf) { *--p = '\0'; syslog(pri, buf); p = buf; } if (len > sizeof(buf) - 1) { syslog(pri, *argv++); if (!--argc) break; } else { memcpy(p, *argv++, len); p += len; if (!--argc) break; *p++ = ' '; *--p = '\0'; } } if (p != buf) { *p = '\0'; syslog(pri, buf); } } else /* Main loop */ while (fgets(buf, sizeof(buf), stdin) != NULL) syslog(pri, buf); return EXIT_SUCCESS; }
main(int argc, char **argv) { int pri = LOG_NOTICE; int ch, logflags = 0; char *tag, buf[1024]; tag = NULL; while ((ch = getopt(argc, argv, "f:ip:st:")) != EOF) switch((char)ch) { case 'f': /* file to log */ if (freopen(optarg, "r", stdin) == NULL) { (void)fprintf(stderr, "logger: %s: %s.\n", optarg, strerror(errno)); exit(1); } break; case 'i': /* log process id also */ logflags |= LOG_PID; break; case 'p': /* priority */ pri = pencode(optarg); break; case 's': /* log to standard error */ logflags |= LOG_PERROR; break; case 't': /* tag */ tag = optarg; break; case '?': default: usage(); } argc -= optind; argv += optind; /* setup for logging */ openlog(tag ? tag : getlogin(), logflags, 0); (void) fclose(stdout); /* log input line if appropriate */ if (argc > 0) { register char *p, *endp; int len; for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) { len = strlen(*argv); if (p + len > endp && p > buf) { syslog(pri, "%s", buf); p = buf; } if (len > sizeof(buf) - 1) syslog(pri, "%s", *argv++); else { if (p != buf) *p++ = ' '; bcopy(*argv++, p, len); *(p += len) = '\0'; } } if (p != buf) syslog(pri, "%s", buf); exit(0); } /* main loop */ while (fgets(buf, sizeof(buf), stdin) != NULL) syslog(pri, "%s", buf); exit(0); }