/*PRINTFLIKE4*/ static void wrtmsg(int severity, char *action, char *tag, char *text, ...) { int errorflg; /* TRUE if problem generating message */ va_list argp; /* Pointer into vararg list */ /* No problems yet */ errorflg = FALSE; /* Generate the error message */ va_start(argp, text); if (text != MM_NULLTXT) { errorflg = vsnprintf(msgbuf, MM_MXTXTLN, text, argp) > MM_MXTXTLN; } (void) fmtmsg(MM_PRINT, fcnlbl, severity, (text == MM_NULLTXT) ? MM_NULLTXT : msgbuf, action, tag); va_end(argp); /* * If there was a buffer overflow generating the error message, * write a message and quit (things are probably corrupt in the * static data space now */ if (errorflg) { (void) fmtmsg(MM_PRINT, fcnlbl, MM_WARNING, gettext("Internal message buffer overflow"), MM_NULLACT, MM_NULLTAG); exit(100); } }
int main (void) { addseverity (5, "NOTE2"); fmtmsg (MM_PRINT, "only1field", MM_INFO, "text2", "action2", "tag2"); fmtmsg (MM_PRINT, "UX:cat", 5, "invalid syntax", "refer to manual", "UX:cat:001"); fmtmsg (MM_PRINT, "label:foo", 6, "text", "action", "tag"); return 0; }
void a_syslog (int prio, const char *fmt, ...) { FILE *fP; int prim = prio & LOG_PRIMASK; va_list val; int i; char tmpbuf[256]; switch (prim) { case LOG_DEBUG: if (verbose <= 1) return; /* DEBUG only if verbose > 1 */ break; case LOG_INFO: if (verbose == 0) return; /* INFO only if verbose */ break; } va_start(val, fmt); if (prim <= LOG_NOTICE) { fP = stderr; vsprintf(tmpbuf, fmt, val); i = fmtmsg (MM_SOFT|MM_APPL|MM_CONSOLE|MM_NRECOV, "SUNWamsvc:amunixd", MM_ERROR, tmpbuf, "Restart Access Manager", "No documentation"); } else fP = stdout; vfprintf(fP, fmt, val); va_end(val); fputc('\n', fP); }
void main() { char *msg; msg = fmtmsg( "%s %d %s", "Failed", 100, "times" ); printf( "%s\n", msg ); }
/* * fsm_rtermreq - Receive Terminate-Req. */ static void fsm_rtermreq(fsm *f, int id, u_char *p, int len) { char str[80]; FSMDEBUG((LOG_INFO, "fsm_rtermreq(%s): Rcvd id %d.", PROTO_NAME(f), id)); switch (f->state) { case ACKRCVD: case ACKSENT: f->state = REQSENT; /* Start over but keep trying */ break; case OPENED: if (len > 0) { fmtmsg(str, sizeof(str), "%0.*v", len, p); syslog(LOG_INFO, "%s terminated by peer (%s)", PROTO_NAME(f), str); } else syslog(LOG_INFO, "%s terminated by peer", PROTO_NAME(f)); if (f->callbacks->down) (*f->callbacks->down)(f); /* Inform upper layers */ f->retransmits = 0; f->state = STOPPING; TIMEOUT(fsm_timeout, f, f->timeouttime); break; } fsm_sdata(f, TERMACK, id, NULL, 0); }
void testValues() { f = 2; int result = fmtmsg(MM_NULLMC, MM_NULLLBL, MM_NULLSEV, MM_NULLTXT, MM_NULLACT, MM_NULLTAG); //@ assert result == MM_OK || result == MM_NOTOK || result == MM_NOMSG || result == MM_NOCON; //@ assert f == 2; //@ assert vacuous: \false; }
static int do_test (void) { /* Ugly, but fmtmsg would otherwise print to stderr which we do not want. */ fclose (stderr); stderr = stdout; int e1; e1 = fmtmsg (MM_PRINT, "label:part", MM_WARNING, "text", "action", "tag"); int e2; e2 = fmtmsg (MM_PRINT, "label2:part2", 11, "text2", "action2", "tag2"); addseverity (10, "additional severity"); int e3; e3 = fmtmsg (MM_PRINT, "label3:part3", 10, "text3", "action3", "tag3"); return e1 != 0 || e2 != 0 || e3 != 0; }
/*--------------------------------------------------------------*/ int main(int argc, char *argv[]) { int fd, ret_val; FILE *fp; setup(); /* temp file is now open */ /*--------------------------------------------------------------*/ blenter(); /* Check that system SEV_LEVEL output is correct */ close(2); /* redirect stderr to file */ fd = creat("fmtfile", 0644); ret_val = fmtmsg(MM_PRINT | MM_SOFT, "LTP:fmtmsg", MM_INFO, "LTP fmtmsg() test1 message, NOT an error", "This is correct output, no action needed", "LTP:msg:001"); close(fd); if (ret_val != 0) { fprintf(temp, "fmtmsg returned %d, expected 0\n\n", ret_val); local_flag = FAILED; } fp = fopen("fmtfile", "r"); clearbuf(); fread(buf, sizeof(buf[0]), strlen(str1), fp); if (strcmp(str1, buf) != 0) { fprintf(temp, "Expected string: %s\n", str1); fprintf(temp, "does not match\n"); fprintf(temp, "received string: %s\n\n", buf); local_flag = FAILED; } /* Read past spaces in output */ fread(&ch, sizeof(ch), 1, fp); while (isspace(ch)) fread(&ch, sizeof(ch), 1, fp); ungetc(ch, fp); clearbuf(); fread(buf, sizeof(buf[0]), strlen(str2), fp); fclose(fp); if (strcmp(str2, buf) != 0) { fprintf(temp, "Expected string: %s\n", str2); fprintf(temp, "does not match\n"); fprintf(temp, "received string: %s\n\n", buf); local_flag = FAILED; } blexit(); /*--------------------------------------------------------------*/ blenter(); /* Check that a system defined SEV_LEVEL cannot get redefined */ ret_val = addseverity(3, "INVALID"); if (ret_val != MM_NOTOK) { fprintf(temp, "addseverity returned %d, expected MM_NOTOK\n", ret_val); local_flag = FAILED; } blexit(); /*--------------------------------------------------------------*/ blenter(); /* Check that we can define our own */ /* SEV_LEVEL and output is correct */ ret_val = addseverity(5, "LTP_TEST"); if (ret_val != MM_OK) { fprintf(temp, "addseverity returned %d, expected MM_OK\n", ret_val); local_flag = FAILED; } close(2); /* redirect stderr to file */ fd = creat("fmtfile", 0644); ret_val = fmtmsg(MM_PRINT | MM_HARD | MM_OPSYS, "LTP:fmtmsg", 5, "LTP fmtmsg() test2 message, NOT an error", "This is correct output, no action needed", "LTP:msg:002"); close(fd); if (ret_val != 0) { fprintf(temp, "fmtmsg returned %d, expected 0\n", ret_val); local_flag = FAILED; } fp = fopen("fmtfile", "r"); clearbuf(); fread(buf, sizeof(buf[0]), strlen(str3), fp); if (strcmp(str3, buf) != 0) { fprintf(temp, "Expected string: %s\n", str3); fprintf(temp, "does not match\n"); fprintf(temp, "received string: %s\n\n", buf); local_flag = FAILED; } /* Read past spaces in output */ fread(&ch, sizeof(ch), 1, fp); while (isspace(ch)) fread(&ch, sizeof(ch), 1, fp); ungetc(ch, fp); clearbuf(); fread(buf, sizeof(buf[0]), strlen(str4), fp); if (strcmp(str4, buf) != 0) { fprintf(temp, "Expected string: %s\n", str4); fprintf(temp, "does not match\n"); fprintf(temp, "received string: %s\n\n", buf); local_flag = FAILED; } fclose(fp); remove("fmtfile"); blexit(); /*--------------------------------------------------------------*/ blenter(); /* Test result of writing to /dev/console */ ret_val = fmtmsg(MM_CONSOLE | MM_HARD | MM_OPSYS, "LTP:fmtmsg", 5, "LTP fmtmsg() test3 message, NOT an error", "This is correct output, no action needed", "LTP:msg:003"); if (ret_val != MM_OK) { fprintf(temp, "fmtmsg returned %d, expected MM_OK\n", ret_val); fprintf(temp, "failed to write to console\n\n"); local_flag = FAILED; } blexit(); /*--------------------------------------------------------------*/ /* Clean up any files created by test before call to anyfail. */ anyfail(); /* THIS CALL DOES NOT RETURN - EXITS!! */ tst_exit(); }
static char * run_test(long classification, const char *label, int severity, const char *text, const char *action, const char *tag) { int pip[2]; pid_t pid, wpid; char *result, *p; size_t resultsize; ssize_t n; int status; if (pipe(pip) == -1) err(2, "pipe"); pid = fork(); if (pid == -1) err(2, "fork"); if (pid == 0) { close(pip[0]); if (pip[1] != STDERR_FILENO && dup2(pip[1], STDERR_FILENO) == -1) _exit(2); if (fmtmsg(classification, label, severity, text, action, tag) != MM_OK) _exit(1); else _exit(0); } close(pip[1]); resultsize = 1024; result = malloc(resultsize); p = result; while ((n = read(pip[0], p, result + resultsize - p - 1)) != 0) { if (n == -1) { if (errno == EINTR) continue; else err(2, "read"); } p += n; if (result + resultsize == p - 1) { resultsize *= 2; result = realloc(result, resultsize); if (result == NULL) err(2, "realloc"); } } if (memchr(result, '\0', p - result) != NULL) { free(result); return (NULL); } *p = '\0'; close(pip[0]); while ((wpid = waitpid(pid, &status, 0)) == -1 && errno == EINTR) ; if (wpid == -1) err(2, "waitpid"); if (status != 0) { free(result); return (NULL); } return (result); }
int zmain (void) { int result = 0; mtrace (); char TEST[] = "ABCD"; if (addseverity (MM_TEST, TEST) != MM_OK) { puts ("addseverity failed"); result = 1; } strcpy (TEST, "TEST"); if (fmtmsg (MM_PRINT, "GLIBC:tst-fmtmsg", MM_HALT, "halt", "should print message for MM_HALT", "GLIBC:tst-fmtmsg:1") != MM_OK) result = 1; if (fmtmsg (MM_PRINT, "GLIBC:tst-fmtmsg", MM_ERROR, "halt", "should print message for MM_ERROR", "GLIBC:tst-fmtmsg:2") != MM_OK) result = 1; if (fmtmsg (MM_PRINT, "GLIBC:tst-fmtmsg", MM_WARNING, "halt", "should print message for MM_WARNING", "GLIBC:tst-fmtmsg:3") != MM_OK) result = 1; if (fmtmsg (MM_PRINT, "GLIBC:tst-fmtmsg", MM_INFO, "halt", "should print message for MM_INFO", "GLIBC:tst-fmtmsg:4") != MM_OK) result = 1; if (fmtmsg (MM_PRINT, "GLIBC:tst-fmtmsg", MM_NOSEV, "halt", "should print message for MM_NOSEV", "GLIBC:tst-fmtmsg:5") != MM_OK) result = 1; if (fmtmsg (MM_PRINT, "GLIBC:tst-fmtmsg", MM_TEST, "halt", "should print message for MM_TEST", "GLIBC:tst-fmtmsg:6") != MM_OK) result = 1; if (addseverity (MM_TEST, NULL) != MM_OK) { puts ("second addseverity failed"); result = 1; } if (addseverity (MM_TEST, NULL) != MM_NOTOK) { puts ("third addseverity unexpectedly succeeded"); result = 1; } char *p = strdup ("TEST2"); if (addseverity (MM_TEST, p) != MM_OK) { puts ("fourth addseverity failed"); result = 1; } if (addseverity (MM_TEST, "TEST3") != MM_OK) { puts ("fifth addseverity failed"); result = 1; } free (p); return result; }
void runFailure() { const char *label, text, action, tag; fmtmsg(MM_NULLMC, label, MM_NULLSEV, text, action, tag); }
void runSuccess() { fmtmsg(MM_NULLMC, MM_NULLLBL, MM_NULLSEV, MM_NULLTXT, MM_NULLACT, MM_NULLTAG); }