중간에 fd에 이상한 값이 들어갑니다

cb2531의 이미지

rts기반 서버이고 이러한 루프를 돌리는 스레드가 있습니다. (while앞의 초기화 코드는 생략)

while(S_RUNNING)
	{
		memset(&header,0,sizeof(header));
		pfd.events = POLLIN | POLLPRI;
		pfd.revents = 0;
		pfd.fd = fd;
		
		if (poll(&pfd,1,LINE_WAIT) < 1) {
			continue ; 
		}
		
		FLAG = recv(fd , (char *)&header , (int)sizeof(header),MSG_WAITALL);			
		
		if (FLAG != sizeof(header)) {
			err("헤더 읽기 실패 %s",strerror(errno));
			break;
		}
		header.s_ = fd;

		memset(buf,0,sizeof(buf));
		FLAG = recv(fd, buf , header.len,MSG_WAITALL);
		if (header.len == 0 || header.len > H_MAX  *  8)){
			err("내용 읽기 실패 %s",strerror(errno));
			break;
		}		 
	}
close(fd);

가끔씩(이틀에 한번정도), 주로 접속자가 좀 많아서 처리량이 많을때
서버가 죽길래 gdb로 찾아보니 마지막 if에 걸려서 루프를 빠져나가고
close에서 죽는 것이었습니다. 저 부분 info locals를 해보면
(gdb) info locals
fd = 808519986
FLAG = 9199
header = {s_ = 4123337036523778617, r_ = 3158025, cmd = 771424256, len = 160}
buf = "3171\t1\t00\0\0\0\0\0\0\0E \215\0\0\0\0\0f\0\0\0\017\0\0\0FNF\t3210160\t1\t00\0\0\0\0\0\0\0E \215\0\0\0\0\0f\0\0\0\017\0\0\0FNF\t3416144\t1\t00\0\0\0\0\0\0\0E \215\0\0\0\0\0f\0\0\0\017\0\0\0FNF\t3464403\t1\t00\0\0\0\0\0\0\0E \215\0\0\0\0\0f\0\0\0\017\0\0\0FNF\t3523039\t1\t00\0\0\0\0\0\0\0E \215\0\0\0\0\0f\0\0\0\017\0\0\0FNF\t3734384\t"...
pfd = {fd = 45, events = 3, revents = 1}

pfd의 fd에는 값이 제대로 들어가 있고, 두번째 if에도 안걸리는 것으로 보아 buf를 recv하면서 뭔가가 꼬이는 것 같습니다.
fd에 저런 쓰레기값이 들어가다니..-_-

가장 궁금한건.... 지금껏 쭉 이상이 없었는데 얼마전에 시스템 재부팅 이후로 이 문제가 발생한다는 것입니다 -_-
그럼 코드의 문제가 아닌 것인지..
스레드나 시그널 관련해서 뭔가 설정을 빼먹은 것일까요..
고수분들의 도움을 구합니다

익명 사용자의 이미지

close()에서 죽었다면, 이미 이전에 사망신고가 난것같습니다.
이 경우, header.len의 값이 타당한지(버퍼오버플로우 고려) 검사후, 데이터부분을 읽어들이도록
코드를 수정하시는것이 좋아 보입니다.

      ....
      FLAG = recv(fd, buf , header.len,MSG_WAITALL); 
      if (header.len == 0 || header.len > H_MAX  *  8)){ 
         err("내용 읽기 실패 %s",strerror(errno)); 
         break; 
      } 
      ....

를 다음과 같이 수정해보세요.
(* 가정 buf의 크기가 H_MAX * 8 라고 함 )
      ....
      if (header.len == 0 || header.len > H_MAX  *  8)){ 
         err("내용 읽기 실패 %s",strerror(errno)); 
         break; 
      } 
      FLAG = recv(fd, buf , header.len,MSG_WAITALL); 
      if ( FLAG가 정상인가? ) {
           ....어쩌구...
      }
      ....
익명 사용자의 이미지

심지어, header.len < 0인 경우 처리도 필요한듯합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.