~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to misc-utils/logger.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
#include <sys/types.h>
48
48
#include <sys/socket.h>
49
49
#include <sys/un.h>
 
50
#include <arpa/inet.h>
 
51
#include <netdb.h>
 
52
#include <getopt.h>
 
53
 
 
54
#include "c.h"
50
55
#include "nls.h"
 
56
#include "strutils.h"
51
57
 
52
58
#define SYSLOG_NAMES
53
59
#include <syslog.h>
54
60
 
55
61
int     decode __P((char *, CODE *));
56
62
int     pencode __P((char *));
57
 
void    usage __P((void));
58
63
 
59
64
static int optd = 0;
 
65
static int udpport = 514;
60
66
 
61
67
static int
62
68
myopenlog(const char *sock) {
63
69
       int fd;
64
70
       static struct sockaddr_un s_addr; /* AF_UNIX address of local logger */
65
71
 
66
 
       if (strlen(sock) >= sizeof(s_addr.sun_path)) {
67
 
               printf (_("logger: openlog: pathname too long\n"));
68
 
               exit(1);
69
 
       }
 
72
       if (strlen(sock) >= sizeof(s_addr.sun_path))
 
73
               errx(EXIT_FAILURE, _("openlog %s: pathname too long"), sock);
70
74
 
71
75
       s_addr.sun_family = AF_UNIX;
72
76
       (void)strcpy(s_addr.sun_path, sock);
73
77
 
74
 
       if ((fd = socket(AF_UNIX, optd ? SOCK_DGRAM : SOCK_STREAM, 0)) == -1) {
75
 
               printf (_("socket: %s.\n"), strerror(errno));
76
 
               exit (1);
77
 
       }
78
 
 
79
 
       if (connect(fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1) {
80
 
               printf (_("connect: %s.\n"), strerror(errno));
81
 
               exit (1);
82
 
       }
 
78
       if ((fd = socket(AF_UNIX, optd ? SOCK_DGRAM : SOCK_STREAM, 0)) == -1)
 
79
               err(EXIT_FAILURE, _("socket %s"), sock);
 
80
 
 
81
       if (connect(fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1)
 
82
               err(EXIT_FAILURE, _("connect %s"), sock);
 
83
 
83
84
       return fd;
84
85
}
85
86
 
 
87
static int
 
88
udpopenlog(const char *servername,int port) {
 
89
        int fd;
 
90
        struct sockaddr_in s_addr;
 
91
        struct hostent *serverhost;
 
92
 
 
93
        if ((serverhost = gethostbyname(servername)) == NULL )
 
94
                errx(EXIT_FAILURE, _("unable to resolve '%s'"), servername);
 
95
 
 
96
        if ((fd = socket(AF_INET, SOCK_DGRAM , 0)) == -1)
 
97
                err(EXIT_FAILURE, _("socket"));
 
98
 
 
99
        bcopy(serverhost->h_addr,&s_addr.sin_addr,serverhost->h_length);
 
100
        s_addr.sin_family=AF_INET;
 
101
        s_addr.sin_port=htons(port);
 
102
 
 
103
        if (connect(fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1)
 
104
                err(EXIT_FAILURE, _("connect"));
 
105
 
 
106
        return fd;
 
107
}
86
108
static void
87
109
mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
88
110
       char buf[1000], pid[30], *cp, *tp;
111
133
       }
112
134
}
113
135
 
 
136
static void __attribute__ ((__noreturn__)) usage(FILE *out)
 
137
{
 
138
        fputs(_("\nUsage:\n"), out);
 
139
        fprintf(out,
 
140
              _(" %s [options] [message]\n"), program_invocation_short_name);
 
141
 
 
142
        fputs(_("\nOptions:\n"), out);
 
143
        fputs(_(" -d, --udp             use UDP (TCP is default)\n"
 
144
                " -i, --id              log the process ID too\n"
 
145
                " -f, --file <file>     log the contents of this file\n"
 
146
                " -h, --help            display this help text and exit\n"), out);
 
147
        fputs(_(" -n, --server <name>   write to this remote syslog server\n"
 
148
                " -P, --port <number>   use this UDP port\n"
 
149
                " -p, --priority <prio> mark given message with this priority\n"
 
150
                " -s, --stderr          output message to standard error as well\n"), out);
 
151
        fputs(_(" -t, --tag <tag>       mark every line with this tag\n"
 
152
                " -u, --socket <socket> write to this Unix socket\n"
 
153
                " -V, --version         output version information and exit\n\n"), out);
 
154
 
 
155
        exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
 
156
}
 
157
 
114
158
/*
115
159
 * logger -- read and log utility
116
160
 *
122
166
        int ch, logflags, pri;
123
167
        char *tag, buf[1024];
124
168
        char *usock = NULL;
 
169
        char *udpserver = NULL;
125
170
        int LogSock = -1;
 
171
        long tmpport;
 
172
 
 
173
        static const struct option longopts[] = {
 
174
                { "id",         no_argument,        0, 'i' },
 
175
                { "stderr",     no_argument,        0, 's' },
 
176
                { "file",       required_argument,  0, 'f' },
 
177
                { "priority",   required_argument,  0, 'p' },
 
178
                { "tag",        required_argument,  0, 't' },
 
179
                { "socket",     required_argument,  0, 'u' },
 
180
                { "udp",        no_argument,        0, 'd' },
 
181
                { "server",     required_argument,  0, 'n' },
 
182
                { "port",       required_argument,  0, 'P' },
 
183
                { "version",    no_argument,        0, 'V' },
 
184
                { "help",       no_argument,        0, 'h' },
 
185
                { NULL,         0, 0, 0 }
 
186
        };
126
187
 
127
188
        setlocale(LC_ALL, "");
128
189
        bindtextdomain(PACKAGE, LOCALEDIR);
131
192
        tag = NULL;
132
193
        pri = LOG_NOTICE;
133
194
        logflags = 0;
134
 
        while ((ch = getopt(argc, argv, "f:ip:st:u:d")) != -1)
 
195
        while ((ch = getopt_long(argc, argv, "f:ip:st:u:dn:P:Vh",
 
196
                                            longopts, NULL)) != -1) {
135
197
                switch((char)ch) {
136
198
                case 'f':               /* file to log */
137
 
                        if (freopen(optarg, "r", stdin) == NULL) {
138
 
                                int errsv = errno;
139
 
                                (void)fprintf(stderr, _("logger: %s: %s.\n"),
140
 
                                    optarg, strerror(errsv));
141
 
                                exit(1);
142
 
                        }
 
199
                        if (freopen(optarg, "r", stdin) == NULL)
 
200
                                err(EXIT_FAILURE, _("file %s"),
 
201
                                    optarg);
143
202
                        break;
144
203
                case 'i':               /* log process id also */
145
204
                        logflags |= LOG_PID;
159
218
                case 'd':
160
219
                        optd = 1;       /* use datagrams */
161
220
                        break;
 
221
                case 'n':               /* udp socket */
 
222
                        optd = 1;       /* use datagrams because udp */
 
223
                        udpserver = optarg;
 
224
                        break;
 
225
                case 'P':               /* change udp port */
 
226
                        tmpport = strtol_or_err(optarg,
 
227
                                                _("failed to parse port number"));
 
228
                        if (tmpport < 0 || 65535 < tmpport)
 
229
                                errx(EXIT_FAILURE, _("port `%ld' out of range"),
 
230
                                                tmpport);
 
231
                        udpport = (int) tmpport;
 
232
                        break;
 
233
                case 'V':
 
234
                        printf(_("%s from %s\n"), program_invocation_short_name,
 
235
                                                  PACKAGE_STRING);
 
236
                        exit(EXIT_SUCCESS);
 
237
                case 'h':
 
238
                        usage(stdout);
162
239
                case '?':
163
240
                default:
164
 
                        usage();
 
241
                        usage(stderr);
165
242
                }
 
243
        }
166
244
        argc -= optind;
167
245
        argv += optind;
168
246
 
169
247
        /* setup for logging */
170
 
        if (!usock)
 
248
        if (!usock && !udpserver)
171
249
                openlog(tag ? tag : getlogin(), logflags, 0);
 
250
        else if (udpserver)
 
251
                LogSock = udpopenlog(udpserver,udpport);
172
252
        else
173
253
                LogSock = myopenlog(usock);
174
254
 
182
262
                for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
183
263
                        len = strlen(*argv);
184
264
                        if (p + len > endp && p > buf) {
185
 
                            if (!usock)
 
265
                            if (!usock && !udpserver)
186
266
                                syslog(pri, "%s", buf);
187
267
                            else
188
268
                                mysyslog(LogSock, logflags, pri, tag, buf);
189
269
                                p = buf;
190
270
                        }
191
271
                        if (len > sizeof(buf) - 1) {
192
 
                            if (!usock)
 
272
                            if (!usock && !udpserver)
193
273
                                syslog(pri, "%s", *argv++);
194
274
                            else
195
275
                                mysyslog(LogSock, logflags, pri, tag, *argv++);
206
286
                    else
207
287
                        mysyslog(LogSock, logflags, pri, tag, buf);
208
288
                }
209
 
        } else
 
289
        } else {
210
290
                while (fgets(buf, sizeof(buf), stdin) != NULL) {
211
291
                    /* glibc is buggy and adds an additional newline,
212
292
                       so we have to remove it here until glibc is fixed */
220
300
                    else
221
301
                        mysyslog(LogSock, logflags, pri, tag, buf);
222
302
                }
 
303
        }
223
304
        if (!usock)
224
305
                closelog();
225
306
        else
226
307
                close(LogSock);
227
 
        exit(0);
 
308
 
 
309
        return EXIT_SUCCESS;
228
310
}
229
311
 
230
312
/*
241
323
        if (*s) {
242
324
                *s = '\0';
243
325
                fac = decode(save, facilitynames);
244
 
                if (fac < 0) {
245
 
                        (void)fprintf(stderr,
246
 
                            _("logger: unknown facility name: %s.\n"), save);
247
 
                        exit(1);
248
 
                }
 
326
                if (fac < 0)
 
327
                        errx(EXIT_FAILURE,
 
328
                            _("unknown facility name: %s."), save);
249
329
                *s++ = '.';
250
330
        }
251
331
        else {
253
333
                s = save;
254
334
        }
255
335
        lev = decode(s, prioritynames);
256
 
        if (lev < 0) {
257
 
                (void)fprintf(stderr,
258
 
                    _("logger: unknown priority name: %s.\n"), save);
259
 
                exit(1);
260
 
        }
 
336
        if (lev < 0)
 
337
                errx(EXIT_FAILURE,
 
338
                    _("unknown priority name: %s."), save);
261
339
        return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
262
340
}
263
341
 
277
355
 
278
356
        return (-1);
279
357
}
280
 
 
281
 
void
282
 
usage()
283
 
{
284
 
        (void)fprintf(stderr,
285
 
            _("usage: logger [-is] [-f file] [-p pri] [-t tag] [-u socket] [ message ... ]\n"));
286
 
        exit(1);
287
 
}