~ubuntu-branches/ubuntu/karmic/powersave/karmic

« back to all changes in this revision

Viewing changes to daemon/script_return_helper/script_return_helper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2006-01-13 21:38:52 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060113213852-lqnirx6tfj6q76jv
Tags: 0.11.2-1
* New upstream release.
* Removed patches shebang_fix.diff, awk_path_fix.diff and
  wttyhx_fixes.diff, all merged upstream.
* hal and dbus are now mandatory. Added the corresponding dependencies to
  debian/control.
* Added powersaved.postinst, dbus needs to be reloaded after powersaved has
  been installed. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <iostream>
2
 
#include "../../libpower/powersave_daemonlib.h"
3
 
#include <sys/socket.h>
4
 
#include <sys/un.h>
5
 
 
6
 
using namespace std;
7
 
 
8
 
int open_client_connection(void);
9
 
 
10
 
int main(int *argc, char *argv[]) {
11
 
        // check whether there is a cmdline argument
12
 
        if (argv[1] == NULL ) {
13
 
                cerr << "You didn't provide any string which can be sent to the daemon\n";
14
 
                return 1;
15
 
        }
16
 
                
17
 
        // open a connection to the daemon
18
 
        int fd = open_client_connection();
19
 
        if (fd<0) {
20
 
                cerr << "Error opening connection to daemon\n";
21
 
                return 1;
22
 
        }
23
 
 
24
 
        int send = POWERSAVED_EVENT_RETURN;
25
 
        // write the EVENT_RETURN integer to the daemon
26
 
        int i = write(fd, &send, sizeof(send));
27
 
        if (i<0) {
28
 
                cerr << "Powersaved didn't accept event return\n";
29
 
                return 1;
30
 
        }
31
 
 
32
 
        // prepare the string to send
33
 
        char * sendstr = argv[1];
34
 
        //sendstr[strlen(sendstr)] = '\n'; // no longer, we now send 0-terminated strings.
35
 
 
36
 
        // write the string to the daemon
37
 
        int r = write(fd,sendstr, strlen(sendstr) + 1);
38
 
        if (r<=0) {
39
 
                cerr << "Error writing string to daemon\n";
40
 
        }
41
 
 
42
 
        // wheter the daemon accepted the string doesn't matter, so close
43
 
        // the connection
44
 
        close(fd);
45
 
 
46
 
        return 0;
47
 
}
48
 
 
49
 
// opens a socket to connect to daemon
50
 
int open_client_connection(void){
51
 
        struct sockaddr_un address;
52
 
 
53
 
        int sockfd;
54
 
        int result;
55
 
 
56
 
        sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
57
 
 
58
 
        if (sockfd == -1){
59
 
                return -1;
60
 
        }
61
 
        
62
 
        address.sun_family = AF_UNIX;
63
 
        strcpy(address.sun_path, POWERSAVED_SOCKET_PATH);
64
 
 
65
 
        result = connect(sockfd, (struct sockaddr*)&address, sizeof(address));  
66
 
 
67
 
        if (sockfd < 0){
68
 
                return -1;
69
 
        }
70
 
        if (result == 0){
71
 
                        return sockfd;
72
 
        }
73
 
        else {
74
 
                close(sockfd);
75
 
                return -1;
76
 
        }
77
 
}
78