/*
 * We are intercepting socket() so that we can set the stackname in time.
 * We can't just do this when the stack's name is set; pthread_setname_np
 * allows this to be done from another thread; and onload_set_stackname
 * can only act on the current thread.  So we need to do it here instead.
*/
int socket(int socket_family, int socket_type, int protocol)
{
  int ok, ext_ok;

  /* Set up passthroughs */
  if( ensure_intercepts() < 0 )
    return -EOPNOTSUPP;

  /* Set up stack name based on socket type */
  ext_ok = apply_extname(socket_type);
  /* Actually create the socket */
  ok = _socket(socket_family, socket_type, protocol);

  /* Go back to old stack name, if we changed */
  if( ext_ok )
    onload_stackname_restore();

  LOG("socket returning %d", ok);
  return ok;
}
JNIEXPORT jint JNICALL
Java_OnloadExt_RestoreStackName (JNIEnv* env, jclass cls)
{
	return (jint) onload_stackname_restore();
}