title: Shared library function does working correctly after first dlopen
body: <p>I am creating a websocket server in C. Since taking a server down, recompiling it, and running it again is counterproductive to what a server app should do, I am looking for ways to dynamically load in my functions so that I can keep the main server app running while being able to alter / create new functions that will be used in it. I created a function that allows me to call a function by name with correct arguments like you would a normal function call, but when I go to call it the second time it does not do the same thing the second time I call it dynamically. To lay out my problem by steps, consider the following situations:</p>
  
  <p><strong>Situation 1</strong></p>
  
  <ol>
  <li>Start server application without calling sendMessage dynamically.</li>
  <li>Connect to the websocket server via browser.</li>
  <li>After successful connection send a message to the server (I use <code>Hello World</code>)</li>
  <li>Server will echo the message the client sent.</li>
  <li>Send the same message to the server again.</li>
  <li>Server will echo message again. (this is when the server sendMessage function is not loaded dynamically</li>
  <li>Repeating steps 5 and 6 will not cause the client to disconnect.</li>
  </ol>
  
  <p>Now for the situation using a dynamic version of the servers sendMessage function to echo the client message.</p>
  
  <p><strong>Situation 2</strong></p>
  
  <ol>
  <li>Start server application while allowing sendMessage to be called using loadFunction.</li>
  <li>Connect to the websocket server via browser.</li>
  <li>After successful connection send a message to the server (again I use <code>Hello World</code>)</li>
  <li>Server will echo the message the client sent like it should.</li>
  <li>Send the same message to the server again.</li>
  <li>This time the server does not echo the message the client sent.</li>
  <li>Sending more messages after the first will eventually cause the connection to end (This is where I am having a problem</li>
  </ol>
  
  <p>Situation 1 is when my function <code>sendMessage</code> is called normally (not through <code>loadFunction</code>) while situation 2 is where I replace <code>sendMessage</code> with my <code>loadFunction</code> call that loads the library holding <code>sendMessage</code>, assigns it to a location function variable (see code) and call the function like it would normally.</p>
  
  <p>I am thinking that the problem lies with the write function in <code>sendMessage</code> when I dynamically load it. But the function works perfectly when I don't load it dynamically which is odd to me.</p>
  
  <p>My question is why is why would my <code>sendMessage</code> function operate differently from when I call it normally and when I call it dynamically? Below is some code and output from both situations</p>
  
  <p><strong>sendMessage.c</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;unistd.h&gt;
  #include "include/structs.h"
  //#include "include/functions.h"
  
  /*
   * sendMessage: this function is used then we want to send message (s)
   *              of length (len) from the server to a client (sock)
   *
   * ARGUMENTS
   * sock:    the socket where we want the message to go
   * s:       A string containing the message we want to send
   * len: the length of the string s
   */
  void *sendMessage(int sock, char *s, int len) {
      int frameCount;
      uint16_t len16;
      char frame[10];
      char *reply =   malloc(sizeof(char) * (len + 8));
  
      frame[0] =  '\x81';
  
      if (len &lt;= 125) {
          frame[1] = len;
          frameCount = 2;
      } else if (len &gt;= 126 &amp;&amp; len &lt;= 65535) {
          frame[1] = 126;
          len16 = htons(len);
          memcpy(frame + 2, &amp;len16, 2);
          frameCount = 4;
      } else {
          frame[1] = 127;
          //NOTE: HAVE NOT FULLY CONFIGURED A MESSAGE OF THIS LENGTH (TODO)
          //frame[2] = (char)( ((char)len &gt;&gt; 56) &amp; (char)255 );
          //frame[3] = (char)( ((char)len &gt;&gt; 48) &amp; (char)255 );
          //frame[4] = (char)( ((char)len &gt;&gt; 40) &amp; (char)255 );
          //frame[5] = (char)( ((char)len &gt;&gt; 32) &amp; (char)255 );
          //frame[6] = (char)( ((char)len &gt;&gt; 24) &amp; (char)255 );
          frame[7] = (char)( ((char)len &gt;&gt; 16) &amp; (char)255 );
          frame[8] = (char)( ((char)len &gt;&gt; 8) &amp; (char)255 );
          frame[9] = (char)( ((char)len) &amp; (char)255 );
          frameCount = 10;
      }//END IF
  
      memcpy(reply, frame, frameCount);
      memcpy(reply + frameCount, s, len);
  
      //printf("sock: %d\ts: %s\tlen: %d\n", sock, s, len);
      if (write(sock, reply, strlen(reply)) &lt;= 0) {
          printf("\n\nWE ARE NOT WRITING!!\n\n");
      } else {
          //printf("we did write\n");
      }//END IF
  
      free(reply);
      reply = NULL;
  
      return NULL;
  }//END FUNCTION
  </code></pre>
  
  <p><strong>loadFunction.c</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;dlfcn.h&gt;
  #include "include/functions.h"
  
  int checkForError(char *error) {
      if (error != NULL) {
          printf("ERROR: %s\n", error);
          exit(EXIT_FAILURE);
      }//END IF
  
      return 0;
  }//END IF
  
  void * loadFunction(char *func, void ** args) {
      void *handle;
      //void * (*alterStruct)(int sock, char *action);
  
      int filenameLength;
      char * filename;
      //void *(*funcPtr);
  
  
      filenameLength = strlen("lib/lib") + strlen(func) + strlen(".dll");
      filename = malloc(sizeof(char) * (filenameLength + 1));
      strcpy(filename, "lib/lib");
      strcat(filename, func);
      strcat(filename, ".dll");
  
      handle = dlopen(filename, RTLD_LAZY);
      free(filename);
      if (!handle) {
          checkForError(dlerror());
      }//END IF
  
      dlerror();
  
      if (strncmp(func, "sendMessage", strlen(func)) == 0) {
          void * (*funcPtr)(int, char *, int);
          //*(void **) (&amp;funcPtr) = dlsym(handle, func);
          funcPtr = (void *)dlsym(handle, func);
          checkForError(dlerror());
          (*funcPtr)((int)args[0], (char *)args[1], (int)args[2]);
          //free(*(void **)(&amp;funcPtr));
          //*(void **) (&amp;funcPtr) = NULL;
      }// else if (strncmp(func, "alterStruct", strlen(func)) == 0) {
          //void * (*funcPtr)(int sock, char *action);
      //} else if (strncmp(func, "execute", strlen(func)) == 0) {
          //void * (*funcPtr)(const char *command, clientStruct s, FILE **in, FILE **out, FILE **err);
      //} else {
          //void * (*funcPtr)(int sock, char *s, int len);
      //}//END IF
  
      dlclose(handle);
      handle = NULL;
      return NULL;
  
  
      return NULL;
  
  }//END loadFunction
  </code></pre>
  
  <p>If you need more code to solve this problem I have it accessable on <a href="https://github.com/mic1780/websockets/tree/dynamic" rel="nofollow">GitHub here</a> (the dynamic branch is where the problem can be found)</p>
  
  <p>Also, I am using Cygwins' GNU gcc compiler (which I have never have a problem compiling on) to compile my application and libraries so I may not have access to certain Linux commands (for example <code>dlmopen</code>). That said, please do not say use a different compiler because I've had no other problems thus far and I do not intend on changing how I compile my code.</p>
  
  <p>I did not document the command I use to compile just the libsendMessage.dll used in <code>loadFunction</code>. you can obtain it using the following</p>
  
  <pre><code>gcc -c -fPIC sendMessage.c
  mv sendMessage.o objects/sendMessage.o
  gcc -shared -o lib/libsendMessage.dll objects/sendMessage.o libfunctions.c
  </code></pre>
  
  <p>Thank you in advance.</p>