summaryrefslogtreecommitdiff
path: root/harnesses/host_controller/utils/ipc/file_lock.py
blob: 67b6bf7c9dee8f2952ee0894157ac0a75dbe458e (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
#
# Copyright (C) 2018 The Android Open Source Project
#
# 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.
#

import fcntl
import logging
import os

from host_controller import common


class FileLock(object):
    """Class for using files as a locking mechanism for devices.

    This is for checking whether a certain device is running a job or not when
    the automated self-update happens.

    Attributes:
        _devlock_dir: string, represent the home directory of the user.
        _lock_fd: dict, maps serial number of the devices and file descriptor.
    """

    def __init__(self, file_name=None, mode=None):
        """Initializes the file lock managing class instance.

        Args:
            file_name: string, name of the file to be opened. Existing lock
                       files will be opened if given as None
            mode: string, the mode argument to open() function.
        """
        self._lock_fd = {}
        self._devlock_dir = os.path.join(
            os.path.expanduser("~"), common._DEVLOCK_DIR)
        if not os.path.exists(self._devlock_dir):
            os.mkdir(self._devlock_dir)

        if file_name:
            file_list = [file_name]
        else:
            file_list = [file_name for file_name in os.listdir(self._devlock_dir)]
        logging.debug("file_list for file lock: %s", file_list)

        for file_name in file_list:
            if os.path.isfile(os.path.join(self._devlock_dir, file_name)):
                self._OpenFile(file_name, mode)

    def _OpenFile(self, serial, mode=None):
        """Opens the given lock file and store the file descriptor to _lock_fd.

        Args:
            serial: string, serial number of a device.
            mode: string, the mode argument to open() function.
                  Set to "w+" if given as None.
        """
        if serial in self._lock_fd and self._lock_fd[serial]:
            logging.info("Lock for the device %s already exists." % serial)
            return

        try:
            if not mode:
                mode = "w+"
            self._lock_fd[serial] = open(
                os.path.join(self._devlock_dir, serial), mode, 0)
        except IOError as e:
            logging.exception(e)
            return False

    def LockDevice(self, serial, suppress_lock_warning=False, block=False):
        """Tries to lock the file corresponding to "serial".

        Args:
            serial: string, serial number of a device.
            suppress_lock_warning: bool, True to suppress warning log output.
            block: bool, True to block at the fcntl.lockf(), waiting for it
                   to be unlocked.

        Returns:
            True if successfully acquired the lock. False otherwise.
        """
        if serial not in self._lock_fd:
            ret = self._OpenFile(serial)
            if ret == False:
                return ret

        try:
            operation = fcntl.LOCK_EX
            if not block:
                operation |= fcntl.LOCK_NB
            fcntl.lockf(self._lock_fd[serial], operation)
        except IOError as e:
            if not suppress_lock_warning:
                logging.exception(e)
            return False

        return True

    def UnlockDevice(self, serial):
        """Releases the lock file corresponding to "serial".

        Args:
            serial: string, serial number of a device.
        """
        if serial not in self._lock_fd:
            logging.error("Lock for the device %s does not exist." % serial)
            return False

        fcntl.lockf(self._lock_fd[serial], fcntl.LOCK_UN)