1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
/*
* power-manager
* Copyright (c) 2012 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file util.c
* @version 0.1
* @brief Utilities for Power manager
*
* This file includes logging, daemonize
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#ifdef ENABLE_DLOG_OUT
#define LOG_TAG "POWER_MANAGER"
#endif
#include "util.h"
/**
* @addtogroup POWER_MANAGER
* @{
*/
/**
* @brief logging function
*
* This is log wrapper
*
* @param[in] priority log pritority
* @param[in] fmt format string
*/
void pm_log(int priority, char *fmt, ...)
{
va_list ap;
char buf[NAME_MAX]; /* NAME_MAX is 255 */
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
#ifdef ENABLE_DLOG_OUT
switch (priority) {
case DLOG_DEBUG:
SLOGD("%s", buf);
break;
case DLOG_ERROR:
SLOGE("%s", buf);
break;
case DLOG_INFO:
SLOGI("%s", buf);
break;
default:
SLOGV("%s", buf);
break;
}
#else
syslog(priority, "%s", buf);
#endif
printf("\x1b[1;33;44m[PowerManager] %s\x1b[0m\n\n", buf);
}
/**
* @brief write the pid
*
* get a pid and write it to pidpath
*
* @param[in] pidpath pid file path
* @return 0 (always)
*/
int writepid(char *pidpath)
{
FILE *fp;
fp = fopen(pidpath, "w");
if (fp != NULL) {
fprintf(fp, "%d", getpid());
fclose(fp);
}
return 0;
}
/**
* @brief read the pid
*
* get a pid and write it to pidpath
*
* @param[in] pidpath pid file path
* @return pid : success, -1 : failed
*/
int readpid(char *pidpath)
{
FILE *fp;
int ret = -1;
fp = fopen(pidpath, "r");
if (fp != NULL) {
fscanf(fp, "%5d", &ret);
fclose(fp);
}
return ret;
}
/**
* @brief daemonize function
*
* fork the process, kill the parent process
* and replace all the standard fds to /dev/null.
*
* @return 0 : success, -1 : fork() error
*/
int daemonize(void)
{
pid_t pid;
pid = fork();
if (pid < 0)
return -1;
else if (pid != 0)
exit(0);
setsid();
chdir("/");
close(0);
close(1);
close(2);
open("/dev/null", O_RDONLY);
open("/dev/null", O_RDWR);
dup(1);
return 0;
}
/**
* @brief function to run a process
*
* fork the process, and run the other process if it is child.
*
* @return new process pid on success, -1 on error
*/
int exec_process(char *name)
{
int ret, pid;
int i;
if (name[0] == '\0')
return 0;
pid = fork();
switch (pid) {
case -1:
LOGERR("Fork error");
ret = -1;
break;
case 0:
for (i = 0; i < _NSIG; i++)
signal(i, SIG_DFL);
execlp(name, name, NULL);
LOGERR("execlp() error : %s\n", strerror(errno));
exit(-1);
break;
default:
ret = pid;
break;
}
return ret;
}
char *get_pkgname(char *exepath)
{
char *filename;
char pkgname[NAME_MAX];
filename = strrchr(exepath, '/');
if (filename == NULL)
filename = exepath;
else
filename = filename + 1;
snprintf(pkgname, NAME_MAX, "deb.com.samsung.%s", filename);
return strdup(pkgname);
}
/**
* @}
*/
|