~ubuntu-branches/ubuntu/utopic/thermald/utopic

« back to all changes in this revision

Viewing changes to .pc/0019-csys_fs-fix-fd-leaks-in-lseek-error-return-path.patch/src/thd_sys_fs.cpp

  • Committer: Package Import Robot
  • Author(s): Colin King
  • Date: 2014-05-19 12:41:22 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20140519124122-zan11arvjxtbqcq5
Tags: 1.2-1
* Adjust for coretemp path change
* Deny non root users to send system bus dbus messages
* Remove compile warning
* Remove rpmlint warning for manual page
* Remove old patches that are now included into version 1.2
* Sync up with version 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * thd_sys_fs.cpp: sysfs class implementation
3
 
 *
4
 
 * Copyright (C) 2012 Intel Corporation. All rights reserved.
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or
7
 
 * modify it under the terms of the GNU General Public License version
8
 
 * 2 or later as published by the Free Software Foundation.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program; if not, write to the Free Software
17
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18
 
 * 02110-1301, USA.
19
 
 *
20
 
 *
21
 
 * Author Name <Srinivas.Pandruvada@linux.intel.com>
22
 
 *
23
 
 */
24
 
 
25
 
#include "thd_sys_fs.h"
26
 
#include "thd_common.h"
27
 
#include <stdlib.h>
28
 
 
29
 
int csys_fs::write(const std::string &path, const std::string &buf) {
30
 
        std::string p = base_path + path;
31
 
        int fd = ::open(p.c_str(), O_WRONLY);
32
 
        if (fd < 0) {
33
 
                thd_log_warn("sysfs write failed %s\n", path.c_str());
34
 
                return -errno;
35
 
        }
36
 
        int ret = ::write(fd, buf.c_str(), buf.size());
37
 
        if (ret < 0)
38
 
                thd_log_warn("sysfs write failed %s\n", path.c_str());
39
 
        close(fd);
40
 
 
41
 
        return ret;
42
 
}
43
 
 
44
 
int csys_fs::write(const std::string &path, unsigned int position, unsigned
45
 
long long data) {
46
 
        std::string p = base_path + path;
47
 
        int fd = ::open(p.c_str(), O_WRONLY);
48
 
        if (fd < 0) {
49
 
                thd_log_warn("sysfs write failed %s\n", path.c_str());
50
 
                return -errno;
51
 
        }
52
 
        if (::lseek(fd, position, SEEK_CUR) == -1) {
53
 
                thd_log_warn("sysfs write failed %s\n", path.c_str());
54
 
                return -errno;
55
 
        }
56
 
        int ret = ::write(fd, &data, sizeof(data));
57
 
        if (ret < 0)
58
 
                thd_log_warn("sysfs write failed %s\n", path.c_str());
59
 
        close(fd);
60
 
 
61
 
        return ret;
62
 
}
63
 
 
64
 
int csys_fs::write(const std::string &path, unsigned int data) {
65
 
        std::ostringstream os;
66
 
        os << data;
67
 
        return csys_fs::write(path, os.str());
68
 
}
69
 
 
70
 
int csys_fs::read(const std::string &path, char *buf, int len) {
71
 
        std::string p = base_path + path;
72
 
        int fd = ::open(p.c_str(), O_RDONLY);
73
 
        if (fd < 0) {
74
 
                thd_log_warn("sysfs read failed %s\n", path.c_str());
75
 
                return -errno;
76
 
        }
77
 
        int ret = ::read(fd, buf, len);
78
 
        if (ret < 0)
79
 
                thd_log_warn("sysfs read failed %s\n", path.c_str());
80
 
        close(fd);
81
 
 
82
 
        return ret;
83
 
}
84
 
 
85
 
int csys_fs::read(const std::string &path, unsigned int position, char *buf,
86
 
                int len) {
87
 
        std::string p = base_path + path;
88
 
        int fd = ::open(p.c_str(), O_RDONLY);
89
 
        if (fd < 0) {
90
 
                thd_log_warn("sysfs read failed %s\n", path.c_str());
91
 
                return -errno;
92
 
        }
93
 
        if (::lseek(fd, position, SEEK_CUR) == -1) {
94
 
                thd_log_warn("sysfs read failed %s\n", path.c_str());
95
 
                return -errno;
96
 
        }
97
 
        int ret = ::read(fd, buf, len);
98
 
        if (ret < 0)
99
 
                thd_log_warn("sysfs read failed %s\n", path.c_str());
100
 
        close(fd);
101
 
 
102
 
        return ret;
103
 
}
104
 
 
105
 
int csys_fs::read(const std::string &path, unsigned int *ptr_val) {
106
 
        std::string p = base_path + path;
107
 
        char str[16];
108
 
        int ret;
109
 
 
110
 
        int fd = ::open(p.c_str(), O_RDONLY);
111
 
        if (fd < 0) {
112
 
                thd_log_warn("sysfs read failed %s\n", path.c_str());
113
 
                return -errno;
114
 
        }
115
 
        ret = ::read(fd, str, sizeof(str));
116
 
        if (ret > 0)
117
 
                *ptr_val = atoi(str);
118
 
        else
119
 
                thd_log_warn("sysfs read failed %s\n", path.c_str());
120
 
        close(fd);
121
 
 
122
 
        return ret;
123
 
}
124
 
 
125
 
int csys_fs::read(const std::string &path, unsigned long *ptr_val) {
126
 
        std::string p = base_path + path;
127
 
        char str[32];
128
 
        int ret;
129
 
 
130
 
        int fd = ::open(p.c_str(), O_RDONLY);
131
 
        if (fd < 0) {
132
 
                thd_log_warn("sysfs read failed %s\n", path.c_str());
133
 
                return -errno;
134
 
        }
135
 
        ret = ::read(fd, str, sizeof(str));
136
 
        if (ret > 0)
137
 
                *ptr_val = atol(str);
138
 
        else
139
 
                thd_log_warn("sysfs read failed %s\n", path.c_str());
140
 
        close(fd);
141
 
 
142
 
        return ret;
143
 
}
144
 
 
145
 
int csys_fs::read(const std::string &path, std::string &buf) {
146
 
        std::string p = base_path + path;
147
 
 
148
 
        std::ifstream f(p.c_str(), std::fstream::in);
149
 
        if (f.fail()) {
150
 
                thd_log_warn("sysfs read failed %s\n", path.c_str());
151
 
                return -EINVAL;
152
 
        }
153
 
        int ret = 0;
154
 
        f >> buf;
155
 
        if (f.bad()) {
156
 
                thd_log_warn("sysfs read failed %s\n", path.c_str());
157
 
                ret = -EIO;
158
 
        }
159
 
        f.close();
160
 
 
161
 
        return ret;
162
 
}
163
 
 
164
 
bool csys_fs::exists(const std::string &path) {
165
 
        struct stat s;
166
 
 
167
 
        return (bool) (stat((base_path + path).c_str(), &s) == 0);
168
 
}
169
 
 
170
 
bool csys_fs::exists() {
171
 
        return csys_fs::exists("");
172
 
}
173
 
 
174
 
int csys_fs::read_symbolic_link_value(const std::string &path, char *buf,
175
 
                int len) {
176
 
        std::string p = base_path + path;
177
 
        int ret = ::readlink(p.c_str(), buf, len);
178
 
        if (ret < 0) {
179
 
                thd_log_warn("read_symbolic_link %s\n", path.c_str());
180
 
                return -errno;
181
 
        }
182
 
        buf[ret] = '\0';
183
 
        return 0;
184
 
}