source: TI05-delivery/trunk/src/python_ext/util.c @ 1353

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/trunk/src/python_ext/util.c@1353
Revision 1353, 3.0 KB checked in by spascoe, 13 years ago (diff)

Added a convenient debugging funtion ndg_debug_sleep(). Also
made the argument passing to bbftpd_main slightly more conservative.

Line 
1/**
2 * NDG python embedded bbftp daemon module.
3 *
4 * @author Stephen Pascoe
5 *
6 * Copyright (C) 2006 CCLRC & NERC
7 *
8 * This software may be distributed under the terms of the Q Public Licence, version 1.0 or later.
9 *
10 */
11
12#include <stdlib.h>
13#include <string.h>
14#include <Python.h>
15#include <unistd.h>
16#include <stdio.h>
17
18#include "util.h"
19
20/**
21 * dump pid to a file and sleep.  For use when debugging.
22 * @param secs number of seconds to sleep
23 */
24void ndg_debug_sleep(int secs) {
25  FILE *fh;
26  fh = fopen("pybbftpd.pid", "w");
27  fprintf(fh, "%d\n", getpid());
28  fclose(fh);
29
30  sleep(secs);
31}
32   
33
34/**
35 * Routines for constructing a argc/argv style argument list.
36 */
37
38/**
39 * Initialise a structure.
40 * @return TRUE on success or FALSE on a memory error.
41 */
42int ndg_argv_init(struct ndg_argv *argv_s, int argc) {
43  argv_s->argc = argc;
44
45  if ((argv_s->argv = (char **)malloc(argc * sizeof(char *))) == NULL) {
46    return FALSE;
47  }
48
49  argv_s->next = 0;
50
51  return TRUE;
52}
53
54/**
55 * Free a structure.  \a argv_s should be cleared first with \c ndg_argv_clear().
56 *
57 * @return TRUE on success or FALSE if the structure isn't empty.
58 */
59int ndg_argv_free(struct ndg_argv *argv_s) {
60  if (argv_s->next != 0) return FALSE;
61  free(argv_s->argv);
62  return TRUE;
63}
64
65/**
66 * Clear all arguments from the structure.
67 *
68 */
69void ndg_argv_clear(struct ndg_argv *argv_s) {
70  char *arg;
71  while (argv_s->next > 0) {
72    argv_s->next--;
73    arg = argv_s->argv[argv_s->next];
74    free(arg);
75  }
76}
77
78/**
79 * Add an argument.
80 *
81 * @return TRUE on success or FALSE on an error.
82 */
83int ndg_argv_add(struct ndg_argv *argv_s, char *arg) {
84
85  if (argv_s->argc <= argv_s->next) return FALSE;
86  if ((argv_s->argv[argv_s->next] = (char *)malloc(strlen(arg) + 1)) == NULL) {
87    return FALSE;
88  }
89  strcpy(argv_s->argv[argv_s->next], arg);
90  argv_s->next++;
91  return TRUE;
92}
93
94
95/**
96 * Copy the python exception message to a bbftp logmessage.
97 *
98 * If the python error indicator is set it will be cleared.
99 * If the python error indicator is not set the empty string will be copied to \a logmessage.
100 * The exception message is truncated if it exceeds \c NDG_MAX_LOGMESSAGE.
101 *
102 * @param logmessage a pointer to a buffer of length \c NDG_MAX_LOGMESSAGE
103 *     for storing an error message when returning -1.
104 * @param prefix a string to be prefixed to the logmessage.
105 */
106void ndg_pyerr_to_logmessage(char *logmessage, char *prefix) {
107  PyObject *ptype, *pvalue, *ptraceback, *obj;
108  char *exname, *exvalue;
109
110  PyErr_Fetch(&ptype, &pvalue, &ptraceback);
111 
112  if ((obj = PyObject_GetAttrString(ptype, "__name__")) == NULL) goto error;
113  exname = PyString_AsString(obj); Py_DECREF(obj);
114  if (exname == NULL) goto error;
115
116  if ((obj = PyObject_Str(pvalue)) == NULL) goto error;
117  exvalue = PyString_AsString(obj); Py_DECREF(obj);
118  if (exvalue == NULL) goto error;
119
120  snprintf(logmessage, NDG_MAX_LOGMESSAGE, "%s: %s: %s", prefix, exname, exvalue);
121  return;
122 
123 error:
124  sprintf("ndg_pyerr_to_logmessage: internal python error", logmessage);
125  return;
126}
Note: See TracBrowser for help on using the repository browser.