Ejemplo n.º 1
0
uintptr_t
sml_getStdNumbers(uintptr_t triple)
{
    // Triples are also tag-free when pairs are!
    mkTagTripleML(triple);
    elemRecordML(triple,0) = convertIntToML(STDIN_FILENO);
    elemRecordML(triple,1) = convertIntToML(STDOUT_FILENO);
    elemRecordML(triple,2) = convertIntToML(STDERR_FILENO);
    return triple;
}
Ejemplo n.º 2
0
uintptr_t
sml_pipe(uintptr_t triple)
{
    int a[2], r;
    // Triples are also tag-free when pairs are!
    mkTagTripleML(triple);
    r = pipe(a);
    elemRecordML(triple,0) = convertIntToML(r);
    elemRecordML(triple,1) = convertIntToML(a[0]);
    elemRecordML(triple,2) = convertIntToML(a[1]);
    return triple;
}
Ejemplo n.º 3
0
uintptr_t
sml_times(uintptr_t tuple)
{
    struct tms buf;
    clock_t r;
    mkTagRecordML(tuple, 5);
    r = times(&buf);
    if (r == (clock_t) -1) raise_exn((uintptr_t)&exn_OVERFLOW);
    elemRecordML(tuple,0) = convertIntToML(r & (SIZE_MAX / 4));
    elemRecordML(tuple,1) = convertIntToML(buf.tms_utime & (SIZE_MAX / 4));
    elemRecordML(tuple,2) = convertIntToML(buf.tms_stime & (SIZE_MAX / 4));
    elemRecordML(tuple,3) = convertIntToML(buf.tms_cutime & (SIZE_MAX / 4));
    elemRecordML(tuple,4) = convertIntToML(buf.tms_cstime & (SIZE_MAX / 4));
    return tuple;
}
Ejemplo n.º 4
0
static uintptr_t
sml_statA(uintptr_t pair, struct stat *b)
{
    int res;
    res = 0;
    res |= S_ISREG(b->st_mode);
    res <<= 1;
    res |= S_ISDIR(b->st_mode);
    res <<= 1;
    res |= S_ISCHR(b->st_mode);
    res <<= 1;
    res |= S_ISBLK(b->st_mode);
    res <<= 1;
    res |= S_ISFIFO(b->st_mode);
    res <<= 1;
    res |= S_ISLNK(b->st_mode);
    res <<= 1;
    res |= S_ISSOCK(b->st_mode);
    elemRecordML(pair,0) = convertIntToML(res);
    res = 0;
    res |= (S_ISGID & b->st_mode ? 1 : 0);
    res <<= 1;
    res |= (S_ISUID & b->st_mode ? 1 : 0);
    res <<= 1;
    res |= (S_IXOTH & b->st_mode ? 1 : 0);
    res <<= 1;
    res |= (S_IWOTH & b->st_mode ? 1 : 0);
    res <<= 1;
    res |= (S_IROTH & b->st_mode ? 1 : 0);
    res <<= 1;
    res |= (S_IRWXO & b->st_mode ? 1 : 0);
    res <<= 1;
    res |= (S_IXGRP & b->st_mode ? 1 : 0);
    res <<= 1;
    res |= (S_IWGRP & b->st_mode ? 1 : 0);
    res <<= 1;
    res |= (S_IRGRP & b->st_mode ? 1 : 0);
    res <<= 1;
    res |= (S_IRWXG & b->st_mode ? 1 : 0);
    res <<= 1;
    res |= (S_IXUSR & b->st_mode ? 1 : 0);
    res <<= 1;
    res |= (S_IWUSR & b->st_mode ? 1 : 0);
    res <<= 1;
    res |= (S_IRUSR & b->st_mode ? 1 : 0);
    res <<= 1;
    res |= (S_IRWXU & b->st_mode ? 1 : 0);
    elemRecordML(pair,1) = convertIntToML(res);
    elemRecordML(pair,2) = convertIntToML(b->st_ino);
    elemRecordML(pair,3) = convertIntToML(b->st_dev);
    elemRecordML(pair,4) = convertIntToML(b->st_nlink);
    elemRecordML(pair,5) = convertIntToML(b->st_size);
    elemRecordML(pair,6) = convertIntToML(b->st_uid);
    elemRecordML(pair,7) = convertIntToML(b->st_gid);
    return pair;
}
Ejemplo n.º 5
0
uintptr_t 
sml_mktime (uintptr_t vAddr, uintptr_t v) 
{
  struct tm tmr;
  tmr.tm_hour = convertIntToC(elemRecordML(v,0));
  tmr.tm_isdst = convertIntToC(elemRecordML(v,1));
  tmr.tm_mday = convertIntToC(elemRecordML(v,2));
  tmr.tm_min = convertIntToC(elemRecordML(v,3));
  tmr.tm_mon = convertIntToC(elemRecordML(v,4));
  tmr.tm_sec = convertIntToC(elemRecordML(v,5));
  tmr.tm_wday = convertIntToC(elemRecordML(v,6));
  tmr.tm_yday = convertIntToC(elemRecordML(v,7));
  tmr.tm_year = convertIntToC(elemRecordML(v,8));
  get_d(vAddr) = (double)(tm2cal(&tmr));
  set_dtag(vAddr);
  return vAddr;
}
Ejemplo n.º 6
0
long
REG_POLY_FUN_HDR(sml_getpwnam, long tuple, Region homeR, Region shellR, String nameML, long s, long exn)
{
    long res;
    char *b;
    struct passwd pbuf, *pbuf2;
    char *name = &(nameML->data);
    mkTagRecordML(tuple,5);
    s = convertIntToC(s) + 1;
    b = (char *) malloc(s);
    if (!b)
    {
        res = errno;
        elemRecordML(tuple,4) = res;
        return tuple;
    }
    res = getpwnam_r(name, &pbuf, b, s-1, &pbuf2);
    elemRecordML(tuple,4) = res;
    if (res)
    {
        free(b);
        return tuple;
    }
    if (!pbuf2)
    {
        free(b);
        raise_exn(exn);
    }
    elemRecordML(tuple,0) = (long) pbuf2->pw_uid;
    elemRecordML(tuple,1) = (long) pbuf2->pw_gid;
    elemRecordML(tuple,2) = (long) REG_POLY_CALL(convertStringToML, homeR, pbuf2->pw_dir);
    elemRecordML(tuple,3) = (long) REG_POLY_CALL(convertStringToML, shellR, pbuf2->pw_shell);
    free(b);
    return tuple;
}
Ejemplo n.º 7
0
uintptr_t 
sml_gmtime (uintptr_t vAddr, uintptr_t r) 
{
  struct tm tmr;
  time_t clock = (long)(get_d(r));
  gmtime_r(&clock,&tmr);
  mkTagRecordML(vAddr,9);
  elemRecordML(vAddr,0) = convertIntToML(tmr.tm_hour);
  elemRecordML(vAddr,1) = convertIntToML(tmr.tm_isdst);
  elemRecordML(vAddr,2) = convertIntToML(tmr.tm_mday);
  elemRecordML(vAddr,3) = convertIntToML(tmr.tm_min);
  elemRecordML(vAddr,4) = convertIntToML(tmr.tm_mon);
  elemRecordML(vAddr,5) = convertIntToML(tmr.tm_sec);
  elemRecordML(vAddr,6) = convertIntToML(tmr.tm_wday);
  elemRecordML(vAddr,7) = convertIntToML(tmr.tm_yday);
  elemRecordML(vAddr,8) = convertIntToML(tmr.tm_year);
  return vAddr;
}
Ejemplo n.º 8
0
uintptr_t
sml_fstat(uintptr_t pair, size_t fd)
{
    int res;
    struct stat b;
    mkTagPairML(pair);
    res = fstat((int) fd, &b);
    if (res == -1)
    {
        elemRecordML(pair,0) = convertIntToML(-1);
        return pair;
    }
    return sml_statA(pair, &b);
}
Ejemplo n.º 9
0
uintptr_t
sml_stat(uintptr_t pair, String file)
{
    int res;
    struct stat b;
    mkTagPairML(pair);
    res = stat(&(file->data), &b);
    if (res == -1)
    {
        elemRecordML(pair,0) = convertIntToML(-1);
        return pair;
    }
    return sml_statA(pair, &b);
}
Ejemplo n.º 10
0
String
REG_POLY_FUN_HDR(sml_asctime, Region rAddr, uintptr_t v, int exn) 
{
  struct tm tmr;
  char *r;
  char res[30]; /* Should at least be 26 + 0 termination according to man asctime */
  tmr.tm_hour = convertIntToC(elemRecordML(v,0));
  tmr.tm_isdst = convertIntToC(elemRecordML(v,1));
  tmr.tm_mday = convertIntToC(elemRecordML(v,2));
  tmr.tm_min = convertIntToC(elemRecordML(v,3));
  tmr.tm_mon = convertIntToC(elemRecordML(v,4));
  tmr.tm_sec = convertIntToC(elemRecordML(v,5));
  tmr.tm_wday = convertIntToC(elemRecordML(v,6));
  tmr.tm_yday = convertIntToC(elemRecordML(v,7));
  tmr.tm_year = convertIntToC(elemRecordML(v,8));
  r = asctime_r(&tmr, res);
  if ( r == NULL ) 
    {
      raise_exn(exn);
    }
  return REG_POLY_CALL(convertStringToML, rAddr, res);
}
Ejemplo n.º 11
0
String
REG_POLY_FUN_HDR(sml_strftime, Region rAddr, String fmt, uintptr_t v, int exn) 
{
  struct tm tmr;
  int ressize;
#define BUFSIZE 256
  char buf[BUFSIZE];
  tmr.tm_hour = convertIntToC(elemRecordML(v,0));
  tmr.tm_isdst = convertIntToC(elemRecordML(v,1));
  tmr.tm_mday = convertIntToC(elemRecordML(v,2));
  tmr.tm_min = convertIntToC(elemRecordML(v,3));
  tmr.tm_mon = convertIntToC(elemRecordML(v,4));
  tmr.tm_sec = convertIntToC(elemRecordML(v,5));
  tmr.tm_wday = convertIntToC(elemRecordML(v,6));
  tmr.tm_yday = convertIntToC(elemRecordML(v,7));
  tmr.tm_year = convertIntToC(elemRecordML(v,8));
  ressize = strftime(buf, BUFSIZE, &(fmt->data), &tmr);
  if ( ressize == 0 || ressize == BUFSIZE )
    {
      raise_exn(exn);
    }
  return REG_POLY_CALL(convertStringToML, rAddr, buf);
#undef BUFSIZE
}
Ejemplo n.º 12
0
uintptr_t 
sml_getrutime(uintptr_t vAddr) 
{
  struct rusage rusages;
  getrusage(RUSAGE_SELF, &rusages);
  elemRecordML(vAddr,2) = convertIntToML(rusages.ru_stime.tv_sec);
  elemRecordML(vAddr,3) = convertIntToML(rusages.ru_stime.tv_usec);
  elemRecordML(vAddr,4) = convertIntToML(rusages.ru_utime.tv_sec);
  elemRecordML(vAddr,5) = convertIntToML(rusages.ru_utime.tv_usec);
  mkTagRecordML(vAddr,6);
  elemRecordML(vAddr,0) = convertIntToML(0);   /* zero gc */
  elemRecordML(vAddr,1) = convertIntToML(0);
  return vAddr;
}
Ejemplo n.º 13
0
// ML: cache * String * String -> (int * string_ptr)
int
apsml_cacheSet (int resultPair, Region sAddr, cache * c, int keyValPair, request_data * rd)	/*{{{ */
{
  // allocate new entry and key,value placeholders
// ppCache(c, rd);
  String key1 = (String) elemRecordML (keyValPair, 0);
  String value1 = (String) elemRecordML (keyValPair, 1);
  time_t timeout = (time_t) elemRecordML (keyValPair, 2);
  char *value = &(value1->data);
  int valuesize = sizeStringDefine(value1);
  char *key = &(key1->data);
  int keysize = sizeStringDefine(key1);
  int size = sizeof (entry) + keysize + 1 + valuesize + 1;
  entry *newentry =
    (entry *) malloc (size);
//      ap_log_error (APLOG_MARK, LOG_DEBUG, 0, rd->server,
//		    "apsml_cacheCreate: malloc 0x%x, length: %d, sizeof(entry): %d, keysize: %d, valuesize: %d, key: %s, val: %s", (unsigned long) newentry, size, sizeof(entry), keysize, valuesize, key, value);
  if (newentry == NULL)
    return 0;
  char *newkey = (char *) (newentry + 1);
  char *newvalue = newkey + (keysize + 1);

  // prepare entry by copy data to my space and set pointers
  strncpy (newkey, key, keysize);
  newkey[keysize] = 0;
  strncpy (newvalue, value, valuesize);
  newvalue[valuesize] = 0;
  newentry->key = newkey;
//  newentry->key.hash = charhashfunction (newkey);
  newentry->data = newvalue;
  newentry->size = keysize + valuesize + sizeof (entry) + 2;
  time_t ct = time (NULL);
  if (timeout && c->timeout)
  {
    newentry->timeout = MIN (timeout, c->timeout);
  }
  else if (timeout)
  {
    newentry->timeout = timeout;
  }
  else
  {
    newentry->timeout = c->timeout;
  }
  newentry->time = ct + newentry->timeout;

  // We are going in !!! (as we get a writes lock we have 
  // complete control [no more locks])
  apr_thread_rwlock_wrlock (c->rwlock);
  int tmpsize = c->htable->hashTableSize;
  entry *oldentry = NULL;
//  void **oldentry1 = (void **) &oldentry;
//  ap_log_error (APLOG_MARK, LOG_NOTICE, 0, rd->server,
//		"apsml_cacheSet: key %s, hash: %i", key, newentry->key.hash);
  if (entrytable_find (c->htable, newentry->key, &oldentry) == hash_DNE)
  {
    // No old entry with that key 
    if ((newentry->timeout == -1 
            && cacheheap_heapinsert(c->heap, newentry, (time_t) 0) == heap_OUTOFMEM) 
        || (newentry->timeout && newentry->timeout != (time_t) -1
            && cacheheap_heapinsert(c->heap, newentry, newentry->time) == heap_OUTOFMEM))
    {
      ap_log_error (APLOG_MARK, LOG_NOTICE, 0, rd->server,
        "apsml_cacheSet: pid %d, received heap_OUTOFMEM", rd->ctx->pid);
      free(newentry);
      newentry = 0;
    }
    if (newentry && entrytable_insert (c->htable, newentry->key, newentry) == hash_OUTOFMEM)
	  {
      ap_log_error (APLOG_MARK, LOG_NOTICE, 0, rd->server,
        "apsml_cacheSet: pid %d, received hash_OUTOFMEM", rd->ctx->pid);
	    free (newentry);
	    newentry = 0;
	  }
//  ppCache(c, rd);
    oldentry = 0;
  }
  else
  {
    // Old exists
    if ((newentry->timeout == (time_t) -1 
           && cacheheap_heapinsert(c->heap, newentry, (time_t) 0) == heap_OUTOFMEM)
        || (newentry->timeout && newentry->timeout != (time_t) -1
           && cacheheap_heapinsert(c->heap, newentry, newentry->time) == heap_OUTOFMEM))
    {
      ap_log_error (APLOG_MARK, LOG_NOTICE, 0, rd->server,
        "apsml_cacheSet: pid %d, received heap_OUTOFMEM", rd->ctx->pid);
      free(newentry);
      newentry = 0;
    }
    if (newentry && entrytable_update (c->htable, newentry->key, newentry) == hash_OUTOFMEM)
	  {
      ap_log_error (APLOG_MARK, LOG_NOTICE, 0, rd->server,
        "apsml_cacheSet: pid %d, received hash_OUTOFMEM", rd->ctx->pid);
	    free (newentry);
	    newentry = 0;
	  }
  }

  if (newentry)
  {
    c->size += newentry->size;
    c->size += (tmpsize - c->htable->hashTableSize) * sizeof (entrytable_hashelement_t);
  }
//  ppCache(c, rd);
  int too_old = 0;
  if (oldentry)
  {
    // Old entry needs removel
    // time_t t = (ct - oldentry->time) < 0 ? 0 : ct - oldentry->time;
    if (oldentry->timeout && ct > oldentry->time)
	  {
	    too_old = 1;
	    second (resultPair) = 0;
	  }
    else
    {
      second (resultPair) = (int) convertStringToML (sAddr, oldentry->data);
    }
    listremoveitem (c, oldentry, rd);
  }
  if (too_old)
    oldentry = 0;
  if (newentry)
    LINKEDLIST_INSERTUNDER (c->sentinel, newentry);
  // I think we are done now
//  ppCache(c, rd);
  entry *curentry;
//  ap_log_error(APLOG_MARK, LOG_NOTICE, 0, rd->server, 
//          "apsml_cacheSet: size %d, maxsize = %d, ct: %d", c->size, c->maxsize, ct);
  while (cacheheap_heapminimal(c->heap, &curentry) != heap_UNDERFLOW)
  {
    if (curentry->time < ct)
    {
      cacheremoveitem(c, curentry, rd);
    }
    else break;
  }
//  ppCache(c, rd);
  if (c->maxsize != -1)
  {
    while (c->size > c->maxsize)
    {
      curentry = c->sentinel->up;
      if (curentry == c->sentinel)
        break;
      cacheremoveitem (c, curentry, rd);
    }
  }
  apr_thread_rwlock_unlock (c->rwlock);
//  ppCache(c, rd);
  if (oldentry && newentry)
    {
      first (resultPair) = 1;
      return resultPair;
    }
  second (resultPair) = 0;
  if (newentry)
    {
      first (resultPair) = 2;
      return resultPair;
    }
  first (resultPair) = 0;
  return resultPair;
}				/*}}} */
Ejemplo n.º 14
0
static uintptr_t *
apdns_getFQDN_MX_1 (Region rAddrLPairs, Region rAddrEPairs,
		    Region rAddrString, char *str, uintptr_t *list, int depth, request_data *rd)
{
  if (depth < 0)
    return list;
  // i,j are used as loop counters
  // the dnspackage returned from the resolver is scanned from top to buttom
  // next is the package pointer relative to the start of the package
  int j, next;
  char ans[NS_PACKETSZ + 1];
  char dnsnamesa[NS_MAXDNAME];
  char *dnsnames = dnsnamesa;
  char *input = str;
  uintptr_t *pair, *listpair;
  String rs;
  dnshead *head = (dnshead *) ans;
  // get the dns package
  int n = res_search (input, C_IN, T_MX, (unsigned char *) ans, NS_PACKETSZ + 1);
  input = 0;
  if (n == -1)
    return list;
  if (n < sizeof (dnshead))
    return list;
  ntohhead (ans, head);
  if ((head->flags & 0xF) != 0)
    return list;
  if (head->anscount < 1)
    return list;
  // skip questions
  next = NS_HFIXEDSZ;
  for (j = 0; j < head->questcount; j++)
    {
      next = skipname (ans, next, n);
      next = iflessthan (next + 4, n);
      if (next < 0)
	return list;
    }
  // The answers
  int rv;
  for (j = 0; j < head->anscount; j++)
    {
//      int a_name = next;
      if (next >= n)
	return list;
      next = skipname (ans, next, n);
      if (next + NS_RRFIXEDSZ >= n || next < 0)
	return list;
      uint16_t a_type = twocharto16 (ans[next], ans[next + 1]);
      next += 4;
      uint32_t a_ttl =
	fourcharto32 (ans[next], ans[next + 1], ans[next + 2], ans[next + 3]);
      next += 4;
      uint16_t a_rdlength = twocharto16 (ans[next], ans[next + 1]);
      next += 2;
      if (a_type == T_MX)
	{			// We got a mx record
	  if (next + a_rdlength >= n || a_rdlength < 3)
	    return list;
	  uint16_t a_mx_pref = twocharto16 (ans[next], ans[next + 1]);
	  rv = dumpname (dnsnames, NS_MAXDNAME, ans, next + 2, n);
	  rs = convertStringToML (rAddrString, dnsnames);
	  allocRecordML (rAddrEPairs, 3, pair);
	  elemRecordML (pair, 0) = (uintptr_t) a_mx_pref;
	  elemRecordML (pair, 1) = (uintptr_t) a_ttl;
	  elemRecordML (pair, 2) = (uintptr_t) rs;
	  allocRecordML (rAddrLPairs, 2, listpair);
	  first (listpair) = (uintptr_t) pair;
	  second (listpair) = (uintptr_t) list;
	  makeCONS (listpair, list);
    ap_log_error (APLOG_MARK, LOG_DEBUG, 0, rd->server, 
        "apdns_getFQDN_MX: pref %i, ttl %i, %s", a_mx_pref, a_ttl, dnsnames);
	}
      else if (a_type == T_CNAME)
	{			// we got an alias (cononical name)
	  rv = dumpname (dnsnames, NS_MAXDNAME, ans, next, n);
	  input = dnsnames;
	  break;
	}
      else
	{			// we got something we did not ask for
	  // or cannot handle at the momnet
	  return list;
	}
      next += a_rdlength;
    }
  if (input)
    return apdns_getFQDN_MX_1 (rAddrLPairs, rAddrEPairs,
			       rAddrString, input, list, depth - 1, rd);
  return list;
}