~ubuntu-branches/ubuntu/dapper/cpufreqd/dapper

« back to all changes in this revision

Viewing changes to utils/setspeed.c

  • Committer: Bazaar Package Importer
  • Author(s): Mattia Dongili
  • Date: 2005-11-27 18:47:42 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051127184742-9h26euwetr6kh1e6
Tags: 2.0.0-1

* New upstream release.
* cpufreqd.init: exit succesfully in case a stop is issued and
  cpufreqd is found running as requested by LSB thus making it
  possible to remove cpufreqd when cpufreqd itsef is stopped
  (closes: #340133)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <dirent.h>
 
2
#include <errno.h>
 
3
#include <fcntl.h>
 
4
#include <stdlib.h>
 
5
#include <stdio.h>
 
6
#include <unistd.h>
 
7
#include <sys/types.h>
 
8
#include <sys/stat.h>
 
9
#include <sys/socket.h>
 
10
#include <sys/time.h>
 
11
#include <sys/un.h>
 
12
#include "cpufreqd_remote.h"
 
13
 
 
14
static int cpufreqd_dirs(const struct dirent *d) {
 
15
        return (strncmp(d->d_name, "cpufreqd-", 9) == 0);
 
16
}
 
17
 
 
18
int main(int argc, char *argv[])
 
19
{
 
20
        int sock;
 
21
        struct dirent **namelist = NULL;
 
22
        struct sockaddr_un sck;
 
23
        struct stat st;
 
24
        time_t last_mtime = 0;
 
25
        long int n = 0;
 
26
        unsigned int cmd = 0;
 
27
        char *endptr = NULL;
 
28
        char buf[108];
 
29
        
 
30
 
 
31
        if (argc != 2) {
 
32
                printf("usage: %s [manual | dynamic | <profile#>]\n", argv[0]);
 
33
                return 1;
 
34
        }
 
35
        
 
36
        sck.sun_family = AF_UNIX;
 
37
        sck.sun_path[0] = '\0';
 
38
        /* get path */
 
39
        n = scandir("/tmp", &namelist, &cpufreqd_dirs, NULL);
 
40
        if (n > 0) { 
 
41
                while (n--) {
 
42
                        snprintf(buf, 108, "/tmp/%s", namelist[n]->d_name);
 
43
                        free(namelist[n]);
 
44
                        if (stat(buf, &st) != 0) {
 
45
                                fprintf(stderr, "%s: %s\n", buf, strerror(errno));
 
46
                                continue;
 
47
                        }
 
48
#if 0
 
49
                        fprintf(stdout, "%s %lu %lu %lu\n", buf, st.st_ctime, st.st_atime, st.st_mtime);
 
50
#endif
 
51
                        if (last_mtime == 0 || last_mtime < st.st_mtime) {
 
52
                                last_mtime = st.st_mtime;
 
53
                                snprintf(sck.sun_path, 108,"%s/cpufreqd", buf);
 
54
#if 0
 
55
                                fprintf(stdout, "--> %s\n", buf);
 
56
#endif
 
57
                        }
 
58
                }
 
59
                free(namelist);
 
60
        } else {
 
61
                fprintf(stderr, "No cpufreqd socket found\n");
 
62
                return ENOENT;
 
63
        }
 
64
        if (!sck.sun_path) {
 
65
                fprintf(stderr, "No cpufreqd socket found\n");
 
66
                return ENOENT;
 
67
        }
 
68
        fprintf(stdout, "socket I'll try to connect: %s\n", sck.sun_path);
 
69
 
 
70
 
 
71
        if (!strcmp(argv[1], "dynamic"))
 
72
                cmd = MAKE_COMMAND(CMD_SET_MODE, ARG_DYNAMIC);
 
73
        else if (!strcmp(argv[1], "manual"))
 
74
                cmd = MAKE_COMMAND(CMD_SET_MODE, ARG_MANUAL);
 
75
        else {
 
76
                n = strtol(argv[1], &endptr, 10);
 
77
                if (errno == ERANGE) {
 
78
                        fprintf (stderr, "Overflow in long int %ld (%s)\n", n, argv[1]);
 
79
                        return ERANGE;
 
80
                }
 
81
                if (n >> 16) {
 
82
                        fprintf (stderr, "Profile number out of range. Must be 0 < %s > %d\n",
 
83
                                        argv[1], 0xffff);
 
84
                        return EINVAL;
 
85
                }
 
86
                if (endptr[0] != '\0') {
 
87
                        fprintf (stderr, "Unknown argument %s\n", argv[1]);
 
88
                        return EINVAL;
 
89
                }
 
90
                cmd = MAKE_COMMAND(CMD_SET_PROFILE, n);
 
91
        }
 
92
 
 
93
        fprintf(stdout,  "command: %.8x %.4x %.4x\n", cmd, REMOTE_CMD(cmd), REMOTE_ARG(cmd));
 
94
        
 
95
        if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
 
96
                perror("socket()");
 
97
                return errno;
 
98
        }
 
99
 
 
100
        if (connect(sock, (struct sockaddr *)&sck, sizeof(sck)) == -1) {
 
101
                perror("connect()");
 
102
                close(sock);
 
103
                return errno;
 
104
        }
 
105
 
 
106
        if (write(sock, &cmd, 4) != 4)
 
107
                perror("write()");      
 
108
 
 
109
        close(sock);
 
110
 
 
111
        return 0;
 
112
}