Re: How to use dbms_pipe (or something else) to execute unix command
Date: Tue, 18 Aug 1998 09:51:59 +0300
Message-ID: <35D9248F.93B94EDA_at_iil.intel.com>
#include <string.h>
#include <stdlib.h>
#include "host_cmd.h"
EXEC SQL INCLUDE sqlca;
int sql_err ();
void main()
{
char usr[15];
char login_str[35];
int pipe_status;
char command[255];
/* Connect to the DB with login and passwd defined in host_cmd.h */
strcpy(usr,LOGIN_USER);
strcpy(login_str,LOGIN_STR);
EXEC SQL WHENEVER SQLERROR DO sql_err();
EXEC SQL CONNECT :login_str;
printf ("--- %s has been conected to DB. ---\n",usr);
printf ("Wainting for commands ...\n");
/* Loop - wait on the pipe (HOST_PIPE), read message from the pipe
and execute it (the message is a command) */
do
{
EXEC SQL EXECUTE
BEGIN /* Try read from the pipe. If pipe doesn't exist, it creates it. */ /* If pipe is empty, it waits (with no time limit). */ :pipe_status := DBMS_PIPE.RECEIVE_MESSAGE ('HOST_PIPE'); DBMS_PIPE.UNPACK_MESSAGE (:command); END;
END-EXEC; /* Execute the command that recieved from the pipe. */ printf ("Command is: %s\n",command); system (command);
} while (1); /* endless loop */
EXEC SQL COMMIT WORK RELEASE;
printf ("\nDisconecting from DB ... \n");
}
/* SQL Errors handler.
On any errors that are not handled before this procedure will be activated. */
int sql_err ()
{
fprintf(stderr,"\nSQL error %.*s\n",
sqlca.sqlerrm.sqlerrml,sqlca.sqlerrm.sqlerrmc);EXEC SQL WHENEVER SQLERROR CONTINUE;
EXEC SQL ROLLBACK RELEASE;
exit(-1);
}
PROCEDURE host_cmd( cmd IN VARCHAR2 )
IS
status NUMBER;
BEGIN
- Prepare the message for sending it through the pipe DBMS_PIPE.PACK_MESSAGE( cmd );
- Send the message to pipe HOST_PIPE. If pipe doesn't exist, it creates it. status := DBMS_PIPE.SEND_MESSAGE( 'HOST_PIPE' );
- check if command completed succesfully
IF ( status <> 0 ) THEN
raise_application_error( -20001, 'Pipe error' );
END IF;
END;
- application/x-unknown-content-type-hfile attachment: host_cmd.h