LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-03-2013, 01:43 PM   #1
methodtwo
Member
 
Registered: May 2007
Posts: 146

Rep: Reputation: 18
Don't know why C program esgfaults after get opt()?


Hi
This is, i think, the relevant part of my C program that segfaults straight after processing options:

Code:
char *usr_nam=0;
char *host=0;

int main(int argc, char *argv[])
{
 int len, l;
 int n=0, c;
 char *cp, *service = "12349";
 char send_auth[128], auth_ok[1];
 int err, err2, zero;
 struct passwd *pwd;  
 sigset_t childmask, oldsigmask;

 memset(&hints, 0, sizeof(hints));
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_family = AF_INET;
 printf("Starting getopt: \n");
 
        opterr = 0;		/* don't want getopt() writing to stderr */
	while ((c = getopt(argc, argv, "h:l:")) != -1) {
		switch (c) {
		case 'l':
			usr_nam = optarg;
                        printf("usr_nam is %s\n", usr_nam); 
			break;
                case 'h':
                        host = optarg;
                        printf("Host is %s\n", host);
                        break;
		
		}
	}
	/*if (optind >= argc) 
		perror("usage: pty [ -d driver -einv ] program [ arg ... ]");*/
   
  argc -= optind;
  argv += optind;

  printf("We are here");
     
  /* We will be writing to sockets so block SIG_PIPE */
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  sa.sa_handler = SIG_IGN;
  if (sigaction(SIGPIPE, &sa, NULL) < 0)
		perror("sigaction failed");
The output that i get from this is:Starting getopt:
Code:
Host is localhost
usr_nam is chuck
Segmentation fault: 11
The code always segfaults whether or not i initialise *host and *usr_nam to NULL or not!
Please could someone let me know or help me regarding why this programming is segfaulting on the printf statement following the option processing code?
Thankx in advance for any help/replies!
 
Old 06-03-2013, 01:49 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
I don't see how you have concluded the seg fault occurs there.

I would put a \n at the end of the string "We are here" before I would conclude the seg fault was not somewhere far past that point. Without the \n, the text is buffered.
 
Old 06-03-2013, 02:31 PM   #3
methodtwo
Member
 
Registered: May 2007
Posts: 146

Original Poster
Rep: Reputation: 18
I put the \n in the printf statement. I also put this after it:

Code:
 if(signal(SIGPIPE, SIG_IGN) == SIG_ERR)
	perror("Can't ignore sigpipe");
  
 childpid = getpid();
 printf("We got as far as here!");
The output was the same. Indicating that the second printf never got executed. Got me puzzled!

output:

Code:
Starting getopt: 
Host is localhost
usr_nam is chuck
usage: pty [ -d driver -einv ] program [ arg ... ]: Undefined error: 0
We are here
Segmentation fault: 11
I also fixed the line with the port number on it to:

Code:
char *cp;
char *service = "12349";
 
Old 06-03-2013, 02:38 PM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by methodtwo View Post
Got me puzzled!
What puzzles me is that apparently you have not bothered to run the program through a debugger. Step through your code, examining variables (particularly pointers) to see if you are attempting to access a null (or uninitialized) pointer.
 
Old 06-03-2013, 03:20 PM   #5
ogod
LQ Newbie
 
Registered: Mar 2009
Posts: 3

Rep: Reputation: 0
I would recommend you always to run the program through valgrind or similar, that is usually really helpful unless you mix e.g. C and Fortran. Furthermore, you have left out the declaration of hints as well. But finally you should allocate space for usr_nam and host an fill these using strcpy or sprintf.
 
Old 06-03-2013, 03:29 PM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by ogod View Post
But finally you should allocate space for usr_nam and host an fill these using strcpy or sprintf.
Not really necessary. Space is already allocated on the stack for the strings which are passed in as args. Assigning a pointer to these strings is ok.
 
Old 06-03-2013, 03:44 PM   #7
methodtwo
Member
 
Registered: May 2007
Posts: 146

Original Poster
Rep: Reputation: 18
I made sockfd a global variable, instead of a member of a global struct. I also put \n in all the printf()s. After this the program progresses a lot further. Just a few more things to fix. Thank you very much for all the replies. Yes i will run it through GDB(i usually do this as a last resort)
Thankx very much
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] I don't see why this C program will not segfault... trist007 Programming 6 08-06-2011 08:31 AM
shell getopts: opt w/ optional parameter is taking next opt as its parameter! GrapefruiTgirl Programming 22 10-27-2010 06:00 AM
Regarding # /opt/CTEact/bin/act & /opt/SUNWexplo/bin/explorer not available for x86 rajaniyer123 Solaris / OpenSolaris 6 04-24-2008 09:09 AM
i don't want the login program alaios Linux - General 6 08-10-2004 02:09 PM
"opt/openoffice.org/program/instdb.ins"can not be read?? masterguardian Linux - Software 1 08-07-2004 12:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration