Main Page | Modules | Directories | File List | Globals | Related Pages

io.c

Go to the documentation of this file.
00001 #ifndef _IO_C_ /* -*- linux-c -*- */
00002 #define _IO_C_
00003 
00004 #include "relay-app.h"
00005 #include "print.c"
00006 
00007 /** @file io.c
00008  * @brief I/O functions
00009  */
00010 /** @addtogroup io I/O
00011  * I/O functions
00012  * @{
00013  */
00014 
00015 /** private buffer for _stp_log() */
00016 #define STP_LOG_BUF_LEN 2047
00017 static char _stp_lbuf[NR_CPUS][STP_LOG_BUF_LEN + 1];
00018 
00019 /** Logs Data.
00020  * This function prints to the system log if stpd has not connected
00021  * yet.  Otherwise it sends the message immediately to stpd.
00022  * @param fmt A variable number of args.
00023  * @note Lines are limited in length by printk buffer. If there is
00024  * no newline in the format string, then other syslog output could
00025  * get appended to the SystemTap line.
00026  * @todo Evaluate if this function is necessary.
00027  */
00028 
00029 void _stp_log (const char *fmt, ...)
00030 {
00031         int num;
00032         char *buf = &_stp_lbuf[smp_processor_id()][0];
00033         va_list args;
00034         va_start(args, fmt);
00035         num = vscnprintf (buf, STP_LOG_BUF_LEN, fmt, args);
00036         va_end(args);
00037         buf[num] = '\0';
00038 
00039         if (app.logging)
00040                 send_reply (STP_REALTIME_DATA, buf, num + 1, stpd_pid);
00041         else
00042                 printk("STP: %s", buf);
00043 }
00044 
00045 static void stpd_app_started(void)
00046 {
00047         printk ("stpd has started.\n");
00048 }
00049 
00050 static void stpd_app_stopped(void)
00051 {
00052         printk ("stpd has stopped.\n");
00053 }
00054 
00055 static void probe_exit(void);
00056 
00057 #include <linux/delay.h>
00058 static int stpd_command (int type, void *data)
00059 {
00060         if (type == STP_EXIT) {
00061                 printk ("STP_EXIT received\n");
00062                 probe_exit();
00063 #ifndef STP_NETLINK_ONLY
00064                 relay_flush(app.chan);
00065                 ssleep(2); /* FIXME: time for data to be flushed */
00066 #endif
00067                 send_reply (STP_EXIT, __this_module.name, strlen(__this_module.name) + 1, stpd_pid);
00068                 return 1;
00069         }
00070         return 0;
00071 }
00072 
00073 /*
00074  * relay-app callbacks
00075  */
00076 static struct relay_app_callbacks stp_callbacks =
00077 {
00078         .app_started = stpd_app_started,
00079         .app_stopped = stpd_app_stopped,
00080         .user_command = stpd_command
00081 };
00082 
00083 /** Opens netlink and relayfs connections to stpd.
00084  * This must be called before any I/O is done, probably 
00085  * at the start of module initialization.
00086  */
00087 int _stp_netlink_open(void)
00088 {
00089         if (init_relay_app("stpd", "cpu", &stp_callbacks)) {
00090                 printk ("STP: couldn't init relay app\n");
00091                 return -1;
00092         }
00093         return 0;
00094 }
00095 
00096 /** Closes netlink and relayfs connections to stpd.
00097  * This must be called after all I/O is done, probably 
00098  * at the end of module cleanup.
00099  * @returns 0 on success.  -1 if there is a problem establishing
00100  * a connection.
00101  */
00102         
00103 void _stp_netlink_close (void)
00104 {
00105         send_reply (STP_DONE, NULL, 0, stpd_pid);
00106         close_relay_app();
00107 }
00108 
00109 /** @} */
00110 #endif /* _IO_C_ */