This is snippet of the code:
const char* programName ="abc-123";
openlog(programName, LOG_PID, LOG_LOCAL7);
char* msg = "2008/10/16 09:32:35 [3986897824]: call back out of
sequence";
syslog(LOG_ERR,"%s", msg );
Oct 16 09:32:35 devhost23 <87>%Ä<84>EQ DÝ[21961]: 2008/10/16
09:32:35 [3986897824]: call back out of sequence
B ident instead of abc-123 becomes "<87>%Ä<84>EQ DÝ". (This one is
not real but when I print the value in std out it is looks good.)
Advice needed. Thanks.
> I have strange behavior in syslog on Red Hat Linux 2.6.
>
> This is snippet of the code:
>
> const char* programName ="abc-123";
> openlog(programName, LOG_PID, LOG_LOCAL7);
> char* msg = "2008/10/16 09:32:35 [3986897824]: call back out of
> sequence";
> syslog(LOG_ERR,"%s", msg );
>
> Oct 16 09:32:35 devhost23 <87>%Ä<84>EQ Dİ[21961]: 2008/10/16
> 09:32:35 [3986897824]: call back out of sequence
>
> B ident instead of abc-123 becomes "<87>%Ä<84>EQ Dİ". (This one is
> not real but when I print the value in std out it is looks good.)
> Advice needed. Thanks.
Is this exactly the code you wrote?
It appears that openlog() saves away the pointer you pass it, but
doesn't copy the string itself. So if that memory gets overwritten
later, syslog() will log the new contents, i.e. garbage.
With what you've written, programName points to the literal string
constant "abc-123" which would normally be somewhere in the program's
read-only data section. So I don't see how it could get changed, unless
something else in your code trashes openlog()'s own internal copy of
that pointer. If you post your complete code, someone might be able to
spot the bug. Another option is to figure out the address of that
pointer (it appears to be a static variable in libc's misc/syslog.c
named LogTag) and use gdb to set watchpoints on the pointer itself as
well as the string it points to.
However, if instead you wrote something like
void do_openlog(void) {
const char programName[] = "abc-123";
openlog(programName, LOG_PID, LOG_LOCAL7);
}
then yours is the behavior I'd expect. In that case you're passing a
pointer to the string "abc-123" on the stack, and after the do_openlog
function returns, the stack is likely to be overwritten with something
else.
If that's not the case, the bug is probably somewhere else in your code,
and you'll have to post something complete that someone could actually
compile and run.
Yes, this is just part of the huge program. For simplicity I chop it
of.
You solution had worked! Thank you.