tizen 2.3 release
[framework/system/swap-probe.git] / helper / dastdout.c
1 /*
2  *  DA probe
3  *
4  * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  *
8  * Cherepanov Vitaliy <v.cherepanov@samsung.com>
9  *
10  * This library is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU Lesser General Public License as published by the
12  * Free Software Foundation; either version 2.1 of the License, or (at your option)
13  * any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, write to the Free Software Foundation, Inc., 51
22  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  * Contributors:
25  * - S-Core Co., Ltd
26  * - Samsung RnD Institute Russia
27  *
28  */
29
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35
36 #include <errno.h>
37 #include "dahelper.h"
38
39 static inline int __redirect(int old, int new)
40 {
41         int ret = 0;
42
43         if (dup2(old, new) == -1) {
44                 /* fail to print up there. there is no sdterr */
45                 /* TODO inform about problem */
46                 fprintf(stderr, "dup2 fail\n");
47                 ret = -1;
48         }
49
50         return ret;
51 }
52
53 int __redirect_std(void)
54 {
55 #ifdef STDTOFILE
56         char STDOUT[MAX_PATH_LENGTH];
57         snprintf(STDOUT,sizeof(STDOUT), "/tmp/da_preloaded_%d_%d.log", getppid(), getpid());
58 #else
59         #define STDOUT          "/dev/null"
60 #endif
61
62         int ret = 0;
63         int fd = open(STDOUT, O_WRONLY | O_CREAT | O_TRUNC, 0777);
64         if (fd != -1) {
65                 if (__redirect(fd, 1) != 0 ||
66                     __redirect(fd, 2) != 0) {
67                         /* fail to print up there. there is no sdterr */
68                         /* TODO inform about problem */
69                         /* fprintf(sdterr, "duplicate fd fail\n"); */
70                         ret = -1;
71                 }
72
73                 close(fd);
74         } else {
75                 /* TODO inform about problem */
76                 close(1);
77                 close(2);
78         }
79
80         close(0);
81         return ret;
82 }