Operating System - HP-UX
1753818 Members
8974 Online
108805 Solutions
New Discussion

Facing problem with popen() system call in HP UX

 
meekris
Occasional Visitor

Facing problem with popen() system call in HP UX

Hi All,

 

I am facing a problem with popen() system call on HP platform.

   System information :
   "uname -a" -
   HP-UX XXXXXX B.11.31 U ia64 XXXXXXXXXXXX

 

Problem description :
I have written a program which sends a mail.
I am using popen() to achieve this.

 

The idea is to open a pipe between sendmail and popen() calling process and then calling process is writing mail content on one end of pipe, closing pipe from that end and then "sendmail" is expected to read the content and send the mail.

 

       file_p = popen("/usr/lib/sendmail -t >/dev/null 2>/dev/null", "w");
      (Sorry, because of some reason I can’t copy whole code here)

 

When I run program, popen() system call successfully returns a pointer to the FILE structure for the opened stream but it doesnt send mail.
Also, I dont see any "sendmail" related message in /var/adm/syslog/mail.log file after executing program.

 

But If I run following command on shell -
   # echo "hello" | sendmail -v xxxx@xxxx.xxx
then I receive a mail and I can also see some "sendmail" related messages in /var/adm/syslog/mail.log file.

 

This forced me to conclude that popen() in the program is not able to run "sendmail" command.

 

Then I thought to check the trace of system call using "tusc".

 

Following is the output of "tusc" -

07/11/14 01:13:28 AM [939] pipe() ............................................................................................................ = 11 (12)
07/11/14 01:13:28 AM [939] fcntl(12, F_GETFD, 0) ............................................................................................. = 0
07/11/14 01:13:28 AM [939] fork() ............................................................................................................ = 1215
07/11/14 01:13:28 AM [1215] fork() .........................................................................(returning as child ...) ......... = 939
07/11/14 01:13:28 AM [1215] close(12) ........................................................................................................ = 0
07/11/14 01:13:28 AM [939] close(11) ......................................................................................................... = 0
07/11/14 01:13:28 AM [1215] close(0) ......................................................................................................... = 0
07/11/14 01:13:28 AM [1215] fcntl(11, F_DUPFD, 0) ............................................................................................ = 0
07/11/14 01:13:28 AM [1215] close(11) ........................................................................................................ = 0
07/11/14 01:13:28 AM [1215] execve("/usr/bin/sh", 0x9ffffffffffe98d0, 0x600000000009e860) .................................................... = 0 [32-bit]
07/11/14 01:13:28 AM [1215] mmap(NULL, 8192, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) ............................... = 0x7effa000
07/11/14 01:13:28 AM [1215] open("/usr/lib/hpux32/dld.so", O_RDONLY, 0) ...................................................................... = 3
07/11/14 01:13:28 AM [1215] read(3, "7fE L F 0102010101\0\0\0\0\0\0\0".., 1024) .............................................................. = 1024
07/11/14 01:13:28 AM [1215] mmap(NULL, 752624, PROT_READ|PROT_EXEC, MAP_SHARED|MAP_FILE|MAP_SHLIB, 3, 0) ..................................... = 0xc0040000
07/11/14 01:13:28 AM [1215] sysconf(_SC_PAGE_SIZE) ........................................................................................... = 4096
07/11/14 01:13:28 AM [1215] mmap(NULL, 7384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_SHLIB, -1, 0) ............................... = 0x7eff8000
07/11/14 01:13:28 AM [1215] mmap(0x7eff5000, 12120, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FILE|MAP_SHLIB, 3, 786432) ......................... = 0x7eff5000
07/11/14 01:13:28 AM [1215] close(3) ......................................................................................................... = 0
07/11/14 01:13:28 AM [1215] mmap(NULL, 8192, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) ............................... = 0x7eff2000
07/11/14 01:13:28 AM [1215] sysconf(_SC_PAGE_SIZE) ........................................................................................... = 4096
------------------------
------------------------
07/11/14 01:13:28 AM [1215] brk(0x4001cbc0) .................................................................................................. = 0
07/11/14 01:13:28 AM [1215] brk(0x4001ebb0) .................................................................................................. = 0
07/11/14 01:13:28 AM [1215] brk(0x40020000) .................................................................................................. = 0
07/11/14 01:13:28 AM [1215] sigprocmask(SIG_SETMASK, 0x7ffff2e0, 0x7ffff2c0) ................................................................. = 0
                               set: SIGURG
                              oset: SIGHUP|SIGINT|SIGQUIT|SIGILL|SIGTRAP|SIGABRT|SIGEMT|SIGFPE|SIGBUS|SIGSEGV|SIGSYS|SIGPIPE|SIGALRM|SIGTERM|SIGUSR1|SIGUSR2|SIGCLD|SIGPWR|SIGVTALRM|SIGPROF|SIGIO|SIGWINCH|SIGTSTP|SIGCONT|SIGTTIN|SIGTTOU|SIGURG|SIGLOST|_SIGDIL|_SIGXCPU|_SIGXFSZ|SIGRT1|SIGRT2|SIGRT3|SIGRT4|SIGRT5|SIGRT6|SIGRT7|SIGRT8
07/11/14 01:13:28 AM [1215] sigprocmask(SIG_SETMASK, 0x7fffd5b0, 0x7fffd5d0) ................................................................. = 0
                               set: FULL SET
                              oset: SIGURG
------------------------
------------------------
07/11/14 01:13:28 AM [1215] open("/dev/null", O_RDONLY, 05030) ............................................................................... = 3
07/11/14 01:13:28 AM [1215] close(3) ......................................................................................................... = 0
07/11/14 01:13:28 AM [1215] getuid() ......................................................................................................... = 0 (0)
07/11/14 01:13:28 AM [1215] getuid() ......................................................................................................... = 0 (0)
07/11/14 01:13:28 AM [1215] getgid() ......................................................................................................... = 3 (3)
07/11/14 01:13:28 AM [1215] getgid() ......................................................................................................... = 3 (3)
07/11/14 01:13:28 AM [1215] access("/var/tmp", F_OK) ......................................................................................... = 0
07/11/14 01:13:28 AM [1215] time(0x40016898) ................................................................................................. = 1405062808
                              date: Fri Jul 11 01:13:28 2014
07/11/14 01:13:28 AM [1215] sigprocmask(SIG_SETMASK, 0x7ffff2a0, 0x7ffff2c0) ................................................................. = 0
                               set: FULL SET
                              oset: SIGURG
07/11/14 01:13:28 AM [1215] sigprocmask(SIG_SETMASK, 0x7ffff2c0, 0x7ffff2a0) ................................................................. = 0
                               set: SIGURG
                              oset: SIGHUP|SIGINT|SIGQUIT|SIGILL|SIGTRAP|SIGABRT|SIGEMT|SIGFPE|SIGBUS|SIGSEGV|SIGSYS|SIGPIPE|SIGALRM|SIGTERM|SIGUSR1|SIGUSR2|SIGCLD|SIGPWR|SIGVTALRM|SIGPROF|SIGIO|SIGWINCH|SIGTSTP|SIGCONT|SIGTTIN|SIGTTOU|SIGURG|SIGLOST|_SIGDIL|_SIGXCPU|_SIGXFSZ|SIGRT1|SIGRT2|SIGRT3|SIGRT4|SIGRT5|SIGRT6|SIGRT7|SIGRT8
------------------------
------------------------
07/11/14 01:13:28 AM [1215] sigvector(SIGABRT, 0x7ffff240, 0x7ffff250) ....................................................................... = 0
                    vec.sv_handler: 0x7efd6848
                       vec.sv_mask: EMPTY SET
                      vec.sv_flags: SV_RESETHAND
                   ovec.sv_handler: SIG_DFL
                      ovec.sv_mask: SIGABRT
                     ovec.sv_flags: 0
07/11/14 01:13:28 AM [1215] sigvector(SIGALRM, 0x7ffff240, 0x7ffff250) ....................................................................... = 0
                    vec.sv_handler: 0x7efd6838
                       vec.sv_mask: EMPTY SET
                      vec.sv_flags: SV_RESETHAND
                   ovec.sv_handler: SIG_DFL
                      ovec.sv_mask: SIGALRM
                     ovec.sv_flags: 0
------------------------
------------------------
07/11/14 01:13:28 AM [1215] sigprocmask(SIG_SETMASK, 0x7ffff2d0, 0x7ffff2f0) ................................................................. = 0
                               set: FULL SET
                              oset: SIGWINCH|SIGURG
07/11/14 01:13:28 AM [1215] sigprocmask(SIG_SETMASK, 0x7ffff2f0, 0x7ffff2d0) ................................................................. = 0
                               set: SIGWINCH|SIGURG
                              oset: SIGHUP|SIGINT|SIGQUIT|SIGILL|SIGTRAP|SIGABRT|SIGEMT|SIGFPE|SIGBUS|SIGSEGV|SIGSYS|SIGPIPE|SIGALRM|SIGTERM|SIGUSR1|SIGUSR2|SIGCLD|SIGPWR|SIGVTALRM|SIGPROF|SIGIO|SIGWINCH|SIGTSTP|SIGCONT|SIGTTIN|SIGTTOU|SIGURG|SIGLOST|_SIGDIL|_SIGXCPU|_SIGXFSZ|SIGRT1|SIGRT2|SIGRT3|SIGRT4|SIGRT5|SIGRT6|SIGRT7|SIGRT8
------------------------
------------------------
07/11/14 01:13:28 AM [1215] sigprocmask(SIG_SETMASK, 0x7ffff300, NULL) ....................................................................... = 0
                               set: SIGWINCH|SIGURG
                              oset: NULL
07/11/14 01:13:28 AM [1215] open("/usr/lib/nls/msg/C/psh.cat", O_RDONLY, 02720) .............................................................. = 3
07/11/14 01:13:28 AM [1215] fstat(3, 0x7fffe5e0) ............................................................................................. = 0
07/11/14 01:13:28 AM [1215] fcntl(3, F_SETFD, 1) ............................................................................................. = 0
07/11/14 01:13:28 AM [1215] lseek(3, 0, SEEK_SET) ............................................................................................ = 0
07/11/14 01:13:28 AM [1215] read(3, "m s g c a t 0 1 \0\0\0a3\001\001".., 1212) .............................................................. = 1212
07/11/14 01:13:28 AM [1215] lseek(3, 4904, SEEK_SET) ......................................................................................... = 4904
07/11/14 01:13:28 AM [1215] read(3, "S y n t a x   e r r o r ", 12) .......................................................................... = 12
07/11/14 01:13:28 AM [1215] sigprocmask(SIG_SETMASK, 0x7ffff270, 0x7ffff290) ................................................................. = 0
                               set: FULL SET
                              oset: SIGWINCH|SIGURG
------------------------
------------------------
07/11/14 01:13:28 AM [1215] write(2, "\ :   S y n t a x   e r r o r \n", 16) ................................................................. = 16
07/11/14 01:13:28 AM [1215] sigprocmask(SIG_SETMASK, 0x7ffff280, 0x7ffff2a0) ................................................................. = 0
                               set: FULL SET
                              oset: SIGWINCH|SIGURG
07/11/14 01:13:28 AM [1215] sigprocmask(SIG_SETMASK, 0x7ffff2a0, 0x7ffff280) ................................................................. = 0
                               set: SIGWINCH|SIGURG
                              oset: SIGHUP|SIGINT|SIGQUIT|SIGILL|SIGTRAP|SIGABRT|SIGEMT|SIGFPE|SIGBUS|SIGSEGV|SIGSYS|SIGPIPE|SIGALRM|SIGTERM|SIGUSR1|SIGUSR2|SIGCLD|SIGPWR|SIGVTALRM|SIGPROF|SIGIO|SIGWINCH|SIGTSTP|SIGCONT|SIGTTIN|SIGTTOU|SIGURG|SIGLOST|_SIGDIL|_SIGXCPU|_SIGXFSZ|SIGRT1|SIGRT2|SIGRT3|SIGRT4|SIGRT5|SIGRT6|SIGRT7|SIGRT8
07/11/14 01:13:28 AM [1215] exit(1) .......................................................................................................... WIFEXITED(1)

 

Here I can see that popen() is trying to execve("/usr/bin/sh", ....) shell and then its failing because of some reason.
It never runs "sendmail" command in the shell.


The "tusc" output shows some "syntax error" message -

 

open("/usr/lib/nls/msg/C/psh.cat", O_RDONLY, 02720)
read(3, "m s g c a t 0 1 \0\0\0a3\001\001".., 1212)
read(3, "S y n t a x   e r r o r ", 12)
write(2, "\ :   S y n t a x   e r r o r \n", 16)

 

I really do not understand what is the problem occurring with popen()?
If there is some problem occurring with popen(), then why its returning a pointer to opened stream successfully instead of an error?

 

NOTE : The same program is running and sending mail successfully on other UNIX flavors except HP.

 

P.S. This thread has been moved from HP-UX>General to  HP-UX > languages. -HP Forum Moderator

 

 

3 REPLIES 3
Dennis Handly
Acclaimed Contributor

Re: Facing problem with popen(3) on HP-UX

sendmail is in /usr/sbin/.  Why don't you use mailx instead?

Please use the tusc options -fp.  Remove -t and add -ea.

Also redirect stdout & stderr to a file so you can see errors.

 

>If there is some problem occurring with popen(), then why it's returning a pointer to opened stream successfully ...?

 

The problem is with sh and your input command, not with popen(3).

 

meekris
Occasional Visitor

Re: Facing problem with popen() system call in HP-UX

sendmail is in /usr/sbin/

  On my system i am having - 
    # ls -l /usr/lib/sendmail
    lr-xr-xr-x   1 bin        bin             18 May 28 19:59 /usr/lib/sendmail -> /usr/sbin/sendmail

   So, it doesnt make any difference if I use "/usr/lib/sendmail" or "/usr/sbin/sendmail"

 

Why don't you use mailx instead?
   Problem is not with "sendmail" rather its popen() which is dying before calling "sendmail".

 

tusc command I am using is -

   tusc -T "%D %r" -o outfile -v -f -p

 

Also redirect stdout & stderr to a file so you can see errors.

   I had already tried it.
   I dont see any error message in the file in which I had redirected the stdout & stderr. The file was empty.

 

The problem is with sh and your input command, not with popen(3).

   If the problem is with the input command than why its working fine on other unix flavors!!

 

Dennis Handly
Acclaimed Contributor

Re: Facing problem with popen(3) on HP-UX

>it doesn't make any difference if I use "/usr/lib/sendmail" or "/usr/sbin/sendmail"

 

Yes but you shouldn't using Tlinks.

 

>Problem is not with "sendmail" rather its popen() which is dying before calling "sendmail".

 

I have no problems using popen(3) with /usr/sbin/sendmail on 11.31:

#include <stdio.h>
#include <string.h>
#include <errno.h>

int main() {
   FILE *p = popen("/usr/sbin/sendmail -t", "w");
   if (!p) {
      fprintf(stderr, "Error on popen: (%d), %s\n", errno, strerror(errno));
      return 1;
   }
   fprintf(p, "From: foo@abcdef.com\n");
   fprintf(p, "To:  bar@abcdef.com\n");
   fprintf(p, "Subject: test sendmail\n");
   fprintf(p, "\n");
   fprintf(p, "dummy body\n");
   pclose(p);
}

This also worked:

FILE *p = popen("/usr/sbin/sendmail -t >/dev/null 2>/dev/null", "w");

 

Using tusc, I get a warning but the mail is sent:

$ tusc -fp -ea -o tusc.out a.out
NOTE: /usr/sbin/sendmail: losing setuid/setgid privileges when tracing

Using -ea shows the execve args:

[14598] execve(0x60000000c0350a30, 0x200000007fffe2f0, 0x7fffebd0) [entry]
                              argv[0] @ 0xffffffffc0350a3c:
                              argv[1] @ 0xffffffffc0350a40:
                              argv[2] @ 0x4000890: "/usr/sbin/sendmail -t"
                               env[0] @ 0x7fffeca6: "_=/usr/local/bin/tusc"
                               env[1] @ 0x7fffecbc: "host=foobar"
                               env[2] @ 0x7fffecc7: "MANPATH=/usr/share/man/%L:/ ...

[14598] execve("/usr/bin/sh", 0x7fffe2f0, 0x7fffebd0) .... = 0 [32-bit]

...

[14599] execve(0x400252e0, 0x40025300, 0x400785b0) ....... [entry]
                              argv[0] @ 0x40025320: "/usr/sbin/sendmail"
                              argv[1] @ 0x40025340: "-t"
                               env[0] @ 0x40025360: "_=/usr/sbin/sendmail"
[14598] vfork() .......................................... = 14599
[14599] execve("/usr/sbin/sendmail", 0x40025300, 0x400785b0) = 0 [32-bit]


And /usr/lib/sendmail also works for me.

 

>tusc -T "%D %r" -o outfile -v -f -p

 

This -T and -v just clutters things up and I can't see the forest for the trees.

 

I do see the same type of error using /usr/lib/nls/msg/C/psh.cat if I have bad syntax.

 

>I dont see any error message in the file in which I had redirected the stdout & stderr. The file was empty.

 

I would have expected that "Syntax error" message.

Though I'm wondering why the message had: "\ :   S y n t a x   e r r o r \n", 16)

A backslash instead of "sh: ".

 

>If the problem is with the input command than why it's working fine on other unix flavors!!

 

It works for me.  :-)

My libc is from: PATCH_11.31/PHCO_41529

And sh: PATCH_11.31 PHCO_41399

 

Using tusc -ea will show the sh command line and variables.  I suppose it could be one of the latter?