static plc_sProcess * init_process () { plc_sProcess *pp; pwr_tStatus sts = PLC__SUCCESS; #if 0 thread_SetPrio(NULL, 15); #endif errh_Init("pwr_plc", errh_eAnix_plc); errh_SetStatus( PWR__SRVSTARTUP); pp = (plc_sProcess *) calloc(1, sizeof(*pp)); if (pp == NULL) { errh_Fatal("Out of virtual memory"); exit(0); } sts = gdh_Init("pwr_plc"); if (EVEN(sts)) { errh_Fatal("gdh_Init, %m", sts); errh_SetStatus( PWR__SRVTERM); exit(sts); } #if defined OS_VMS qdb->thread_lock.isThreaded = 1; qdb->thread_lock.cond_signal = thread_CondSignal; qdb->thread_lock.cond_wait = thread_CondWait; #endif qcom_CreateQ(&sts, &pp->eventQ, NULL, "plcEvent"); if (EVEN(sts)) { errh_Fatal("qcom_CreateQ(eventQ), %m", sts); errh_SetStatus( PWR__SRVTERM); exit(sts); } sts = thread_MutexInit(&pp->io_copy_mutex); if (EVEN(sts)) { errh_Fatal("thread_MutexInit(io_copy_mutex), %m", sts); errh_SetStatus( PWR__SRVTERM); exit(sts); } return pp; }
que_sQue * que_Create ( pwr_tStatus *status, que_sQue *qp ) { que_sQue *lqp = NULL; pwr_dStatus(sts, status, QUE__SUCCESS); if (qp == NULL) { lqp = qp = (que_sQue *) calloc(1, sizeof(*qp)); if (lqp == NULL) pwr_Return(lqp, sts, QUE__INSVIRMEM); } thread_CondInit(&qp->cond); thread_MutexInit(&qp->mutex); lst_Init(NULL, &qp->lh, NULL); return qp; }
static pwr_tStatus IoRackInit ( io_tCtx ctx, io_sAgent *ap, io_sRack *rp ) { io_sServerLocal *local; pthread_t thread; pwr_tOName name; pwr_tStatus sts; pwr_sClass_Modbus_TCP_Server *op; int i; unsigned short port; op = (pwr_sClass_Modbus_TCP_Server *) rp->op; op->Connections = 0; port = op->Port == 0 ? 502 : op->Port; sts = gdh_ObjidToName(rp->Objid, (char *) &name, sizeof(name), cdh_mNName); errh_Info( "Init of Modbus TCP Server %s", name); rp->Local = calloc(1, sizeof(io_sServerLocal)); local = rp->Local; if ( op->DisableServer) return IO__SUCCESS; /* Create socket, store in local struct */ uid_t ruid; ruid = getuid(); printf( "ruid: %d\n", ruid); local->s = socket(AF_INET, SOCK_STREAM, 0); if (local->s < 0) { errh_Error( "Error creating socket for IO modbus tcp server %s, %d", rp->Name, local->s); return 0; } local->loc_addr.sin_family = AF_INET; local->loc_addr.sin_port = htons(port); for ( i = 0; i < 10; i++) { sts = bind( local->s, (struct sockaddr *) &local->loc_addr, sizeof(local->loc_addr)); if ( sts == 0) break; perror( "Modbus TCP Bind socket failure, retrying... "); sleep( 10); } if ( sts != 0) { printf( "Modbus TCP Bind socket failure, exiting"); errh_Error( "Error bind socket to port for IO modbus tcp server %s, %d", rp->Name, local->s); return 0; } errh_Info( "Modbus TCP Sever bind to port %d, %s", port, name); sts = listen( local->s, 16); sts = thread_MutexInit( &local->mutex); if ( EVEN(sts)) return sts; /* Create a thread that listens for connections */ sts = pthread_create( &thread, NULL, mb_connect, (void *)rp); if ( sts != 0) return sts; sts = mb_init_channels( ctx, ap, rp); if ( EVEN(sts)) return sts; op->Status = MB__NORMAL; return IO__SUCCESS; }