summaryrefslogtreecommitdiff
path: root/tizen/src/util/osutil.c
blob: b2bb82b3bf0c2ecc8161ede0a6991ff604f4c897 (plain)
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
/*
 * Emulator
 *
 * Copyright (C) 2012, 2013 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact:
 * SeokYeon Hwang <syeon.hwang@samsung.com>
 * MunKyu Im <munkyu.im@samsung.com>
 * GiWoong Kim <giwoong.kim@samsung.com>
 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
 * HyunJun Son
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Contributors:
 * - S-Core Co., Ltd
 *
 */

/**
  @file     osutil.c
  @brief    Common functions for osutil
 */

#include "osutil.h"
#include "debug_ch.h"

#include <curl/curl.h>
#include <string.h>

#ifndef CONFIG_WIN32
MULTI_DEBUG_CHANNEL(emulator, osutil);
static sdcard_info info;

static int fd_lock(int fd)
{
    struct flock lock;

    lock.l_type = F_WRLCK;
    lock.l_start = 0;
    lock.l_whence = SEEK_SET;
    lock.l_len = 0;
    lock.l_pid = getpid();

    return fcntl(fd, F_SETLK, &lock);
}

static int fd_unlock(int fd)
{
    struct flock lock;

    lock.l_type = F_UNLCK;
    lock.l_start = 0;
    lock.l_whence = SEEK_SET;
    lock.l_len = 0;
    lock.l_pid = getpid();

    return fcntl(fd, F_SETLK, &lock);
}

bool make_sdcard_lock_posix(char *sdcard)
{
    char *lock_file = g_strdup_printf("%s-%d.lck", sdcard, getpid());
    int fd = open(lock_file, O_CREAT|O_RDWR, 0666);
    if (fd == -1)
    {
        perror("file open error : ");
        return false;
    }
    if (fd_lock(fd) == -1)
    {
        perror("file is lock ");
        close(fd);
        return false;
    }
    info.fd = fd;
    INFO("Get file lock: %s\n", lock_file);
    return true;
}

int remove_sdcard_lock_posix(char *sdcard)
{
    errno = 0;
    char *lock_file = g_strdup_printf("%s-%d.lck", sdcard, getpid());
    int fd = open(lock_file, O_RDWR, 0666);
    if (fd == -1)
    {
        perror("file open error : ");
        if(errno == ENOENT) {
            return ERR_NOENT;
        }
        return ERR_UNLCK;
    }

    if (fd_unlock(fd) == -1)
    {
        perror("file unlock error ");
        close(fd);
        return ERR_UNLCK;
    }

    if (unlink(lock_file) < 0) {
        perror("unlink error ");
        close(fd);
        return ERR_UNLCK;
    }

    INFO("unlock success: %s\n", lock_file);
    close(fd);
    close(info.fd);
    return ERR_SUCCESS;
}
#endif