blob: 095189b05fdfc5c23afdb09f611007640e311789 [file] [log] [blame]
Zijie He825f65e2017-08-16 14:56:42 -07001/*
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/desktop_capture/screen_drawer_lock_posix.h"
Zijie He825f65e2017-08-16 14:56:42 -070012
13#include <fcntl.h>
14#include <sys/stat.h>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
17#include "rtc_base/logging.h"
Zijie He825f65e2017-08-16 14:56:42 -070018
19namespace webrtc {
20
21namespace {
22
23// A uuid as the name of semaphore.
24static constexpr char kSemaphoreName[] = "GSDL54fe5552804711e6a7253f429a";
25
26} // namespace
27
28ScreenDrawerLockPosix::ScreenDrawerLockPosix()
29 : ScreenDrawerLockPosix(kSemaphoreName) {}
30
31ScreenDrawerLockPosix::ScreenDrawerLockPosix(const char* name) {
32 semaphore_ = sem_open(name, O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO, 1);
33 if (semaphore_ == SEM_FAILED) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010034 RTC_LOG_ERRNO(LS_ERROR) << "Failed to create named semaphore with " << name;
Zijie He825f65e2017-08-16 14:56:42 -070035 RTC_NOTREACHED();
36 }
37
38 sem_wait(semaphore_);
39}
40
41ScreenDrawerLockPosix::~ScreenDrawerLockPosix() {
42 if (semaphore_ == SEM_FAILED) {
43 return;
44 }
45
46 sem_post(semaphore_);
47 sem_close(semaphore_);
48 // sem_unlink a named semaphore won't wait until other clients to release the
49 // sem_t. So if a new process starts, it will sem_open a different kernel
50 // object with the same name and eventually breaks the cross-process lock.
51}
52
53// static
54void ScreenDrawerLockPosix::Unlink(const char* name) {
55 sem_unlink(name);
56}
57
58} // namespace webrtc