📄 suspend2.c
字号:
void suspend_init(const char *port )
{
#ifndef __OS2__
char szPipe[FILENAME_MAX];
SEL selStack;
PSZ pStack;
TID tid;
APIRET rc;
/*--------------------------------------------------------------------*/
/* Set up the handler for signals from our suspend monitor */
/*--------------------------------------------------------------------*/
rc = DosSetSigHandler(SuspendHandler,
&old,
&nAction,
SIGA_ACCEPT,
SIG_PFLG_A);
if (rc)
{
printOS2error( "DosSetSigHandler", rc);
return;
}
/*--------------------------------------------------------------------*/
/* Set up the pipe name to listen upon */
/*--------------------------------------------------------------------*/
strcpy(szPipe, SUSPEND_PIPE);
portName = newstr( port ); // Save for later reference
strcat(szPipe, port );
printmsg(4,"Creating locking pipe %s", szPipe );
#ifdef __OS2__
rc = DosCreateNPipe( szPipe,
&hPipe,
NP_ACCESS_DUPLEX | NP_NOINHERIT | NP_NOWRITEBEHIND,
NP_WAIT | NP_READMODE_BYTE | NP_TYPE_BYTE | 1,
32,
32,
5000);
if (rc)
{
printOS2error( "DosCreateNPipe", rc);
return;
}
#else
rc = DosMakeNmPipe(szPipe,
&hPipe,
NP_ACCESS_DUPLEX | NP_NOINHERIT | NP_NOWRITEBEHIND,
NP_WAIT | NP_READMODE_BYTE | NP_TYPE_BYTE | 1,
32,
32,
5000);
if (rc)
{
printOS2error( "DosMakeNmPipe", rc);
return;
}
#endif
/*--------------------------------------------------------------------*/
/* Now allocate memory for the monitor thread which will */
/* notify us if some program wants our port. */
/*--------------------------------------------------------------------*/
#ifdef __OS2__
pStack = malloc( STACKSIZE );
#else
rc = DosAllocSeg(STACKSIZE, &selStack, SEG_NONSHARED);
if (rc)
{
printOS2error( "DosAllocSeg", rc);
return;
}
pStack = (PSZ) MAKEP(selStack, 0) + STACKSIZE -2 ;
#endif
/*--------------------------------------------------------------------*/
/* Now fire off the monitor thread */
/*--------------------------------------------------------------------*/
rc = DosCreateThread(SuspendThread, &tid, pStack);
if (rc)
{
printOS2error( "DosCreateThread", rc);
return;
}
/*--------------------------------------------------------------------*/
/* Finally, our signal handler */
/*--------------------------------------------------------------------*/
if ( signal( SIGUSR2, usrhandler ) == SIG_ERR )
{
printmsg( 0, "Couldn't set SIGUSR2\n" );
panic();
}
#endif
} /* suspend_init */
/*--------------------------------------------------------------------*/
/* s u s p e n d _ o t h e r */
/* */
/* Request another UUCICO give up a modem */
/*--------------------------------------------------------------------*/
int suspend_other(const boolean suspend,
const char *port )
{
char szPipe[FILENAME_MAX];
HFILE hPipe;
#ifdef __OS2__
ULONG nAction, nBytes;
#else
USHORT nAction, nBytes;
#endif
UCHAR nChar;
APIRET rc = 1;
boolean firstPass = TRUE;
int result;
strcpy(szPipe, SUSPEND_PIPE);
strcat(szPipe, port );
/*--------------------------------------------------------------------*/
/* Try to open the pipe, with one retry if needed */
/*--------------------------------------------------------------------*/
while(rc)
{
rc = DosOpen((PSZ) szPipe,
&hPipe,
&nAction,
0L,
0,
FILE_OPEN,
OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE,
0);
if (rc)
{
if ( debuglevel >= 4 ) // No error if no passive UUCICO
printOS2error( "DosOpen", rc); // So this is only for info
if ((rc == ERROR_PIPE_BUSY) && firstPass )
{
firstPass = FALSE;
#ifdef __OS2__
rc = DosWaitNPipe( szPipe, 5000 ); // Wait up to 5 sec for pipe
if (rc)
{
printOS2error( "DosWaitNPipe", rc);
return 0;
} /* if (rc) */
#else
rc = DosWaitNmPipe( szPipe, 5000 ); // Wait up to 5 sec for pipe
if (rc)
{
printOS2error( "DosWaitNmPipe", rc);
return 0;
} /* if (rc) */
#endif
} /* if */
else
return 0;
} /* if */
} /* while(rc) */
/*--------------------------------------------------------------------*/
/* We have an open connect, write the request to the server */
/* running as part of the passive UUCICO. */
/*--------------------------------------------------------------------*/
rc = DosWrite(hPipe, suspend ? "S" : "R", 1, &nBytes);
if (rc)
{
printOS2error( "DosWrite", rc);
DosClose(hPipe);
return -1;
}
if ( nBytes != 1 )
{
DosClose(hPipe);
return -1;
}
printmsg(2, "Waiting for background uucico to %s use of %s ...",
suspend ? "suspend" : "resume",
port);
/*--------------------------------------------------------------------*/
/* Process the server response */
/*--------------------------------------------------------------------*/
rc = DosRead(hPipe, &nChar, 1, &nBytes);
if (rc)
{
printOS2error( "DosRead", rc);
result = -1;
}
else if ( nBytes != 1 )
{
printmsg(0,"suspend_other: Error: No data from remote UUCICO");
result = -2;
}
else if ( nChar != 'O' )
{
printmsg(0, "Cannot %s background uucico. Result code was %c",
suspend ? "suspend" : "resume",
nChar );
result = -3;
}
else
result = 1; // Success!
/*--------------------------------------------------------------------*/
/* Close up and return to caller */
/*--------------------------------------------------------------------*/
DosClose(hPipe);
return result;
} /* suspend_other */
/*--------------------------------------------------------------------*/
/* s u s p e n d _ w a i t */
/* */
/* Wait to take the serial port back */
/*--------------------------------------------------------------------*/
CONN_STATE suspend_wait(void)
{
#ifdef __OS2__
return CONN_INITIALIZE;
#else
APIRET rc;
printmsg(0,"suspend_wait: Port %s released, program sleeping",
portName );
#ifdef __OS2__
rc = DosResetEventSem( &semFree, &postCount);
if (rc)
printOS2error( "DosResetEventSem", rc);
rc = DosWaitEventSem(&semWait, SEM_INDEFINITE_WAIT);
if (rc)
printOS2error( "DosWaitEventSem", rc);
#else
rc = DosSemClear(&semFree);
if (rc)
printOS2error( "DosSemClear", rc);
rc = DosSemSetWait(&semWait, SEM_INDEFINITE_WAIT);
if (rc)
printOS2error( "DosSemSetWait", rc);
#endif
if (rc)
return CONN_EXIT;
else
return CONN_INITIALIZE;
#endif
} /* suspend_wait */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -