From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on sa.local.altlinux.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.1 X-MC-Unique: -1IFeEDEM-S5CZLRpYd99w-1 From: Alexey Gladkov To: ALT Devel discussion list Date: Tue, 27 Oct 2020 12:33:46 +0100 Message-Id: <20201027113351.3373843-2-legion@altlinux.ru> In-Reply-To: <20201027113351.3373843-1-legion@altlinux.ru> References: <20201027113351.3373843-1-legion@altlinux.ru> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: altlinux.ru Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=WINDOWS-1252 Subject: [devel] [PATCH 1/6] Optimize the filling of the record fields X-BeenThere: devel@lists.altlinux.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: ALT Linux Team development discussions List-Id: ALT Linux Team development discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 27 Oct 2020 11:34:06 -0000 Archived-At: List-Archive: List-Post: Signed-off-by: Alexey Gladkov --- syslogd.c | 84 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/syslogd.c b/syslogd.c index 0f2260d..55281bd 100644 --- a/syslogd.c +++ b/syslogd.c @@ -289,6 +289,16 @@ struct sourceinfo { =09unsigned int flags; } sinfo; =20 +enum record_fields_type { +=09RECORD_FIELD_TIME =3D 0, +=09RECORD_FIELD_SEP1, +=09RECORD_FIELD_HOST, +=09RECORD_FIELD_SEP2, +=09RECORD_FIELD_MSG, +=09RECORD_FIELD_EOL, +=09RECORD_FIELD_COUNTS, +}; + static int=09Debug;=09=09=09/* debug flag */ static int=09Compress =3D 1;=09=09/* compress repeated messages flag */ static char=09LocalHostName[MAXHOSTNAMELEN+1];=09/* our hostname */ @@ -332,7 +342,7 @@ void printline(const struct sourceinfo* const, char *ms= g); void logmsg(int pri, char *msg, const struct sourceinfo* const, int flags)= ; void fprintlog(register struct filed *f, char *from, int flags, char *msg)= ; void endtty(int); -void wallmsg(register struct filed *f, struct iovec *iov, size_t iovsz); +void wallmsg(register struct filed *f, struct iovec iov[RECORD_FIELD_COUNT= S]); void reapchild(int); const char *cvtaddr(struct sockaddr_storage *f, int len); const char *cvthname(struct sockaddr_storage *f, int len); @@ -1443,10 +1453,16 @@ finish: =09sigprocmask(SIG_UNBLOCK, &mask, NULL); } =20 +static inline void set_record_field(struct iovec iov[RECORD_FIELD_COUNTS], +=09=09enum record_fields_type name, char *value, size_t len) +{ +=09iov[name].iov_base =3D value; +=09iov[name].iov_len =3D len =3D=3D -1 ? strlen(value) : len; +} + void fprintlog(struct filed *f, char *from, int flags, char *msg) { -=09struct iovec iov[6]; -=09register struct iovec *v =3D iov; +=09struct iovec iov[RECORD_FIELD_COUNTS]; =09char repbuf[80]; #ifdef SYSLOG_INET =09register int l; @@ -1458,31 +1474,20 @@ void fprintlog(struct filed *f, char *from, int fla= gs, char *msg) =20 =09verbosef("Called fprintlog, "); =20 -=09v->iov_base =3D f->f_lasttime; -=09v->iov_len =3D 15; -=09v++; -=09v->iov_base =3D " "; -=09v->iov_len =3D 1; -=09v++; -=09v->iov_base =3D f->f_prevhost; -=09v->iov_len =3D strlen(v->iov_base); -=09v++; -=09v->iov_base =3D " "; -=09v->iov_len =3D 1; -=09v++; +=09set_record_field(iov, RECORD_FIELD_TIME, f->f_lasttime, 15); +=09set_record_field(iov, RECORD_FIELD_SEP1, " ", 1); +=09set_record_field(iov, RECORD_FIELD_HOST, f->f_prevhost, -1); +=09set_record_field(iov, RECORD_FIELD_SEP2, " ", 1); + =09if (msg) { -=09=09v->iov_base =3D msg; -=09=09v->iov_len =3D strlen(msg); +=09=09set_record_field(iov, RECORD_FIELD_MSG, msg, -1); =09} else if (f->f_prevcount > 1) { =09=09(void) snprintf(repbuf, sizeof(repbuf), "last message repeated %d ti= mes", =09=09 f->f_prevcount); -=09=09v->iov_base =3D repbuf; -=09=09v->iov_len =3D strlen(repbuf); +=09=09set_record_field(iov, RECORD_FIELD_MSG, repbuf, -1); =09} else { -=09=09v->iov_base =3D f->f_prevline; -=09=09v->iov_len =3D f->f_prevlen; +=09=09set_record_field(iov, RECORD_FIELD_MSG, f->f_prevline, f->f_prevlen)= ; =09} -=09v++; =20 =09verbosef("logging to %s", TypeNames[f->f_type]); =20 @@ -1508,14 +1513,13 @@ void fprintlog(struct filed *f, char *from, int fla= gs, char *msg) =09=09=09=09(long)(INET_SUSPEND_TIME - fwd_suspend)); =09=09} =09=09break; -=09=09 =09/* =09 * The trick is to wait some time, then retry to get the =09 * address. If that fails retry x times and then give up. =09 * =09 * You'll run into this problem mostly if the name server you =09 * need for resolving the address is on the same machine, but -=09 * is started after syslogd.=20 +=09 * is started after syslogd. =09 */ =09case F_FORW_UNKN: =09=09verbosef(" %s\n", f->f_un.f_forw.f_hname); @@ -1549,7 +1553,7 @@ void fprintlog(struct filed *f, char *from, int flags= , char *msg) =09=09break; =20 =09case F_FORW: -=09=09/*=20 +=09=09/* =09=09 * Don't send any message to a remote host if it =09=09 * already comes from one. (we don't care 'bout who =09=09 * sent the message, we don't send it anyway) -Joey @@ -1562,7 +1566,7 @@ void fprintlog(struct filed *f, char *from, int flags= , char *msg) =09=09=09int i; =09=09=09f->f_time =3D now; =09=09=09(void) snprintf(line, sizeof(line), "<%d>%s", f->f_prevpri, \ -=09=09=09=09(char *) iov[4].iov_base); +=09=09=09=09(char *) iov[RECORD_FIELD_MSG].iov_base); =09=09=09l =3D strlen(line); =09=09=09if (l > MAXLINE) =09=09=09=09l =3D MAXLINE; @@ -1582,7 +1586,7 @@ void fprintlog(struct filed *f, char *from, int flags= , char *msg) =09=09=09=09=09break; =09=09=09} =09=09=09if (err !=3D -1) { -=09=09=09=09verbosef("INET sendto error: %d =3D %s.\n",=20 +=09=09=09=09verbosef("INET sendto error: %d =3D %s.\n", =09=09=09=09=09err, strerror(err)); =09=09=09=09f->f_type =3D F_FORW_SUSP; =09=09=09=09errno =3D err; @@ -1597,7 +1601,7 @@ void fprintlog(struct filed *f, char *from, int flags= , char *msg) #ifdef UNIXPC =09=09if (1) { #else -=09=09if (flags & IGN_CONS) {=09 +=09=09if (flags & IGN_CONS) { #endif =09=09=09verbosef(" (ignored).\n"); =09=09=09break; @@ -1610,11 +1614,9 @@ void fprintlog(struct filed *f, char *from, int flag= s, char *msg) =09=09f->f_time =3D now; =09=09verbosef(" %s\n", f->f_un.f_fname); =09=09if (f->f_type =3D=3D F_TTY || f->f_type =3D=3D F_CONSOLE) { -=09=09=09v->iov_base =3D "\r\n"; -=09=09=09v->iov_len =3D 2; +=09=09=09set_record_field(iov, RECORD_FIELD_EOL, "\r\n", 2); =09=09} else { -=09=09=09v->iov_base =3D "\n"; -=09=09=09v->iov_len =3D 1; +=09=09=09set_record_field(iov, RECORD_FIELD_EOL, "\n", 1); =09=09} =09again: =09=09/* f->f_file =3D=3D -1 is an indicator that we couldn't @@ -1622,7 +1624,7 @@ void fprintlog(struct filed *f, char *from, int flags= , char *msg) =09=09if (f->f_file =3D=3D -1) =09=09=09break; =20 -=09=09if (writev(f->f_file, iov, 6) < 0) { +=09=09if (writev(f->f_file, iov, RECORD_FIELD_COUNTS) < 0) { =09=09=09int e =3D errno; =20 =09=09=09/* If a named pipe is full, just ignore it for now */ @@ -1669,9 +1671,8 @@ void fprintlog(struct filed *f, char *from, int flags= , char *msg) =09case F_WALL: =09=09f->f_time =3D now; =09=09verbosef("\n"); -=09=09v->iov_base =3D "\r\n"; -=09=09v->iov_len =3D 2; -=09=09wallmsg(f, iov, 6); +=09=09set_record_field(iov, RECORD_FIELD_EOL, "\r\n", 2); +=09=09wallmsg(f, iov); =09=09break; =09} /* switch */ =09if (f->f_type !=3D F_FORW_UNKN) @@ -1693,7 +1694,7 @@ void endtty(int sig) *=09world, or a list of approved users. */ =20 -void wallmsg(struct filed *f, struct iovec *iov, size_t iovsz) +void wallmsg(struct filed *f, struct iovec iov[RECORD_FIELD_COUNTS]) { =09char p[sizeof (_PATH_DEV) + UNAMESZ]; =09register int i; @@ -1722,7 +1723,7 @@ void wallmsg(struct filed *f, struct iovec *iov, size= _t iovsz) =20 =09=09(void) snprintf(greetings, sizeof(greetings), =09=09 "\r\n\7Message from syslogd@%s at %.24s ...\r\n", -=09=09=09(char *) iov[2].iov_base, ctime(&now)); +=09=09=09(char *) iov[RECORD_FIELD_HOST].iov_base, ctime(&now)); =09=09len =3D strlen(greetings); =20 =09=09/* scan the user login file */ @@ -1768,9 +1769,10 @@ void wallmsg(struct filed *f, struct iovec *iov, siz= e_t iovsz) =09=09=09=09if (ttyf >=3D 0) { =09=09=09=09=09struct stat statb; =20 -=09=09=09=09=09if (fstat(ttyf, &statb) =3D=3D 0 && -=09=09=09=09=09 (statb.st_mode & S_IWRITE)) -=09=09=09=09=09=09(void) writev(ttyf, iov, iovsz); +=09=09=09=09=09if (!fstat(ttyf, &statb) && (statb.st_mode & S_IWRITE)) { +=09=09=09=09=09=09if (writev(ttyf, iov, RECORD_FIELD_COUNTS) < 0) +=09=09=09=09=09=09=09errno =3D 0; /* ignore */ +=09=09=09=09=09} =09=09=09=09=09close(ttyf); =09=09=09=09=09ttyf =3D -1; =09=09=09=09} --=20 2.25.4