-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgoogleads-doubleclick
executable file
·70 lines (58 loc) · 1.73 KB
/
googleads-doubleclick
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
#!/bin/bash
help() {
echo 'usage: sudo googleads-doubleclick allow|block'
echo ' googleads-doubleclick --help'
echo
echo ' Will un/comment the `127.0.0.2 googleads.g.doubleclick.net`'
echo ' entry in `/etc/hosts`.'
echo
echo ' This serves to black/whitelist that'
echo ' host as needed in order to access google services which'
echo ' redirect via that host if the respective cookie is not set'
echo
echo ' You probably want something like this:'
echo
echo ' user ALL=NOPASSWD: /path/to/googleads-doubleclick allow'
echo ' user ALL=NOPASSWD: /path/to/googleads-doubleclick block'
echo
exit 1
}
[ "$1" == "--help" ] && help
set -e # stop on error
set -u # stop on undefined variable
set -o pipefail # stop part of pipeline failing
state_file=/tmp/googleads-doubleclick.state.lock
allow_googleads() {
hosts allow googleads.g.doubleclick.net || exit 1
# note that parent is accessing googleads
echo $PPID >> $state_file
}
block_googleads() {
# note that parent doesn't need googleads-doubleclick access any more
sed --in-place "/^$PPID$/d" $state_file
for i in `cat $state_file`; do
if ps --pid $i > /dev/null; then
# there is another process using googleads
return
fi
done
# else
# no other process is using googleads
> $state_file # empty the file
hosts block googleads.g.doubleclick.net || exit 1
}
# use mutex
#
# see https://stackoverflow.com/a/50419392 on how this works
#
: >> $state_file # create a file if it doesn't exist
{
flock $lock_fd # lock file by filedescriptor
if [ "$1" == "allow" ]; then
allow_googleads
elif [ "$1" == "block" ]; then
block_googleads
else
help
fi
} {lock_fd}<$state_file