Main Page | Data Structures | File List | Data Fields | Globals

rpc-retutime.c

Go to the documentation of this file.
00001 /**
00002  * This file is part of alarmd
00003  *
00004  * Contact Person: David Weinehall <david.weinehall@nokia.com>
00005  *
00006  * Copyright (C) 2006 Nokia Corporation
00007  * alarmd and libalarm are free software; you can redistribute them
00008  * and/or modify them under the terms of the GNU Lesser General Public
00009  * License version 2.1 as published by the Free Software Foundation.
00010  *
00011  * alarmd and libalarm are distributed in the hope that they will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this software; if not, write to the Free
00018  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00019  * 02110-1301 USA
00020  */
00021 
00022 #include <unistd.h>
00023 #include <glib/gstrfuncs.h>
00024 #include <glib/gspawn.h>
00025 #include <glib/gmem.h>
00026 #include <time.h>
00027 #include "rpc-retutime.h"
00028 
00029 
00030 #include <stdio.h>
00031 
00032 #define RETUTIME_TIME_FORMAT "%Y-%m-%d/%H:%M:%S"
00033 #define RETUTIME_DISABLE "0000-00-00/00:00:00"
00034 #define RETUTIME_CMD "/usr/sbin/chroot /mnt/initfs /usr/bin/retutime "
00035 #define USER_RETUTIME_CMD "sudo " RETUTIME_CMD
00036 
00037 #define RETUTIME_SET_ALARM "-A "
00038 #define RETUTIME_ACK_ALARM "-S "
00039 #define RETUTIME_QRY_ALARM "-s "
00040 
00041 static gint _retutime_do(const gchar *operation,
00042                 const gchar *argument,
00043                 gchar **output) {
00044         const gchar *command;
00045         gchar *cmd;
00046         gint retval = 1;
00047 
00048         if (getuid() == 0) {
00049                 command = RETUTIME_CMD;
00050         } else {
00051                 command = USER_RETUTIME_CMD;
00052         }
00053 
00054         cmd = g_strconcat(command, operation, argument, NULL);
00055         
00056         fprintf(stderr, "Running command %s\n", cmd);
00057         g_spawn_command_line_sync(cmd, output, NULL, &retval, NULL);
00058         g_free(cmd);
00059 
00060         return retval;
00061 }
00062 
00063 gboolean retutime_set_alarm_time(time_t alarm_time) {
00064         gchar buffer[64];
00065         struct tm ftm;
00066         gint status;
00067 
00068         gmtime_r(&alarm_time, &ftm);
00069         strftime(buffer, 63, RETUTIME_TIME_FORMAT, &ftm);
00070         buffer[63] = '\0';
00071 
00072         status = _retutime_do(RETUTIME_SET_ALARM, buffer, NULL);
00073 
00074         if (status) {
00075                 return FALSE;
00076         }
00077         return TRUE;
00078 }
00079 
00080 gboolean retutime_disable_alarm(void) {
00081         gint status = _retutime_do(RETUTIME_SET_ALARM,
00082                         RETUTIME_DISABLE,
00083                         NULL);
00084 
00085         if (status) {
00086                 return FALSE;
00087         }
00088         return TRUE;
00089 }
00090 
00091 gboolean retutime_ack_alarm(void) {
00092         gint status = _retutime_do(RETUTIME_ACK_ALARM,
00093                         NULL,
00094                         NULL);
00095 
00096         if (status) {
00097                 return FALSE;
00098         }
00099         return TRUE;
00100 }
00101 
00102 gboolean retutime_query_alarm(void) {
00103         gchar *state = NULL;
00104         gboolean retval = FALSE;
00105         gint status = _retutime_do(RETUTIME_QRY_ALARM,
00106                         NULL,
00107                         &state);
00108 
00109         if (state) {
00110                 if (*state == '1') {
00111                         retval = TRUE;
00112                 }
00113                 g_free(state);
00114         }
00115 
00116         if (status) {
00117                 return FALSE;
00118         }
00119         return retval;
00120 }

Generated on Thu Dec 21 18:23:30 2006 for Alarmd by  doxygen 1.4.2