This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Problem setenv/putenv for msgget


I 'm using CYGWIN (the last version 1.5.19.4). My application use the IPC messages queue.

The cygserver is started correctly.
The application server side (is a service) is launched by cygrunsrv ... with -e CYGWIN=server , it is OK
If I launch the application client in a shell script, it is OK if I set before launching the application, the CYGWIN variable to server. It's normal.


My problem is :
----------------------
- The application client is a CGI written in C, launched by Apache or IIS , and I don't know how to set the CYGWIN variable before the application is launched.If I use Apache I can use the SetEnv directive to set a variable in the Apache configuration file. But with IIS I don't know to do that. I need a global solution for Apache and IIS.


So , I tried to set the environment variable CYGWIN to server with the routines setenv(), putenv(), SetEnvironmentVariable() inside the CGI code. I have always the "Bad system call " error when the program executes msgget. It seems that the setenv function doesn't run while the getenv function returns the right value !!

I wrote a test program to reproduce the problem:
   - rcv_msg.c waits for messages
   - send_msg.c sends messages
The test programs are launched in a shell window.

_Test NOK
_The variable CYGWIN is set in the shell for the server rcv_msg .
The variable CYGWIN is set inside the send_msg.c program (setenv routine)
The file /tmp/xx must exist. The programs are launched in 2 different shell windows


/$/ */export CYGWIN=server/*
/$ ./rcv_msg.exe /tmp/xx
Call ftok /tmp/xx
command_file=/tmp/xx,key=16068417
Call msgget
Return msgget 3538944
Loop msgrcv

$ *unset CYGWIN*
$ ./send_msg.exe /tmp/xx AAA
Call getenv CYGWIN
Value returned by getenv CYGWIN =(null)
Call setenv CYGWIN=server
Value returned by setenv CYGWIN result=0
Call getenv CYGWIN
Value returned by getenv CYGWIN =server
Call msgget
Bad system call


/_Test OK (CYGWIN variable set in the shell for the server and client application ): _
/$/ */export CYGWIN=server/*
/$ ./rcv_msg.exe /tmp/xx
Call ftok /tmp/xx
command_file=/tmp/xx,key=16068417
Call msgget
Return msgget 3538944
Loop msgrcv
Message=AAA


//$/* /export CYGWIN=server/*
/$ ./send_msg.exe /tmp/xx AAA
Call getenv CYGWIN
Value returned by getenv CYGWIN =(null)
Call setenv CYGWIN=server
Value returned by setenv CYGWIN result=0
Call getenv CYGWIN
Value returned by getenv CYGWIN =server
Call msgget/

_Note:_ I used ipc-daemon in my previous version, and it was OK. There was no environment variable to set.

Thanks to help me.
Martine

--
Martine Carannante
System Software Development R&D


Bull, Architect of an Open World TM

Tél : (+33) 1 30 80 71 87
http://www.bull.com


#include <stdio.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int      message_queue_id;

#define MAX_INPUT_BUFFER 1024

int main(int argc, char **argv){
	char buffer[1024];
	char command_file[1024];
 	int result;
	struct My_msgbuf {
	  long mtype;
	  char mtext[1024];
	}rbuf  ;
	/*struct msgbuf rbuf;*/

	if (argc < 2)
	  {
	    printf("Key (existing)file argument missing\n");
	    printf("Usage: rcv_msg <file for the key>\n"); 
	    exit(-1);
	  }
	strcpy(command_file,argv[1]);

	result=open_command_file(command_file);
	if (result != 0) exit(-1);
 
	printf("Loop msgrcv\n");
	while (result=msgrcv(message_queue_id,&rbuf,MAX_INPUT_BUFFER,0,0) != -1)
	  {

	    printf("Message=%s\n",rbuf.mtext);
	
	  }
}


int open_command_file(char * command_file) 
{
  key_t key;
 int result;

  result=open(command_file,O_CREAT|O_RDWR,S_IWUSR);
  printf("Call ftok %s\n",command_file);
  key=ftok(command_file,'A');
  printf("command_file=%s,key=%d\n",command_file,key); 
  if (key == -1){
    perror("Ftok\n");
    return -1;
      }
  printf("Call msgget \n");
  message_queue_id = msgget(key,(IPC_CREAT|IPC_EXCL|0666));
  printf("Return msgget %d \n",message_queue_id);
  if (message_queue_id == -1){
      perror("msgget\n");
      return -1;
	}
  return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define MAX_FILENAME_LENGTH 1024
#define MAX_INPUT_BUFFER 1024
#define OK  0
extern char ** environ;

#define ERROR -1

char command_file[MAX_FILENAME_LENGTH];
char command_line[MAX_INPUT_BUFFER];


/* write a command entry to the command file */
int main(int argc, char **argv){
        FILE *fp;
        int message_queue_id;
        key_t key;
	/*struct msgbuf sbuf;*/
	struct msgbuf {
		long mtype;
		char mtext[1024];
	}sbuf;
	int result;
	char *value;


	if (argc < 3)
	{
		printf("Argument missing");
		printf("Usage : send_msg <Key existing file> <message>\n");
		exit(ERROR);
	}
	
	/* get the command line argument*/

	strcpy(command_file,argv[1]);
	strcpy(command_line,argv[2]);
        key=ftok(command_file,'A');

	printf("Call getenv CYGWIN\n");
	value=getenv("CYGWIN");
	printf("Value returned by getenv CYGWIN =%s\n",value);
	printf("Call setenv CYGWIN=server\n");
	result=setenv("CYGWIN","server",1);
	printf("Value returned by setenv CYGWIN result=%d\n",result);
	printf("Call getenv CYGWIN\n");
	value=getenv("CYGWIN");
	printf("Value returned by getenv CYGWIN =%s\n",value);

	/*SetEnvironmentVariable("CYGWIN","server");*/
	

        /* open the command for writing (since this is a pipe, it will really be appended) */
	printf("Call msgget\n");
        message_queue_id = msgget(key,0666);
        if (message_queue_id == -1)
        {
                perror("Error: msgget 0666  !\n");
		printf("command_file =%s key=%d\n",command_file,key);
                return ERROR;
        }

        sbuf.mtype=1;
        strcpy(sbuf.mtext,command_line);

        if(msgsnd(message_queue_id,&sbuf,strlen(sbuf.mtext)+1,IPC_NOWAIT) <0){

                perror("Error: msgsnd!\n");
                return ERROR;
                }

	return OK;
}


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]